01: /*
02: * MyGWT Widget Library
03: * Copyright(c) 2007, MyGWT.
04: * licensing@mygwt.net
05: *
06: * http://mygwt.net/license
07: */
08: package net.mygwt.ui.client.util;
09:
10: import net.mygwt.ui.client.event.Listener;
11:
12: import com.google.gwt.user.client.Timer;
13:
14: /**
15: * A <code>Timer</code> that is cancelled if a new request is made.
16: */
17: public class DelayedTask {
18:
19: private Timer timer;
20: private Listener listener;
21:
22: /**
23: * Creates a new delayed task.
24: *
25: * @param listener the listener to be called
26: */
27: public DelayedTask(final Listener listener) {
28: this .listener = listener;
29: }
30:
31: /**
32: * Cancels the task.
33: */
34: public void cancel() {
35: if (timer != null) {
36: timer.cancel();
37: }
38: }
39:
40: /**
41: * Cancels any running timers and starts a new one.
42: *
43: * @param delay the delay in ms
44: */
45: public void delay(int delay) {
46: if (timer != null) {
47: timer.cancel();
48: timer.schedule(delay);
49: } else {
50: timer = new Timer() {
51: public void run() {
52: timer = null;
53: listener.handleEvent(null);
54: }
55: };
56: timer.schedule(delay);
57: }
58: }
59:
60: }
|