1 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 ; 22 import java.util.NoSuchElementException ; 23 24 27 public class SimplePanel extends Panel { 28 29 private Widget widget; 30 31 34 public SimplePanel() { 35 this(DOM.createDiv()); 36 } 37 38 44 protected SimplePanel(Element elem) { 45 setElement(elem); 46 } 47 48 53 public void add(Widget w) { 54 if (getWidget() != null) { 56 throw new IllegalStateException ("SimplePanel can only contain one child widget"); 57 } 58 setWidget(w); 59 } 60 61 66 public Widget getWidget() { 67 return widget; 68 } 69 70 public Iterator iterator() { 71 return new Iterator () { 74 boolean hasElement = widget != null; 75 Widget returned = null; 76 77 public boolean hasNext() { 78 return hasElement; 79 } 80 81 public Object next() { 82 if (!hasElement || (widget == null)) { 83 throw new NoSuchElementException (); 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 111 public void setWidget(Widget w) { 112 if (widget != null) { 114 disown(widget); 115 } 116 117 if (w != null) { 118 adopt(w, getContainerElement()); 120 } 121 122 widget = w; 123 } 124 125 132 protected Element getContainerElement() { 133 return getElement(); 134 } 135 } 136 | Popular Tags |