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;
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.Widget;
021    
022    public abstract class EventDelegateAdaptor implements EventDelegate
023    {
024            private final int m_eventBits;
025            
026            /**
027             * Default constructor for convienence. Subclasses must override
028             * {@link #getEventBits()} if they want to process events.
029             */
030            public EventDelegateAdaptor()
031            {
032                    m_eventBits = 0;
033            }
034            
035            /**
036             * Creates an event delegate which is interested in the specified bits.
037             * 
038             * @param eventBits a bitmask of the events to process
039             * @see Event
040             */
041            public EventDelegateAdaptor(int eventBits)
042            {
043                    m_eventBits = eventBits;
044            }
045            
046            /*
047             *  (non-Javadoc)
048             * @see asquare.gwt.tk.client.ui.behavior.EventDelegate#onBrowserEvent(com.google.gwt.user.client.ui.Widget, com.google.gwt.user.client.Event)
049             */
050            public void onBrowserEvent(Widget widget, Event event)
051            {
052                    if (! doBrowserEvent(widget, event))
053                    {
054                            DOM.eventPreventDefault(event);
055                            DOM.eventCancelBubble(event, true);
056                    }
057            }
058            
059            /**
060             * A convenience method for processing events. The event is cancelled if the
061             * return value is false.
062             * 
063             * @return false to cancel the event
064             */
065            protected boolean doBrowserEvent(Widget widget, Event event)
066            {
067                    return true;
068            }
069            
070            /**
071             * {@inheritDoc}
072             * Override in subclass if using the default constructor. 
073             */
074            public int getEventBits()
075            {
076                    return m_eventBits;
077            }
078    }