KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.NoSuchElementException JavaDoc;
23
24 /**
25  * Abstract base class for panels that contain only one widget.
26  */

27 public class SimplePanel extends Panel {
28
29   private Widget widget;
30
31   /**
32    * Creates an empty panel that uses a DIV for its contents.
33    */

34   public SimplePanel() {
35     this(DOM.createDiv());
36   }
37
38   /**
39    * Creates an empty panel that uses the specified browser element for its
40    * contents.
41    *
42    * @param elem the browser element to use
43    */

44   protected SimplePanel(Element elem) {
45     setElement(elem);
46   }
47
48   /**
49    * Adds a widget to this panel.
50    *
51    * @param w the child widget to be added
52    */

53   public void add(Widget w) {
54     // Can't add() more than one widget to a SimplePanel.
55
if (getWidget() != null) {
56       throw new IllegalStateException JavaDoc("SimplePanel can only contain one child widget");
57     }
58     setWidget(w);
59   }
60
61   /**
62    * Gets the panel's child widget.
63    *
64    * @return the child widget, or <code>null</code> if none is present
65    */

66   public Widget getWidget() {
67     return widget;
68   }
69
70   public Iterator JavaDoc iterator() {
71     // Return a simple iterator that enumerates the 0 or 1 elements in this
72
// panel.
73
return new Iterator JavaDoc() {
74       boolean hasElement = widget != null;
75       Widget returned = null;
76
77       public boolean hasNext() {
78         return hasElement;
79       }
80
81       public Object JavaDoc next() {
82         if (!hasElement || (widget == null)) {
83           throw new NoSuchElementException JavaDoc();
84         }
85         hasElement = false;
86         return (returned = widget);
87       }
88
89       public void remove() {
90         if (returned != null) {
91           SimplePanel.this.remove(returned);
92         }
93       }
94     };
95   }
96
97   public boolean remove(Widget w) {
98     if (widget == w) {
99       disown(w);
100       widget = null;
101       return true;
102     }
103     return false;
104   }
105
106   /**
107    * Sets this panel's widget. Any existing child widget will be removed.
108    *
109    * @param w the panel's new widget (<code>null</code> will clear the panel)
110    */

111   public void setWidget(Widget w) {
112     // If there is already a widget attached, remove it.
113
if (widget != null) {
114       disown(widget);
115     }
116
117     if (w != null) {
118       // Adopt the child.
119
adopt(w, getContainerElement());
120     }
121
122     widget = w;
123   }
124
125   /**
126    * Override this method to specify that an element other than the root element
127    * be the container for the panel's child widget. This can be useful when you
128    * want to create a simple panel that decorates its contents.
129    *
130    * @return the element to be used as the panel's container
131    */

132   protected Element getContainerElement() {
133     return getElement();
134   }
135 }
136
Popular Tags