1 16 package com.google.gwt.user.client.ui; 17 18 import com.google.gwt.core.client.GWT; 19 import com.google.gwt.user.client.DOM; 20 import com.google.gwt.user.client.Event; 21 import com.google.gwt.user.client.EventListener; 22 import com.google.gwt.user.client.Element; 23 24 29 public class Widget extends UIObject implements EventListener { 30 31 private boolean attached; 32 private Object layoutData; 33 private Widget parent; 34 35 40 public Widget getParent() { 41 return parent; 42 } 43 44 51 public boolean isAttached() { 52 return attached; 53 } 54 55 public void onBrowserEvent(Event event) { 56 } 57 58 65 public void removeFromParent() { 66 if (parent instanceof HasWidgets) { 67 ((HasWidgets) parent).remove(this); 68 } else if (parent != null) { 69 throw new IllegalStateException ( 70 "This widget's parent does not implement HasWidgets"); 71 } 72 } 73 74 87 protected void onAttach() { 88 if (attached) { 89 throw new IllegalStateException ( 90 "Should only call onAttach when the widget is detached from the browser's document"); 91 } 92 93 attached = true; 94 95 DOM.setEventListener(getElement(), this); 99 100 onLoad(); 102 } 103 104 119 protected void onDetach() { 120 if (!attached) { 121 throw new IllegalStateException ( 122 "Should only call onDetach when the widget is attached to the browser's document"); 123 } 124 125 try { 127 onUnload(); 128 } finally { 129 attached = false; 130 131 DOM.setEventListener(getElement(), null); 134 } 135 } 136 137 141 protected void onLoad() { 142 } 143 144 148 protected void onUnload() { 149 } 150 151 161 protected void setElement(Element elem) { 162 if (attached) { 163 DOM.setEventListener(getElement(), null); 167 } 168 169 super.setElement(elem); 170 if (attached) { 171 DOM.setEventListener(elem, this); 175 } 176 } 177 178 184 Object getLayoutData() { 185 return layoutData; 186 } 187 188 196 void setLayoutData(Object layoutData) { 197 this.layoutData = layoutData; 198 } 199 200 206 void setParent(Widget parent) { 207 Widget oldParent = this.parent; 208 this.parent = parent; 209 if (parent == null) { 210 if (oldParent != null && oldParent.isAttached()) { 211 onDetach(); 212 assert !isAttached() : 213 "Failure of " + GWT.getTypeName(this) + " to call super.onDetach()"; 214 } 215 } else if (parent.isAttached()) { 216 onAttach(); 217 assert isAttached() : 218 "Failure of " + GWT.getTypeName(this) + " to call super.onAttach()"; 219 } 220 } 221 } 222 | Popular Tags |