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 java.util.List;
019    
020    import asquare.gwt.tk.client.ui.behavior.Controller;
021    import asquare.gwt.tk.client.ui.behavior.ControllerSupport;
022    import asquare.gwt.tk.client.ui.behavior.ControllerSupportDelegate;
023    
024    import com.google.gwt.user.client.DOM;
025    import com.google.gwt.user.client.Event;
026    import com.google.gwt.user.client.ui.Widget;
027    
028    /**
029     * Wraps an widget to provide event processing via
030     * {@link asquare.gwt.tk.client.ui.behavior.Controller controllers}. The
031     * wrapped widget will process events and notify listeners as if it was not
032     * wrapped.
033     */
034    public class CWrapper extends EventWrapper implements ControllerSupport
035    {
036            private final ControllerSupportDelegate m_controllerSupport;
037            
038            /**
039             * Creates a CWrapper around the specified widget. 
040             */
041            public CWrapper(Widget widget)
042            {
043                    this(widget, null);
044            }
045            
046            /**
047             * Creates a CWrapper around the specified widget. Pass a
048             * value for <code>controllers</code> if you wish to avoid controller
049             * creation via {@link #createControllers()}. Otherwise pass null. 
050             * 
051             * @param controllers a list of 0 or more controllers, or <code>null</code>
052             * @throws IllegalArgumentException if <code>element</code> is null
053             */
054            public CWrapper(Widget widget, List controllers)
055            {
056                    initWidget(widget);
057                    m_controllerSupport = new ControllerSupportDelegate(this);
058                    
059                    if (controllers == null)
060                            controllers = createControllers();
061                    
062                    setControllers(controllers);
063            }
064            
065            /**
066             * A factory method which gives subclasses the opportunity to override
067             * default controller creation.
068             * 
069             * @return a List with 0 or more controllers, or <code>null</code>
070             */
071            protected List createControllers()
072            {
073                    return null;
074            }
075            
076            /*
077             *  (non-Javadoc)
078             * @see asquare.gwt.tk.client.ui.behavior.ControllerSupport#getController(java.lang.Class)
079             */
080            public Controller getController(Class id)
081            {
082                    return m_controllerSupport.getController(id);
083            }
084    
085            /*
086             *  (non-Javadoc)
087             * @see asquare.gwt.tk.client.ui.behavior.ControllerSupport#addController(asquare.gwt.tk.client.ui.behavior.Controller)
088             */
089            public Widget addController(Controller controller)
090            {
091                    return m_controllerSupport.addController(controller);
092            }
093    
094            /*
095             *  (non-Javadoc)
096             * @see asquare.gwt.tk.client.ui.behavior.ControllerSupport#removeController(asquare.gwt.tk.client.ui.behavior.Controller)
097             */
098            public Widget removeController(Controller controller)
099            {
100                    return m_controllerSupport.removeController(controller);
101            }
102            
103            /*
104             *  (non-Javadoc)
105             * @see asquare.gwt.tk.client.ui.behavior.ControllerSupport#setControllers(java.util.List)
106             */
107            public void setControllers(List controllers)
108            {
109                    m_controllerSupport.setControllers(controllers);
110            }
111            
112            /*
113             *  (non-Javadoc)
114             * @see com.google.gwt.user.client.ui.UIObject#sinkEvents(int)
115             */
116            public void sinkEvents(int eventBits)
117            {
118                    m_controllerSupport.sinkEvents(eventBits);
119            }
120    
121            /*
122             *  (non-Javadoc)
123             * @see com.google.gwt.user.client.ui.UIObject#unsinkEvents(int)
124             */
125            public void unsinkEvents(int eventBits)
126            {
127                    m_controllerSupport.unsinkEvents(eventBits);
128            }
129            
130            /*
131             *  (non-Javadoc)
132             * @see com.google.gwt.user.client.ui.Widget#onAttach()
133             */
134            protected void onAttach()
135            {
136                    if (isAttached())
137                            return;
138                    m_controllerSupport.onAttach();
139                    super.onAttach();
140            }
141            
142            /*
143             *  (non-Javadoc)
144             * @see com.google.gwt.user.client.ui.Widget#onDetach()
145             */
146            protected void onDetach()
147            {
148                    if(! isAttached())
149                            return;
150                    
151                    try
152                    {
153                            onUnload();
154                    }
155                    finally
156                    {
157                            super.onDetach();
158                            m_controllerSupport.onDetach();
159                    }
160            }
161            
162            /*
163             *  (non-Javadoc)
164             * @see com.google.gwt.user.client.ui.Widget#onLoad()
165             */
166            protected void onLoad()
167            {
168            }
169            
170            /**
171             * This method is called just before the widget is detached from the
172             * browser's document.
173             */
174            protected void onUnload()
175            {
176            }
177            
178            /*
179             *  (non-Javadoc)
180             * @see com.google.gwt.user.client.EventListener#onBrowserEvent(com.google.gwt.user.client.Event)
181             */
182            public void onBrowserEvent(Event event)
183            {
184                    if ((m_controllerSupport.getLegacyEventBits() & DOM.eventGetType(event)) != 0)
185                    {
186                            super.onBrowserEvent(event);
187                    }
188                    m_controllerSupport.onBrowserEvent(event);
189            }
190    }