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.behavior.impl;
017    
018    import asquare.gwt.tk.client.ui.behavior.*;
019    
020    import com.google.gwt.user.client.ui.FocusListener;
021    import com.google.gwt.user.client.ui.HasFocus;
022    import com.google.gwt.user.client.ui.Widget;
023    
024    public abstract class AbstractTabFocusController extends ControllerAdaptor implements TabFocusController, FocusListener, FocusModelListener
025    {
026            private FocusModel m_focusModel = null;
027            
028            public AbstractTabFocusController()
029            {
030                    this(0);
031            }
032            
033            protected AbstractTabFocusController(int eventBits)
034            {
035                    super(eventBits, TabFocusController.class);
036            }
037            
038            public FocusModel getModel()
039            {
040                    return m_focusModel;
041            }
042            
043            public void setModel(FocusModel focusModel)
044            {
045                    if (m_focusModel != null)
046                    {
047                            for (int i = 0; i < m_focusModel.getSize(); i++)
048                            {
049                                    m_focusModel.getWidgetAt(i).removeFocusListener(this);
050                            }
051                            m_focusModel.removeListener(this);
052                    }
053                    if (focusModel != null)
054                    {
055                            m_focusModel = focusModel;
056                            m_focusModel.addListener(this);
057                            for (int i = 0; i < m_focusModel.getSize(); i++)
058                            {
059                                    m_focusModel.getWidgetAt(i).addFocusListener(this);
060                            }
061                    }
062            }
063            
064            // FocusListener methods
065            public void onFocus(Widget sender)
066            {
067                    assert sender instanceof HasFocus;
068                    m_focusModel.setFocusWidget((HasFocus) sender);
069            }
070            
071            public void onLostFocus(Widget sender)
072            {
073            }
074            
075            // FocusModelListener
076            public void widgetsAdded(FocusModel model, HasFocus[] added)
077            {
078                    for (int i = 0; i < added.length; i++)
079                    {
080                            added[i].addFocusListener(this);
081                    }
082            }
083            
084            public void widgetsRemoved(FocusModel model, HasFocus[] removed)
085            {
086                    for (int i = 0; i < removed.length; i++)
087                    {
088                            removed[i].removeFocusListener(this);
089                    }
090            }
091    }