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;
017    
018    import asquare.gwt.tk.client.ui.EventWrapper;
019    import asquare.gwt.tk.client.ui.behavior.EventDelegate;
020    
021    import com.google.gwt.user.client.DOM;
022    import com.google.gwt.user.client.Event;
023    import com.google.gwt.user.client.ui.Widget;
024    
025    /**
026     * A widget wrapper which delegates events to an
027     * {@link asquare.gwt.tk.client.ui.behavior.EventDelegate EventDelegate}. The
028     * wrapped widget will allowed to process events before they are passed to the
029     * delegate.
030     */
031    public class DelegatingWrapper extends EventWrapper
032    {
033            private final EventDelegate m_delegate;
034                    
035            /**
036             * @throws NullPointerException if <code>widget</code> or <code>delegate</code> are null
037             */
038            public DelegatingWrapper(Widget widget, EventDelegate delegate)
039            {
040                    super(widget, delegate.getEventBits());
041                    m_delegate = delegate;
042            }
043            
044            /*
045             *  (non-Javadoc)
046             * @see com.google.gwt.user.client.EventListener#onBrowserEvent(com.google.gwt.user.client.Event)
047             */
048            public void onBrowserEvent(Event event)
049            {
050                    super.onBrowserEvent(event);
051                    if ((m_delegate.getEventBits() & DOM.eventGetType(event)) != 0)
052                    {
053                            m_delegate.onBrowserEvent(getWidget(), event);
054                    }
055            }
056    }