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