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 com.google.gwt.user.client.DOM;
019    import com.google.gwt.user.client.Event;
020    import com.google.gwt.user.client.ui.Composite;
021    import com.google.gwt.user.client.ui.Widget;
022    
023    /**
024     * A widget wrapper which can process the
025     * wrapped widget's events. 
026     */
027    public abstract class EventWrapper extends Composite
028    {
029            private Widget m_widget;
030            
031            /**
032             * Default constructor for convenience. You will need to call
033             * {@link #initWidget(Widget)} before calling any {@link Widget} methods.
034             */
035            public EventWrapper()
036            {
037                    
038            }
039            
040            public EventWrapper(Widget w, int eventMask)
041            {
042                    initWidget(w);
043                    sinkEvents(eventMask);
044            }
045            
046            /*
047             * (non-Javadoc)
048             * @see com.google.gwt.user.client.ui.Composite#initWidget(com.google.gwt.user.client.ui.Widget)
049             */
050            protected void initWidget(Widget widget)
051            {
052                    super.initWidget(widget);
053                    m_widget = widget;
054            }
055            
056            protected Widget getWidget()
057            {
058                    return m_widget;
059            }
060            
061            /*
062             *  (non-Javadoc)
063             * Hijacks events intended for the wrapped widget's element. 
064             * @see com.google.gwt.user.client.ui.Widget#onAttach()
065             */
066            protected void onAttach()
067            {
068                    super.onAttach();
069                    DOM.setEventListener(getElement(), this);
070            }
071            
072            /**
073             * Override to process browser events. Don't forget to call super
074             * implementation if you want the wrapped widget to process events also.
075             * {@inheritDoc}
076             */
077            public void onBrowserEvent(Event event)
078            {
079                    m_widget.onBrowserEvent(event);
080            }
081    }