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 import com.google.gwt.user.client.Window; 21 import com.google.gwt.user.client.WindowCloseListener; 22 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 26 35 public class RootPanel extends AbsolutePanel { 36 37 private static HashMap rootPanels = new HashMap (); 38 39 47 public static RootPanel get() { 48 return get(null); 49 } 50 51 59 public static RootPanel get(String id) { 60 RootPanel gwt = (RootPanel) rootPanels.get(id); 62 if (gwt != null) { 63 return gwt; 64 } 65 Element elem = null; 67 if (id != null) { 68 if (null == (elem = DOM.getElementById(id))) { 69 return null; 70 } 71 } 72 73 if (rootPanels.size() == 0) { 74 hookWindowClosing(); 75 } 76 77 rootPanels.put(id, gwt = new RootPanel(elem)); 79 return gwt; 80 } 81 82 87 public static native Element getBodyElement() ; 90 91 private static void hookWindowClosing() { 92 Window.addWindowCloseListener(new WindowCloseListener() { 94 public void onWindowClosed() { 95 for (Iterator it = rootPanels.values().iterator(); it.hasNext();) { 99 RootPanel gwt = (RootPanel) it.next(); 100 if (gwt.isAttached()) { 101 gwt.onDetach(); 102 } 103 } 104 } 105 106 public String onWindowClosing() { 107 return null; 108 } 109 }); 110 } 111 112 private RootPanel(Element elem) { 113 if (elem == null) { 114 elem = getBodyElement(); 116 } 117 118 setElement(elem); 119 onAttach(); 120 } 121 } 122 | Popular Tags |