KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.google.gwt.user.client.Window;
21 import com.google.gwt.user.client.WindowCloseListener;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25
26 /**
27  * The panel to which all other widgets must ultimately be added. RootPanels are
28  * never created directly. Rather, they are accessed via {@link RootPanel#get()}.
29  *
30  * <p>
31  * Most applications will add widgets to the default root panel in their
32  * {@link com.google.gwt.core.client.EntryPoint#onModuleLoad} methods.
33  * </p>
34  */

35 public class RootPanel extends AbsolutePanel {
36
37   private static HashMap JavaDoc rootPanels = new HashMap JavaDoc();
38
39   /**
40    * Gets the default root panel. This panel wraps body of the browser's
41    * document. This root panel can contain any number of widgets, which will be
42    * laid out in their natural HTML ordering. Many applications, however, will
43    * add a single panel to the RootPanel to provide more structure.
44    *
45    * @return the default RootPanel
46    */

47   public static RootPanel get() {
48     return get(null);
49   }
50
51   /**
52    * Gets the root panel associated with a given browser element. For this to
53    * work, the HTML document into which the application is loaded must have
54    * specified an element with the given id.
55    *
56    * @param id the id of the element to be wrapped with a root panel
57    * @return the root panel, or <code>null</code> if no such element was found
58    */

59   public static RootPanel get(String JavaDoc id) {
60     // See if this RootPanel is already created.
61
RootPanel gwt = (RootPanel) rootPanels.get(id);
62     if (gwt != null) {
63       return gwt;
64     }
65     // Find the element that this RootPanel will wrap.
66
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     // Create the panel and put it in the map.
78
rootPanels.put(id, gwt = new RootPanel(elem));
79     return gwt;
80   }
81
82   /**
83    * Convenience method for getting the document's body element.
84    *
85    * @return the document's body element
86    */

87   public static native Element getBodyElement() /*-{
88     return $doc.body;
89   }-*/
;
90
91   private static void hookWindowClosing() {
92     // Catch the window closing event.
93
Window.addWindowCloseListener(new WindowCloseListener() {
94       public void onWindowClosed() {
95         // When the window is closing, detach all root panels. This will cause
96
// all of their children's event listeners to be unhooked, which will
97
// avoid potential memory leaks.
98
for (Iterator JavaDoc 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 JavaDoc onWindowClosing() {
107         return null;
108       }
109     });
110   }
111
112   private RootPanel(Element elem) {
113     if (elem == null) {
114       // 'null' means use document's body element.
115
elem = getBodyElement();
116     }
117
118     setElement(elem);
119     onAttach();
120   }
121 }
122
Popular Tags