KickJava   Java API By Example, From Geeks To Geeks.

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


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;
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
23 /**
24  * A simplified, browser-safe timer class. This class serves the same purpose as
25  * java.util.Timer, but is simplified because of the single-threaded
26  * environment.
27  *
28  * To schedule a timer, simply create a subclass of it (overriding {@link #run})
29  * and call {@link #schedule} or {@link #scheduleRepeating}.
30  *
31  * <p>
32  * <h3>Example</h3>
33  * {@example com.google.gwt.examples.TimerExample}
34  * </p>
35  */

36 public abstract class Timer {
37
38   private static ArrayList JavaDoc timers = new ArrayList JavaDoc();
39
40   static {
41     hookWindowClosing();
42   }
43
44   private static native void clearInterval(int id) /*-{
45     $wnd.clearInterval(id);
46   }-*/
;
47
48   private static native void clearTimeout(int id) /*-{
49     $wnd.clearTimeout(id);
50   }-*/
;
51
52   private static native int createInterval(Timer timer, int period) /*-{
53     return $wnd.setInterval(
54       function() { timer.@com.google.gwt.user.client.Timer::fire()(); },
55       period);
56   }-*/
;
57
58   private static native int createTimeout(Timer timer, int delay) /*-{
59     return $wnd.setTimeout(
60       function() { timer.@com.google.gwt.user.client.Timer::fire()(); },
61       delay);
62   }-*/
;
63
64   private static void hookWindowClosing() {
65     // Catch the window closing event.
66
Window.addWindowCloseListener(new WindowCloseListener() {
67       public void onWindowClosed() {
68         // When the window is closing, cancel all extant timers. This ensures
69
// that no leftover timers can cause memory leaks by leaving links from
70
// the window's timeout closures back into Java objects.
71
while (timers.size() > 0) {
72           ((Timer) timers.get(0)).cancel();
73         }
74       }
75
76       public String JavaDoc onWindowClosing() {
77         return null;
78       }
79     });
80   }
81
82   private boolean isRepeating;
83
84   private int timerId;
85
86   /**
87    * Cancels this timer.
88    */

89   public void cancel() {
90     if (isRepeating) {
91       clearInterval(timerId);
92     } else {
93       clearTimeout(timerId);
94     }
95     timers.remove(this);
96   }
97
98   /**
99    * This method will be called when a timer fires. Override it to implement the
100    * timer's logic.
101    */

102   public abstract void run();
103
104   /**
105    * Schedules a timer to elapse in the future.
106    *
107    * @param delayMillis how long to wait before the timer elapses, in
108    * milliseconds
109    */

110   public void schedule(int delayMillis) {
111     if (delayMillis <= 0) {
112       throw new IllegalArgumentException JavaDoc("must be positive");
113     }
114     cancel();
115     isRepeating = false;
116     timerId = createTimeout(this, delayMillis);
117     timers.add(this);
118   }
119
120   /**
121    * Schedules a timer that elapses repeatedly.
122    *
123    * @param periodMillis how long to wait before the timer elapses, in
124    * milliseconds, between each repetition
125    */

126   public void scheduleRepeating(int periodMillis) {
127     if (periodMillis <= 0) {
128       throw new IllegalArgumentException JavaDoc("must be positive");
129     }
130     cancel();
131     isRepeating = true;
132     timerId = createInterval(this, periodMillis);
133     timers.add(this);
134   }
135
136   /*
137    * Called by native code when this timer fires.
138    */

139   final void fire() {
140     UncaughtExceptionHandler handler = GWT.getUncaughtExceptionHandler();
141     if (handler != null) {
142       fireAndCatch(handler);
143     } else {
144       fireImpl();
145     }
146   }
147
148   private void fireAndCatch(UncaughtExceptionHandler handler) {
149     try {
150       fireImpl();
151     } catch (Throwable JavaDoc e) {
152       handler.onUncaughtException(e);
153     }
154   }
155
156   private void fireImpl() {
157     // If this is a one-shot timer, remove it from the timer list. This will
158
// allow it to be garbage collected.
159
if (!isRepeating) {
160       timers.remove(this);
161     }
162
163     // Run the timer's code.
164
run();
165   }
166 }
167
Popular Tags