My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.gwt.user.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;

import java.util.Iterator;

/**
* Abstract base class for all panels, which are widgets that can contain other
* widgets.
*/
public abstract class Panel extends Widget implements HasWidgets {

/**
* Adds a child widget.
*
* <p>
* <b>How to Override this Method</b>
* </p>
* <p>
* There are several important things that must take place in the correct
* order to properly add or insert a Widget to a Panel. Not all of these steps
* will be relevant to every Panel, but all of the steps must be considered.
* <ol>
* <li><b>Validate:</b> Perform any sanity checks to ensure the Panel can
* accept a new Widget. Examples: checking for a valid index on insertion;
* checking that the Panel is not full if there is a max capacity.</li>
* <li><b>Adjust for Reinsertion:</b> Some Panels need to handle the case
* where the Widget is already a child of this Panel. Example: when performing
* a reinsert, the index might need to be adjusted to account for the Widget's
* removal. See {@link ComplexPanel#adjustIndex(Widget, int)}.</li>
* <li><b>Detach Child:</b> Remove the Widget from its existing parent, if
* any. Most Panels will simply call {@link Widget#removeFromParent()} on the
* Widget.</li>
* <li><b>Logical Attach:</b> Any state variables of the Panel should be
* updated to reflect the addition of the new Widget. Example: the Widget is
* added to the Panel's {@link WidgetCollection} at the appropriate index.</li>
* <li><b>Physical Attach:</b> The Widget's Element must be physically
* attached to the Panel's Element, either directly or indirectly.</li>
* <li><b>Adopt:</b> Call {@link #adopt(Widget)} to finalize the add as the
* very last step.</li>
* </ol>
* </p>
*
* @param child the widget to be added
* @throws UnsupportedOperationException if this method is not supported (most
* often this means that a specific overload must be called)
* @see HasWidgets#add(Widget)
*/
public void add(Widget child) {
throw new UnsupportedOperationException(
"This panel does not support no-arg add()");
}

public void clear() {
Iterator<Widget> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}

/**
* Removes a child widget.
*
* <p>
* <b>How to Override this Method</b>
* </p>
* <p>
* There are several important things that must take place in the correct
* order to properly remove a Widget from a Panel. Not all of these steps will
* be relevant to every Panel, but all of the steps must be considered.
* <ol>
* <li><b>Validate:</b> Make sure this Panel is actually the parent of the
* child Widget; return <code>false</code> if it is not.</li>
* <li><b>Orphan:</b> Call {@link #orphan(Widget)} first while the child
* Widget is still attached.</li>
* <li><b>Physical Detach:</b> Adjust the DOM to account for the removal of
* the child Widget. The Widget's Element must be physically removed from the
* DOM.</li>
* <li><b>Logical Detach:</b> Update the Panel's state variables to reflect
* the removal of the child Widget. Example: the Widget is removed from the
* Panel's {@link WidgetCollection}.</li>
* </ol>
* </p>
*
* @param child the widget to be removed
* @return <code>true</code> if the child was present
*/
public abstract boolean remove(Widget child);

/**
* Finalize the attachment of a Widget to this Panel. This method is the
* <b>last</b> step in adding or inserting a Widget into a Panel, and should
* be called after physical attachment in the DOM is complete. This Panel
* becomes the parent of the child Widget, and the child will now fire its
* {@link Widget#onAttach()} event if this Panel is currently attached.
*
* @param child the widget to be adopted
* @see #add(Widget)
*/
protected final void adopt(Widget child) {
assert (child.getParent() == null);
child.setParent(this);
}

/**
* This method was formerly part of the process of adding a Widget to a Panel
* but has been deprecated in favor of {@link #adopt(Widget)}.
*
* @deprecated Use {@link #adopt(Widget)}.
*/
@Deprecated
protected void adopt(Widget w, Element container) {
// Remove the widget from its current parent, if any.
w.removeFromParent();

// Attach it at the DOM and GWT levels.
if (container != null) {
DOM.appendChild(container, w.getElement());
}
w.setParent(this);
}

/**
* This method was formerly part of the process of removing a Widget from a
* Panel but has been deprecated in favor of {@link #orphan(Widget)}.
*
* @deprecated Use {@link #orphan(Widget)}.
*/
@Deprecated
protected void disown(Widget w) {
// Only disown it if it's actually contained in this panel.
if (w.getParent() != this) {
throw new IllegalArgumentException("w is not a child of this panel");
}

// setParent() must be called before removeChild() to ensure that the
// element is still attached when onDetach()/onUnload() are called.
Element elem = w.getElement();
w.setParent(null);
DOM.removeChild(DOM.getParent(elem), elem);
}

@Override
protected void doAttachChildren() {
AttachDetachException.tryCommand(this, AttachDetachException.attachCommand);
}

@Override
protected void doDetachChildren() {
AttachDetachException.tryCommand(this, AttachDetachException.detachCommand);
}

/**
* A Panel's onLoad method will be called after all of its children are
* attached.
*
* @see Widget#onLoad()
*/
@Override
protected void onLoad() {
}

/**
* A Panel's onUnload method will be called before its children become
* detached themselves.
*
* @see Widget#onLoad()
*/
@Override
protected void onUnload() {
}

/**
* <p>
* This method must be called as part of the remove method of any Panel. It
* ensures that the Widget's parent is cleared. This method should be called
* after verifying that the child Widget is an existing child of the Panel,
* but before physically removing the child Widget from the DOM. The child
* will now fire its {@link Widget#onDetach()} event if this Panel is
* currently attached.
* </p>
* <p>
* Calls to {@link #orphan(Widget)} should be wrapped in a try/finally block
* to ensure that the widget is physically detached even if orphan throws an
* exception.
* </p>
*
* @param child the widget to be disowned
* @see #add(Widget)
*/
protected final void orphan(Widget child) {
assert (child.getParent() == this);
child.setParent(null);
}
}

Change log

r7335 by sco...@google.com on Dec 18, 2009   Diff
Tagging 2.0.0.
Go to: 
Project members, sign in to write a code review

Older revisions

r6420 by rj...@google.com on Oct 19, 2009   Diff
Creates the 2.0 release branch for MS2
and beyond as a
straight copy of trunk@6417.

svn cp -r 6417 https://google-web-
...
r6197 by jlaba...@google.com on Sep 23, 2009   Diff
Wraps all calls that affect phsyical
and logical attach state to ensure
that the physical and logical state of
children match the state of their
parents. Essentially, this patch
...
r1379 by gwt.team.knorton on Aug 24, 2007   Diff
Updated to Java1.5 constructs.

Review by: jlabanca, ajr

All revisions of this file

File info

Size: 7418 bytes, 212 lines

File properties

svn:mime-type
text/x-java
svn:eol-style
native
Powered by Google Project Hosting