001    /*
002     * Copyright 2006 Google Inc.
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 com.google.gwt.user.client.ui;
017    
018    import com.google.gwt.user.client.DOM;
019    import com.google.gwt.user.client.Event;
020    
021    import java.util.Iterator;
022    import java.util.Vector;
023    
024    /**
025     * A helper class for implementers of the
026     * {@link com.google.gwt.user.client.ui.SourcesFocusEvents} interface. This
027     * subclass of Vector assumes that all objects added to it will be of type
028     * {@link com.google.gwt.user.client.ui.FocusListener}.
029     */
030    public class FocusListenerCollection extends Vector {
031    
032      /**
033       * Fires a focus event to all listeners.
034       * 
035       * @param sender the widget sending the event.
036       */
037      public void fireFocus(Widget sender) {
038        for (Iterator it = iterator(); it.hasNext();) {
039          FocusListener listener = (FocusListener) it.next();
040          listener.onFocus(sender);
041        }
042      }
043    
044      /**
045       * A helper for widgets that source focus events.
046       * 
047       * @param sender the widget sending the event.
048       * @param evt the {@link Event DOM event} received by the widget.
049       */
050      public void fireFocusEvent(Widget sender, Event event) {
051        switch (DOM.eventGetType(event)) {
052          case Event.ONFOCUS:
053            fireFocus(sender);
054            break;
055    
056          case Event.ONBLUR:
057            fireLostFocus(sender);
058            break;
059        }
060      }
061    
062      /**
063       * Fires a lost-focus event to all listeners.
064       * 
065       * @param sender the widget sending the event.
066       */
067      public void fireLostFocus(Widget sender) {
068        for (Iterator it = iterator(); it.hasNext();) {
069          FocusListener listener = (FocusListener) it.next();
070          listener.onLostFocus(sender);
071        }
072      }
073    }