KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > Window


1 /*
2  * Copyright 2007 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;
17
18 import com.google.gwt.core.client.GWT;
19 import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * This class provides access to the browser window's methods, properties, and
26  * events.
27  */

28 public class Window {
29
30   private static ArrayList JavaDoc closingListeners = new ArrayList JavaDoc();
31   private static ArrayList JavaDoc resizeListeners = new ArrayList JavaDoc();
32
33   static {
34     init();
35   }
36
37   /**
38    * Adds a listener to receive window closing events.
39    *
40    * @param listener the listener to be informed when the window is closing
41    */

42   public static void addWindowCloseListener(WindowCloseListener listener) {
43     closingListeners.add(listener);
44   }
45
46   /**
47    * Adds a listener to receive window resize events.
48    *
49    * @param listener the listener to be informed when the window is resized
50    */

51   public static void addWindowResizeListener(WindowResizeListener listener) {
52     resizeListeners.add(listener);
53   }
54
55   /**
56    * Displays a message in a modal dialog box.
57    *
58    * @param msg the message to be displayed.
59    */

60   public static native void alert(String JavaDoc msg) /*-{
61     $wnd.alert(msg);
62   }-*/
;
63
64   /**
65    * Displays a message in a modal dialog box, along with the standard 'OK' and
66    * 'Cancel' buttons.
67    *
68    * @param msg the message to be displayed.
69    * @return <code>true</code> if 'OK' is clicked, <code>false</code> if
70    * 'Cancel' is clicked.
71    */

72   public static native boolean confirm(String JavaDoc msg) /*-{
73     return $wnd.confirm(msg);
74   }-*/
;
75
76   /**
77    * Use this method to explicitly disable the window's scrollbars. Applications
78    * that choose to resize their user-interfaces to fit within the window's
79    * client area will normally want to disable window scrolling.
80    *
81    * @param enable <code>false</code> to disable window scrolling
82    */

83   public static native void enableScrolling(boolean enable) /*-{
84     $doc.body.style.overflow = enable ? 'auto' : 'hidden';
85   }-*/
;
86
87   /**
88    * Gets the height of the browser window's client area.
89    *
90    * @return the window's client height
91    */

92   public static native int getClientHeight() /*-{
93     // Non-IE browsers || IE standard mode || IE quirks mode.
94     return $wnd.innerHeight || $doc.documentElement.clientHeight ||
95         $doc.body.clientHeight;
96   }-*/
;
97
98   /**
99    * Gets the width of the browser window's client area.
100    *
101    * @return the window's client width
102    */

103   public static native int getClientWidth() /*-{
104     // Non-IE browsers || IE standard mode || IE quirks mode.
105     return $wnd.innerWidth || $doc.documentElement.clientWidth || $doc.body.clientWidth;
106   }-*/
;
107
108   /**
109    * Gets the window's scroll left.
110    *
111    * @return window's scroll left
112    */

113   public static native int getScrollLeft() /*-{
114     // Standard mode || Quirks mode.
115     return $doc.documentElement.scrollLeft || $doc.body.scrollLeft
116   }-*/
;
117
118   /**
119    * Get the window's scroll top.
120    *
121    * @return the window's scroll top
122    */

123   public static native int getScrollTop() /*-{
124     // Standard mode || Quirks mode.
125     return $doc.documentElement.scrollTop || $doc.body.scrollTop;
126   }-*/
;
127
128   /**
129    * Gets the browser window's current title.
130    *
131    * @return the window's title.
132    */

133   public static native String JavaDoc getTitle() /*-{
134     return $doc.title;
135   }-*/
;
136
137   /**
138    * Opens a new browser window. The "name" and "features" arguments are
139    * specified <a HREF=
140    * 'http://www.mozilla.org/docs/dom/domref/dom_window_ref76.html'>here</a>.
141    *
142    * @param url the URL that the new window will display
143    * @param name the name of the window (e.g. "_blank")
144    * @param features the features to be enabled/disabled on this window
145    */

146   public static native void open(String JavaDoc url, String JavaDoc name, String JavaDoc features) /*-{
147     $wnd.open(url, name, features);
148   }-*/
;
149
150   /**
151    * Displays a request for information in a modal dialog box, along with the
152    * standard 'OK' and 'Cancel' buttons.
153    *
154    * @param msg the message to be displayed
155    * @param initialValue the initial value in the dialog's text field
156    * @return the value entered by the user if 'OK' was pressed, or
157    * <code>null</code> if 'Cancel' was pressed
158    */

159   public static native String JavaDoc prompt(String JavaDoc msg, String JavaDoc initialValue) /*-{
160     return $wnd.prompt(msg, initialValue);
161   }-*/
;
162
163   /**
164    * Removes a window closing listener.
165    *
166    * @param listener the listener to be removed
167    */

168   public static void removeWindowCloseListener(WindowCloseListener listener) {
169     closingListeners.remove(listener);
170   }
171
172   /**
173    * Removes a window resize listener.
174    *
175    * @param listener the listener to be removed
176    */

177   public static void removeWindowResizeListener(WindowResizeListener listener) {
178     resizeListeners.remove(listener);
179   }
180
181   /**
182    * Sets the size of the margins used within the window's client area. It is
183    * sometimes necessary to do this because some browsers, such as Internet
184    * Explorer, add margins by default, which can confound attempts to resize
185    * panels to fit exactly within the window.
186    *
187    * @param size the window's new margin size, in CSS units.
188    */

189   public static native void setMargin(String JavaDoc size) /*-{
190     $doc.body.style.margin = size;
191   }-*/
;
192
193   /**
194    * Sets the browser window's title.
195    *
196    * @param title the new window title.
197    */

198   public static native void setTitle(String JavaDoc title) /*-{
199     $doc.title = title;
200   }-*/
;
201
202   static void onClosed() {
203     UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
204     if (handler != null) {
205       fireClosedAndCatch(handler);
206     } else {
207       fireClosedImpl();
208     }
209   }
210
211   static String JavaDoc onClosing() {
212     UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
213     if (handler != null) {
214       return fireClosingAndCatch(handler);
215     } else {
216       return fireClosingImpl();
217     }
218   }
219
220   static void onResize() {
221     UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
222     if (handler != null) {
223       fireResizedAndCatch(handler);
224     } else {
225       fireResizedImpl();
226     }
227   }
228
229   private static void fireClosedAndCatch(UncaughtExceptionHandler handler) {
230     try {
231       fireClosedImpl();
232     } catch (Throwable JavaDoc e) {
233       handler.onUncaughtException(e);
234     }
235   }
236
237   private static void fireClosedImpl() {
238     for (Iterator JavaDoc it = closingListeners.iterator(); it.hasNext();) {
239       WindowCloseListener listener = (WindowCloseListener) it.next();
240       listener.onWindowClosed();
241     }
242   }
243
244   private static String JavaDoc fireClosingAndCatch(UncaughtExceptionHandler handler) {
245     try {
246       return fireClosingImpl();
247     } catch (Throwable JavaDoc e) {
248       handler.onUncaughtException(e);
249       return null;
250     }
251   }
252
253   private static String JavaDoc fireClosingImpl() {
254     String JavaDoc ret = null;
255     for (Iterator JavaDoc it = closingListeners.iterator(); it.hasNext();) {
256       WindowCloseListener listener = (WindowCloseListener) it.next();
257
258       // If any listener wants to suppress the window closing event, then do so.
259
String JavaDoc msg = listener.onWindowClosing();
260       if (ret == null) {
261         ret = msg;
262       }
263     }
264
265     return ret;
266   }
267
268   private static void fireResizedAndCatch(UncaughtExceptionHandler handler) {
269     try {
270       fireResizedImpl();
271     } catch (Throwable JavaDoc e) {
272       handler.onUncaughtException(e);
273     }
274   }
275
276   private static void fireResizedImpl() {
277     for (Iterator JavaDoc it = resizeListeners.iterator(); it.hasNext();) {
278       WindowResizeListener listener = (WindowResizeListener) it.next();
279       listener.onWindowResized(getClientWidth(), getClientHeight());
280     }
281   }
282
283   private static native void init() /*-{
284     // Magic function defined by the selection script.
285     __gwt_initHandlers(
286       function() {
287         @com.google.gwt.user.client.Window::onResize()();
288       },
289       function() {
290         return @com.google.gwt.user.client.Window::onClosing()();
291       },
292       function() {
293         @com.google.gwt.user.client.Window::onClosed()();
294         $wnd.onresize = null;
295         $wnd.onbeforeclose = null;
296         $wnd.onclose = null;
297       }
298     );
299   }-*/
;
300
301   private Window() {
302   }
303 }
304
Popular Tags