KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > Panel


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * Abstract base class for all panels, which are widgets that can contain other
25  * widgets.
26  */

27 public abstract class Panel extends Widget implements HasWidgets {
28
29   public void add(Widget w) {
30     throw new UnsupportedOperationException JavaDoc("This panel does not support no-arg add()");
31   }
32
33   public void clear() {
34     Iterator JavaDoc it = iterator();
35     while (it.hasNext()) {
36       it.next();
37       it.remove();
38     }
39   }
40
41   /**
42    * This method must be called as part of the add method of any panel. It
43    * ensures that the Widget's parent is set properly, and that it is removed
44    * from any existing parent widget. It also attaches the child widget's
45    * DOM element to its new container, ensuring that this process occurs in the
46    * right order.
47    *
48    * @param w the widget to be adopted
49    * @param container the element within which it will be contained
50    */

51   protected void adopt(Widget w, Element container) {
52     // Remove the widget from its current parent, if any.
53
w.removeFromParent();
54
55     // Attach it at the DOM and GWT levels.
56
if (container != null) {
57       DOM.appendChild(container, w.getElement());
58     }
59     w.setParent(this);
60   }
61
62   /**
63    * This method must be called whenever a Widget is removed. It ensures that
64    * the Widget's parent is cleared.
65    *
66    * @param w the widget to be disowned
67    */

68   protected void disown(Widget w) {
69     // Only disown it if it's actually contained in this panel.
70
if (w.getParent() != this) {
71       throw new IllegalArgumentException JavaDoc("w is not a child of this panel");
72     }
73
74     // Remove it at the DOM and GWT levels.
75
Element elem = w.getElement();
76     w.setParent(null);
77     DOM.removeChild(DOM.getParent(elem), elem);
78   }
79
80   protected void onAttach() {
81     super.onAttach();
82
83     // Ensure that all child widgets are attached.
84
for (Iterator JavaDoc it = iterator(); it.hasNext();) {
85       Widget child = (Widget) it.next();
86       child.onAttach();
87     }
88   }
89
90   protected void onDetach() {
91     super.onDetach();
92
93     // Ensure that all child widgets are detached.
94
for (Iterator JavaDoc it = iterator(); it.hasNext();) {
95       Widget child = (Widget) it.next();
96       child.onDetach();
97     }
98   }
99 }
100
Popular Tags