001: /*
002: * Copyright 2007 Google Inc.
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 com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.user.client.DOM;
019: import com.google.gwt.user.client.Element;
020:
021: import java.util.Iterator;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * Abstract base class for panels that contain only one widget.
026: */
027: public class SimplePanel extends Panel {
028:
029: private Widget widget;
030:
031: /**
032: * Creates an empty panel that uses a DIV for its contents.
033: */
034: public SimplePanel() {
035: this (DOM.createDiv());
036: }
037:
038: /**
039: * Creates an empty panel that uses the specified browser element for its
040: * contents.
041: *
042: * @param elem the browser element to use
043: */
044: protected SimplePanel(Element elem) {
045: setElement(elem);
046: }
047:
048: /**
049: * Adds a widget to this panel.
050: *
051: * @param w the child widget to be added
052: */
053: @Override
054: public void add(Widget w) {
055: // Can't add() more than one widget to a SimplePanel.
056: if (getWidget() != null) {
057: throw new IllegalStateException(
058: "SimplePanel can only contain one child widget");
059: }
060: setWidget(w);
061: }
062:
063: /**
064: * Gets the panel's child widget.
065: *
066: * @return the child widget, or <code>null</code> if none is present
067: */
068: public Widget getWidget() {
069: return widget;
070: }
071:
072: public Iterator<Widget> iterator() {
073: // Return a simple iterator that enumerates the 0 or 1 elements in this
074: // panel.
075: return new Iterator<Widget>() {
076: boolean hasElement = widget != null;
077: Widget returned = null;
078:
079: public boolean hasNext() {
080: return hasElement;
081: }
082:
083: public Widget next() {
084: if (!hasElement || (widget == null)) {
085: throw new NoSuchElementException();
086: }
087: hasElement = false;
088: return (returned = widget);
089: }
090:
091: public void remove() {
092: if (returned != null) {
093: SimplePanel.this .remove(returned);
094: }
095: }
096: };
097: }
098:
099: @Override
100: public boolean remove(Widget w) {
101: // Validate.
102: if (widget != w) {
103: return false;
104: }
105:
106: // Orphan.
107: orphan(w);
108:
109: // Physical detach.
110: DOM.removeChild(getContainerElement(), w.getElement());
111:
112: // Logical detach.
113: widget = null;
114: return true;
115: }
116:
117: /**
118: * Sets this panel's widget. Any existing child widget will be removed.
119: *
120: * @param w the panel's new widget, or <code>null</code> to clear the panel
121: */
122: public void setWidget(Widget w) {
123: // Validate
124: if (w == widget) {
125: return;
126: }
127:
128: // Detach new child.
129: if (w != null) {
130: w.removeFromParent();
131: }
132:
133: // Remove old child.
134: if (widget != null) {
135: remove(widget);
136: }
137:
138: // Logical attach.
139: widget = w;
140:
141: if (w != null) {
142: // Physical attach.
143: DOM.appendChild(getContainerElement(), widget.getElement());
144:
145: adopt(w);
146: }
147: }
148:
149: /**
150: * Override this method to specify that an element other than the root element
151: * be the container for the panel's child widget. This can be useful when you
152: * want to create a simple panel that decorates its contents.
153: *
154: * @return the element to be used as the panel's container
155: */
156: protected Element getContainerElement() {
157: return getElement();
158: }
159: }
|