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
/*
* Copyright 2008 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.dom.client.Element;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Base class for panels that contain only one widget.
*/
public class SimplePanel extends Panel {

private Widget widget;

/**
* Creates an empty panel that uses a DIV for its contents.
*/
public SimplePanel() {
this(DOM.createDiv());
}

/**
* Creates an empty panel that uses the specified browser element for its
* contents.
*
* @param elem the browser element to use
*/
protected SimplePanel(Element elem) {
setElement(elem);
}

/**
* Adds a widget to this panel.
*
* @param w the child widget to be added
*/
@Override
public void add(Widget w) {
// Can't add() more than one widget to a SimplePanel.
if (getWidget() != null) {
throw new IllegalStateException("SimplePanel can only contain one child widget");
}
setWidget(w);
}

/**
* Gets the panel's child widget.
*
* @return the child widget, or <code>null</code> if none is present
*/
public Widget getWidget() {
return widget;
}

public Iterator<Widget> iterator() {
// Return a simple iterator that enumerates the 0 or 1 elements in this
// panel.
return new Iterator<Widget>() {
boolean hasElement = widget != null;
Widget returned = null;

public boolean hasNext() {
return hasElement;
}

public Widget next() {
if (!hasElement || (widget == null)) {
throw new NoSuchElementException();
}
hasElement = false;
return (returned = widget);
}

public void remove() {
if (returned != null) {
SimplePanel.this.remove(returned);
}
}
};
}

@Override
public boolean remove(Widget w) {
// Validate.
if (widget != w) {
return false;
}

// Orphan.
try {
orphan(w);
} finally {
// Physical detach.
getContainerElement().removeChild(w.getElement());

// Logical detach.
widget = null;
}
return true;
}

/**
* Sets this panel's widget. Any existing child widget will be removed.
*
* @param w the panel's new widget, or <code>null</code> to clear the panel
*/
public void setWidget(Widget w) {
// Validate
if (w == widget) {
return;
}

// Detach new child.
if (w != null) {
w.removeFromParent();
}

// Remove old child.
if (widget != null) {
remove(widget);
}

// Logical attach.
widget = w;

if (w != null) {
// Physical attach.
DOM.appendChild(getContainerElement(), widget.getElement());

adopt(w);
}
}

/**
* Override this method to specify that an element other than the root element
* be the container for the panel's child widget. This can be useful when you
* want to create a simple panel that decorates its contents.
*
* Note that this method continues to return the
* {@link com.google.gwt.user.client.Element} class defined in the
* <code>User</code> module to maintain backwards compatibility.
*
* @return the element to be used as the panel's container
*/
protected com.google.gwt.user.client.Element getContainerElement() {
return getElement();
}
}

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
...
r2468 by br...@google.com on Apr 16, 2008   Diff
Redesign of CaptionPanel, lots of
additional CaptionPanel unit tests,
and a few related changes in
SimplePanel (updated to use new
Element class) and Node (javadoc
...
All revisions of this file

File info

Size: 3979 bytes, 164 lines

File properties

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