001    /*
002     * Copyright 2006 Mat Gessel <mat.gessel@gmail.com>
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package asquare.gwt.tk.client.ui.commands;
017    
018    import com.google.gwt.user.client.Command;
019    import com.google.gwt.user.client.ui.HasFocus;
020    
021    /**
022     * Focuses or blurs a widget. 
023     * 
024     * @see com.google.gwt.user.client.DeferredCommand#add(com.google.gwt.user.client.Command)
025     */
026    public class FocusCommand implements Command
027    {
028            private final HasFocus m_widget;
029            private final boolean m_focus;
030            
031            /**
032             * Constructs a command which will focus the specified widget. 
033             * 
034             * @param widget a widget which implements {@link HasFocus}
035             */
036            public FocusCommand(HasFocus widget)
037            {
038                    m_widget = widget;
039                    m_focus = true;
040            }
041            
042            /**
043             * Constructs a command which will focus or blur the specified widget. 
044             * 
045             * @param widget a widget which implements {@link HasFocus}
046             * @param focus <code>true</code> to focus <code>widget</code>, <code>false</code> to blur
047             */
048            public FocusCommand(HasFocus widget, boolean focus)
049            {
050                    m_widget = widget;
051                    m_focus = focus;
052            }
053            
054            /*
055             *  (non-Javadoc)
056             * @see com.google.gwt.user.client.Command#execute()
057             */
058            public void execute()
059            {
060                    m_widget.setFocus(m_focus);
061            }
062    }