KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > swing > cursor > DelayTimer


1 package net.sf.panoptes.swing.cursor;
2
3 /**
4  * This class implements a delay timer that will call trigger()
5  * on the DelayTimerCallback delay milliseconds after
6  * startTimer() was called, if stopTimer() was not called first.
7  * The timer will only throw events after startTimer() is called.
8  * Until then, it does nothing. It is safe to call stopTimer()
9  * and startTimer() repeatedly.
10  *
11  * Note that calls to trigger() will happen on the timer thread.
12  *
13  * This class is multiple-thread safe.
14  */

15 public class DelayTimer extends Thread JavaDoc {
16   private final DelayTimerCallback callback;
17   private final Object JavaDoc mutex = new Object JavaDoc();
18   private final Object JavaDoc triggeredMutex = new Object JavaDoc();
19   private final long delay;
20   private boolean quit;
21   private boolean triggered;
22   private long waitTime;
23
24   public DelayTimer(DelayTimerCallback callback, long delay) {
25     this.callback = callback;
26     this.delay = delay;
27     setDaemon(true);
28     start();
29   }
30   /**
31    * Calling this method twice will reset the timer.
32    */

33   public void startTimer() {
34     synchronized (mutex) {
35       waitTime = delay;
36       mutex.notify();
37     }
38   }
39   public void stopTimer() {
40     try {
41       synchronized (mutex) {
42         synchronized (triggeredMutex) {
43           if (triggered) {
44             triggeredMutex.wait();
45           }
46         }
47         waitTime = 0;
48         mutex.notify();
49       }
50     } catch (InterruptedException JavaDoc ie) {
51       System.err.println("trigger failure");
52       ie.printStackTrace(System.err);
53     }
54   }
55   public void run() {
56     try {
57       while (!quit) {
58         synchronized (mutex) {
59           //we rely on wait(0) being implemented to wait forever here
60
if (waitTime < 0) {
61             triggered = true;
62             waitTime = 0;
63           } else {
64             long saveWaitTime = waitTime;
65             waitTime = -1;
66             mutex.wait(saveWaitTime);
67           }
68         }
69         try {
70           if (triggered) {
71             callback.trigger();
72           }
73         } catch (Exception JavaDoc e) {
74           System.err.println(
75               "trigger() threw exception, continuing");
76           e.printStackTrace(System.err);
77         } finally {
78           synchronized (triggeredMutex) {
79             triggered = false;
80             triggeredMutex.notify();
81           }
82         }
83       }
84     } catch (InterruptedException JavaDoc ie) {
85       System.err.println("interrupted in run");
86       ie.printStackTrace(System.err);
87     }
88   }
89   public void quit() {
90     synchronized (mutex) {
91       this.quit = true;
92       mutex.notify();
93     }
94   }
95 }
Popular Tags