1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | // Original copyright is held by Nathan Arthur |
16 | // [Java Specialists' Newsletter, Issue #75, 2003-07-29] |
17 | |
18 | package net.sourceforge.hivegui.autowait; |
19 | |
20 | import org.apache.commons.logging.Log; |
21 | import org.apache.commons.logging.LogFactory; |
22 | |
23 | /** |
24 | * This class implements a delay timer that will call trigger() |
25 | * on the DelayTimerCallback delay milliseconds after |
26 | * startTimer() was called, if stopTimer() was not called first. |
27 | * The timer will only throw events after startTimer() is called. |
28 | * Until then, it does nothing. It is safe to call stopTimer() |
29 | * and startTimer() repeatedly. |
30 | * Note that calls to trigger() will happen on the timer thread. |
31 | * This class is multiple-thread safe. |
32 | * |
33 | * @author Nathan Arthur |
34 | */ |
35 | public class DelayTimer extends Thread |
36 | { |
37 | static final private Log _logger = LogFactory.getLog(DelayTimer.class); |
38 | |
39 | public DelayTimer(DelayTimerCallback callback, long delay) |
40 | { |
41 | _callback = callback; |
42 | _delay = delay; |
43 | setDaemon(true); |
44 | start(); |
45 | } |
46 | |
47 | /** |
48 | * Calling this method twice will reset the timer. |
49 | */ |
50 | public void startTimer() |
51 | { |
52 | synchronized (_mutex) |
53 | { |
54 | _waitTime = _delay; |
55 | _mutex.notify(); |
56 | } |
57 | } |
58 | |
59 | public void stopTimer() |
60 | { |
61 | try |
62 | { |
63 | synchronized (_mutex) |
64 | { |
65 | synchronized (_triggeredMutex) |
66 | { |
67 | if (_triggered) |
68 | { |
69 | _triggeredMutex.wait(); |
70 | } |
71 | } |
72 | _waitTime = 0; |
73 | _mutex.notify(); |
74 | } |
75 | } |
76 | catch (InterruptedException e) |
77 | { |
78 | _logger.error("stopTimer()", e); |
79 | } |
80 | } |
81 | |
82 | @Override public void run() |
83 | { |
84 | try |
85 | { |
86 | while (!_quit) |
87 | { |
88 | synchronized (_mutex) |
89 | { |
90 | // We rely on wait(0) being implemented to wait forever here |
91 | if (_waitTime < 0) |
92 | { |
93 | _triggered = true; |
94 | _waitTime = 0; |
95 | } |
96 | else |
97 | { |
98 | long saveWaitTime = _waitTime; |
99 | _waitTime = -1; |
100 | _mutex.wait(saveWaitTime); |
101 | } |
102 | } |
103 | //CSOFF: IllegalCatchCheck |
104 | try |
105 | { |
106 | if (_triggered) |
107 | { |
108 | _callback.trigger(); |
109 | } |
110 | } |
111 | catch (RuntimeException e) |
112 | { |
113 | _logger.warn("run()", e); |
114 | } |
115 | finally |
116 | { |
117 | synchronized (_triggeredMutex) |
118 | { |
119 | _triggered = false; |
120 | _triggeredMutex.notify(); |
121 | } |
122 | } |
123 | //CSON: IllegalCatchCheck |
124 | } |
125 | } |
126 | catch (InterruptedException e) |
127 | { |
128 | _logger.error("run()", e); |
129 | } |
130 | } |
131 | |
132 | public void quit() |
133 | { |
134 | synchronized (_mutex) |
135 | { |
136 | _quit = true; |
137 | _mutex.notify(); |
138 | } |
139 | } |
140 | |
141 | private final DelayTimerCallback _callback; |
142 | private final Object _mutex = new Object(); |
143 | private final Object _triggeredMutex = new Object(); |
144 | private final long _delay; |
145 | private boolean _quit; |
146 | private boolean _triggered; |
147 | private long _waitTime; |
148 | } |