2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
package javax.swing;
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
import java.util.*;
|
|
33 |
import java.util.concurrent.atomic.AtomicBoolean;
|
|
34 |
import java.util.concurrent.locks.*;
|
|
35 |
import java.awt.*;
|
|
36 |
import java.awt.event.*;
|
|
37 |
import java.io.Serializable;
|
|
38 |
import javax.swing.event.EventListenerList;
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
/**
|
|
43 |
* Fires one or more {@code ActionEvent}s at specified
|
|
44 |
* intervals. An example use is an animation object that uses a
|
|
45 |
* <code>Timer</code> as the trigger for drawing its frames.
|
|
46 |
*<p>
|
|
47 |
* Setting up a timer
|
|
48 |
* involves creating a <code>Timer</code> object,
|
|
49 |
* registering one or more action listeners on it,
|
|
50 |
* and starting the timer using
|
|
51 |
* the <code>start</code> method.
|
|
52 |
* For example,
|
|
53 |
* the following code creates and starts a timer
|
|
54 |
* that fires an action event once per second
|
|
55 |
* (as specified by the first argument to the <code>Timer</code> constructor).
|
|
56 |
* The second argument to the <code>Timer</code> constructor
|
|
57 |
* specifies a listener to receive the timer's action events.
|
|
58 |
*
|
|
59 |
*<pre>
|
|
60 |
* int delay = 1000; //milliseconds
|
|
61 |
* ActionListener taskPerformer = new ActionListener() {
|
|
62 |
* public void actionPerformed(ActionEvent evt) {
|
|
63 |
* <em>//...Perform a task...</em>
|
|
64 |
* }
|
|
65 |
* };
|
|
66 |
* new Timer(delay, taskPerformer).start();</pre>
|
|
67 |
*
|
|
68 |
* <p>
|
|
69 |
* {@code Timers} are constructed by specifying both a delay parameter
|
|
70 |
* and an {@code ActionListener}. The delay parameter is used
|
|
71 |
* to set both the initial delay and the delay between event
|
|
72 |
* firing, in milliseconds. Once the timer has been started,
|
|
73 |
* it waits for the initial delay before firing its
|
|
74 |
* first <code>ActionEvent</code> to registered listeners.
|
|
75 |
* After this first event, it continues to fire events
|
|
76 |
* every time the between-event delay has elapsed, until it
|
|
77 |
* is stopped.
|
|
78 |
* <p>
|
|
79 |
* After construction, the initial delay and the between-event
|
|
80 |
* delay can be changed independently, and additional
|
|
81 |
* <code>ActionListeners</code> may be added.
|
|
82 |
* <p>
|
|
83 |
* If you want the timer to fire only the first time and then stop,
|
|
84 |
* invoke <code>setRepeats(false)</code> on the timer.
|
|
85 |
* <p>
|
|
86 |
* Although all <code>Timer</code>s perform their waiting
|
|
87 |
* using a single, shared thread
|
|
88 |
* (created by the first <code>Timer</code> object that executes),
|
|
89 |
* the action event handlers for <code>Timer</code>s
|
|
90 |
* execute on another thread -- the event-dispatching thread.
|
|
91 |
* This means that the action handlers for <code>Timer</code>s
|
|
92 |
* can safely perform operations on Swing components.
|
|
93 |
* However, it also means that the handlers must execute quickly
|
|
94 |
* to keep the GUI responsive.
|
|
95 |
*
|
|
96 |
* <p>
|
|
97 |
* In v 1.3, another <code>Timer</code> class was added
|
|
98 |
* to the Java platform: <code>java.util.Timer</code>.
|
|
99 |
* Both it and <code>javax.swing.Timer</code>
|
|
100 |
* provide the same basic functionality,
|
|
101 |
* but <code>java.util.Timer</code>
|
|
102 |
* is more general and has more features.
|
|
103 |
* The <code>javax.swing.Timer</code> has two features
|
|
104 |
* that can make it a little easier to use with GUIs.
|
|
105 |
* First, its event handling metaphor is familiar to GUI programmers
|
|
106 |
* and can make dealing with the event-dispatching thread
|
|
107 |
* a bit simpler.
|
|
108 |
* Second, its
|
|
109 |
* automatic thread sharing means that you don't have to
|
|
110 |
* take special steps to avoid spawning
|
|
111 |
* too many threads.
|
|
112 |
* Instead, your timer uses the same thread
|
|
113 |
* used to make cursors blink,
|
|
114 |
* tool tips appear,
|
|
115 |
* and so on.
|
|
116 |
*
|
|
117 |
* <p>
|
|
118 |
* You can find further documentation
|
|
119 |
* and several examples of using timers by visiting
|
|
120 |
* <a href="http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html"
|
|
121 |
* target = "_top">How to Use Timers</a>,
|
|
122 |
* a section in <em>The Java Tutorial.</em>
|
|
123 |
* For more examples and help in choosing between
|
|
124 |
* this <code>Timer</code> class and
|
|
125 |
* <code>java.util.Timer</code>,
|
|
126 |
* see
|
|
127 |
* <a href="http://java.sun.com/products/jfc/tsc/articles/timer/"
|
|
128 |
* target="_top">Using Timers in Swing Applications</a>,
|
|
129 |
* an article in <em>The Swing Connection.</em>
|
|
130 |
* <p>
|
|
131 |
* <strong>Warning:</strong>
|
|
132 |
* Serialized objects of this class will not be compatible with
|
|
133 |
* future Swing releases. The current serialization support is
|
|
134 |
* appropriate for short term storage or RMI between applications running
|
|
135 |
* the same version of Swing. As of 1.4, support for long term storage
|
|
136 |
* of all JavaBeans<sup><font size="-2">TM</font></sup>
|
|
137 |
* has been added to the <code>java.beans</code> package.
|
|
138 |
* Please see {@link java.beans.XMLEncoder}.
|
|
139 |
*
|
|
140 |
* @see java.util.Timer <code>java.util.Timer</code>
|
|
141 |
*
|
|
142 |
*
|
|
143 |
* @author Dave Moore
|
|
144 |
*/
|
|
145 |
public class Timer implements Serializable
|
|
146 |
{
|
|
147 |
/*
|
|
148 |
* NOTE: all fields need to be handled in readResolve
|
|
149 |
*/
|
|
150 |
|
|
151 |
protected EventListenerList listenerList = new EventListenerList();
|
|
152 |
|
|
153 |
// The following field strives to maintain the following:
|
|
154 |
// If coalesce is true, only allow one Runnable to be queued on the
|
|
155 |
// EventQueue and be pending (ie in the process of notifying the
|
|
156 |
// ActionListener). If we didn't do this it would allow for a
|
|
157 |
// situation where the app is taking too long to process the
|
|
158 |
// actionPerformed, and thus we'ld end up queing a bunch of Runnables
|
|
159 |
// and the app would never return: not good. This of course implies
|
|
160 |
// you can get dropped events, but such is life.
|
|
161 |
// notify is used to indicate if the ActionListener can be notified, when
|
|
162 |
// the Runnable is processed if this is true it will notify the listeners.
|
|
163 |
// notify is set to true when the Timer fires and the Runnable is queued.
|
|
164 |
// It will be set to false after notifying the listeners (if coalesce is
|
|
165 |
// true) or if the developer invokes stop.
|
|
166 |
private transient final AtomicBoolean notify = new AtomicBoolean(false);
|
|
167 |
|
|
168 |
private volatile int initialDelay, delay;
|
|
169 |
private volatile boolean repeats = true, coalesce = true;
|
|
170 |
|
|
171 |
private transient final Runnable doPostEvent;
|
|
172 |
|
|
173 |
private static volatile boolean logTimers;
|
|
174 |
|
|
175 |
private transient final Lock lock = new ReentrantLock();
|
|
176 |
|
|
177 |
// This field is maintained by TimerQueue.
|
|
178 |
// eventQueued can also be reset by the TimerQueue, but will only ever
|
|
179 |
// happen in applet case when TimerQueues thread is destroyed.
|
|
180 |
// access to this field is synchronized on getLock() lock.
|
|
181 |
transient TimerQueue.DelayedTimer delayedTimer = null;
|
|
182 |
|
|
183 |
private volatile String actionCommand;
|
|
184 |
|
|
185 |
/**
|
|
186 |
* Creates a {@code Timer} and initializes both the initial delay and
|
|
187 |
* between-event delay to {@code delay} milliseconds. If {@code delay}
|
|
188 |
* is less than or equal to zero, the timer fires as soon as it
|
|
189 |
* is started. If <code>listener</code> is not <code>null</code>,
|
|
190 |
* it's registered as an action listener on the timer.
|
|
191 |
*
|
|
192 |
* @param delay milliseconds for the initial and between-event delay
|
|
193 |
* @param listener an initial listener; can be <code>null</code>
|
|
194 |
*
|
|
195 |
* @see #addActionListener
|
|
196 |
* @see #setInitialDelay
|
|
197 |
* @see #setRepeats
|
|
198 |
*/
|
|
199 |
public Timer(int delay, ActionListener listener) {
|
|
200 |
super();
|
|
201 |
this.delay = delay;
|
|
202 |
this.initialDelay = delay;
|
|
203 |
|
|
204 |
doPostEvent = new DoPostEvent();
|
|
205 |
|
|
206 |
if (listener != null) {
|
|
207 |
addActionListener(listener);
|
|
208 |
}
|
|
209 |
}
|
|
210 |
|
|
211 |
|
|
212 |
/**
|
|
213 |
* DoPostEvent is a runnable class that fires actionEvents to
|
|
214 |
* the listeners on the EventDispatchThread, via invokeLater.
|
|
215 |
* @see Timer#post
|
|
216 |
*/
|
|
217 |
class DoPostEvent implements Runnable
|
|
218 |
{
|
|
219 |
public void run() {
|
|
220 |
if (logTimers) {
|
|
221 |
System.out.println("Timer ringing: " + Timer.this);
|
|
222 |
}
|
|
223 |
if(notify.get()) {
|
|
224 |
fireActionPerformed(new ActionEvent(Timer.this, 0, getActionCommand(),
|
|
225 |
System.currentTimeMillis(),
|
|
226 |
0));
|
|
227 |
if (coalesce) {
|
|
228 |
cancelEvent();
|
|
229 |
}
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
Timer getTimer() {
|
|
234 |
return Timer.this;
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
/**
|
|
239 |
* Adds an action listener to the <code>Timer</code>.
|
|
240 |
*
|
|
241 |
* @param listener the listener to add
|
|
242 |
*
|
|
243 |
* @see #Timer
|
|
244 |
*/
|
|
245 |
public void addActionListener(ActionListener listener) {
|
|
246 |
listenerList.add(ActionListener.class, listener);
|
|
247 |
}
|
|
248 |
|
|
249 |
|
|
250 |
/**
|
|
251 |
* Removes the specified action listener from the <code>Timer</code>.
|
|
252 |
*
|
|
253 |
* @param listener the listener to remove
|
|
254 |
*/
|
|
255 |
public void removeActionListener(ActionListener listener) {
|
|
256 |
listenerList.remove(ActionListener.class, listener);
|
|
257 |
}
|
|
258 |
|
|
259 |
|
|
260 |
/**
|
|
261 |
* Returns an array of all the action listeners registered
|
|
262 |
* on this timer.
|
|
263 |
*
|
|
264 |
* @return all of the timer's <code>ActionListener</code>s or an empty
|
|
265 |
* array if no action listeners are currently registered
|
|
266 |
*
|
|
267 |
* @see #addActionListener
|
|
268 |
* @see #removeActionListener
|
|
269 |
*
|
|
270 |
* @since 1.4
|
|
271 |
*/
|
|
272 |
public ActionListener[] getActionListeners() {
|
|
273 |
return (ActionListener[])listenerList.getListeners(
|
|
274 |
ActionListener.class);
|
|
275 |
}
|
|
276 |
|
|
277 |
|
|
278 |
/**
|
|
279 |
* Notifies all listeners that have registered interest for
|
|
280 |
* notification on this event type.
|
|
281 |
*
|
|
282 |
* @param e the action event to fire
|
|
283 |
* @see EventListenerList
|
|
284 |
*/
|
|
285 |
protected void fireActionPerformed(ActionEvent e) {
|
|
286 |
// Guaranteed to return a non-null array
|
|
287 |
Object[] listeners = listenerList.getListenerList();
|
|
288 |
|
|
289 |
// Process the listeners last to first, notifying
|
|
290 |
// those that are interested in this event
|
|
291 |
for (int i=listeners.length-2; i>=0; i-=2) {
|
|
292 |
if (listeners[i]==ActionListener.class) {
|
|
293 |
((ActionListener)listeners[i+1]).actionPerformed(e);
|
|
294 |
}
|
|
295 |
}
|
|
296 |
}
|
|
297 |
|
|
298 |
/**
|
|
299 |
* Returns an array of all the objects currently registered as
|
|
300 |
* <code><em>Foo</em>Listener</code>s
|
|
301 |
* upon this <code>Timer</code>.
|
|
302 |
* <code><em>Foo</em>Listener</code>s
|
|
303 |
* are registered using the <code>add<em>Foo</em>Listener</code> method.
|
|
304 |
* <p>
|
|
305 |
* You can specify the <code>listenerType</code> argument
|
|
306 |
* with a class literal, such as <code><em>Foo</em>Listener.class</code>.
|
|
307 |
* For example, you can query a <code>Timer</code>
|
|
308 |
* instance <code>t</code>
|
|
309 |
* for its action listeners
|
|
310 |
* with the following code:
|
|
311 |
*
|
|
312 |
* <pre>ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));</pre>
|
|
313 |
*
|
|
314 |
* If no such listeners exist,
|
|
315 |
* this method returns an empty array.
|
|
316 |
*
|
|
317 |
* @param listenerType the type of listeners requested;
|
|
318 |
* this parameter should specify an interface
|
|
319 |
* that descends from <code>java.util.EventListener</code>
|
|
320 |
* @return an array of all objects registered as
|
|
321 |
* <code><em>Foo</em>Listener</code>s
|
|
322 |
* on this timer,
|
|
323 |
* or an empty array if no such
|
|
324 |
* listeners have been added
|
|
325 |
* @exception ClassCastException if <code>listenerType</code> doesn't
|
|
326 |
* specify a class or interface that implements
|
|
327 |
* <code>java.util.EventListener</code>
|
|
328 |
*
|
|
329 |
* @see #getActionListeners
|
|
330 |
* @see #addActionListener
|
|
331 |
* @see #removeActionListener
|
|
332 |
*
|
|
333 |
* @since 1.3
|
|
334 |
*/
|
|
335 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) {
|
|
336 |
return listenerList.getListeners(listenerType);
|
|
337 |
}
|
|
338 |
|
|
339 |
/**
|
|
340 |
* Returns the timer queue.
|
|
341 |
*/
|
|
342 |
private TimerQueue timerQueue() {
|
|
343 |
return TimerQueue.sharedInstance();
|
|
344 |
}
|
|
345 |
|
|
346 |
|
|
347 |
/**
|
|
348 |
* Enables or disables the timer log. When enabled, a message
|
|
349 |
* is posted to <code>System.out</code> whenever the timer goes off.
|
|
350 |
*
|
|
351 |
* @param flag <code>true</code> to enable logging
|
|
352 |
* @see #getLogTimers
|
|
353 |
*/
|
|
354 |
public static void setLogTimers(boolean flag) {
|
|
355 |
logTimers = flag;
|
|
356 |
}
|
|
357 |
|
|
358 |
|
|
359 |
/**
|
|
360 |
* Returns <code>true</code> if logging is enabled.
|
|
361 |
*
|
|
362 |
* @return <code>true</code> if logging is enabled; otherwise, false
|
|
363 |
* @see #setLogTimers
|
|
364 |
*/
|
|
365 |
public static boolean getLogTimers() {
|
|
366 |
return logTimers;
|
|
367 |
}
|
|
368 |
|
|
369 |
|
|
370 |
/**
|
|
371 |
* Sets the <code>Timer</code>'s between-event delay, the number of milliseconds
|
|
372 |
* between successive action events. This does not affect the initial delay
|
|
373 |
* property, which can be set by the {@code setInitialDelay} method.
|
|
374 |
*
|
|
375 |
* @param delay the delay in milliseconds
|
|
376 |
* @see #setInitialDelay
|
|
377 |
*/
|
|
378 |
public void setDelay(int delay) {
|
|
379 |
if (delay < 0) {
|
|
380 |
throw new IllegalArgumentException("Invalid delay: " + delay);
|
|
381 |
}
|
|
382 |
else {
|
|
383 |
this.delay = delay;
|
|
384 |
}
|
|
385 |
}
|
|
386 |
|
|
387 |
|
|
388 |
/**
|
|
389 |
* Returns the delay, in milliseconds,
|
|
390 |
* between firings of action events.
|
|
391 |
*
|
|
392 |
* @see #setDelay
|
|
393 |
* @see #getInitialDelay
|
|
394 |
*/
|
|
395 |
public int getDelay() {
|
|
396 |
return delay;
|
|
397 |
}
|
|
398 |
|
|
399 |
|
|
400 |
/**
|
|
401 |
* Sets the <code>Timer</code>'s initial delay, the time
|
|
402 |
* in milliseconds to wait after the timer is started
|
|
403 |
* before firing the first event. Upon construction, this
|
|
404 |
* is set to be the same as the between-event delay,
|
|
405 |
* but then its value is independent and remains unaffected
|
|
406 |
* by changes to the between-event delay.
|
|
407 |
*
|
|
408 |
* @param initialDelay the initial delay, in milliseconds
|
|
409 |
* @see #setDelay
|
|
410 |
*/
|
|
411 |
public void setInitialDelay(int initialDelay) {
|
|
412 |
if (initialDelay < 0) {
|
|
413 |
throw new IllegalArgumentException("Invalid initial delay: " +
|
|
414 |
initialDelay);
|
|
415 |
}
|
|
416 |
else {
|
|
417 |
this.initialDelay = initialDelay;
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
|
|
422 |
/**
|
|
423 |
* Returns the <code>Timer</code>'s initial delay.
|
|
424 |
*
|
|
425 |
* @see #setInitialDelay
|
|
426 |
* @see #setDelay
|
|
427 |
*/
|
|
428 |
public int getInitialDelay() {
|
|
429 |
return initialDelay;
|
|
430 |
}
|
|
431 |
|
|
432 |
|
|
433 |
/**
|
|
434 |
* If <code>flag</code> is <code>false</code>,
|
|
435 |
* instructs the <code>Timer</code> to send only one
|
|
436 |
* action event to its listeners.
|
|
437 |
*
|
|
438 |
* @param flag specify <code>false</code> to make the timer
|
|
439 |
* stop after sending its first action event
|
|
440 |
*/
|
|
441 |
public void setRepeats(boolean flag) {
|
|
442 |
repeats = flag;
|
|
443 |
}
|
|
444 |
|
|
445 |
|
|
446 |
/**
|
|
447 |
* Returns <code>true</code> (the default)
|
|
448 |
* if the <code>Timer</code> will send
|
|
449 |
* an action event
|
|
450 |
* to its listeners multiple times.
|
|
451 |
*
|
|
452 |
* @see #setRepeats
|
|
453 |
*/
|
|
454 |
public boolean isRepeats() {
|
|
455 |
return repeats;
|
|
456 |
}
|
|
457 |
|
|
458 |
|
|
459 |
/**
|
|
460 |
* Sets whether the <code>Timer</code> coalesces multiple pending
|
|
461 |
* <code>ActionEvent</code> firings.
|
|
462 |
* A busy application may not be able
|
|
463 |
* to keep up with a <code>Timer</code>'s event generation,
|
|
464 |
* causing multiple
|
|
465 |
* action events to be queued. When processed,
|
|
466 |
* the application sends these events one after the other, causing the
|
|
467 |
* <code>Timer</code>'s listeners to receive a sequence of
|
|
468 |
* events with no delay between them. Coalescing avoids this situation
|
|
469 |
* by reducing multiple pending events to a single event.
|
|
470 |
* <code>Timer</code>s
|
|
471 |
* coalesce events by default.
|
|
472 |
*
|
|
473 |
* @param flag specify <code>false</code> to turn off coalescing
|
|
474 |
*/
|
|
475 |
public void setCoalesce(boolean flag) {
|
|
476 |
boolean old = coalesce;
|
|
477 |
coalesce = flag;
|
|
478 |
if (!old && coalesce) {
|
|
479 |
// We must do this as otherwise if the Timer once notified
|
|
480 |
// in !coalese mode notify will be stuck to true and never
|
|
481 |
// become false.
|
|
482 |
cancelEvent();
|
|
483 |
}
|
|
484 |
}
|
|
485 |
|
|
486 |
|
|
487 |
/**
|
|
488 |
* Returns <code>true</code> if the <code>Timer</code> coalesces
|
|
489 |
* multiple pending action events.
|
|
490 |
*
|
|
491 |
* @see #setCoalesce
|
|
492 |
*/
|
|
493 |
public boolean isCoalesce() {
|
|
494 |
return coalesce;
|
|
495 |
}
|
|
496 |
|
|
497 |
|
|
498 |
/**
|
|
499 |
* Sets the string that will be delivered as the action command
|
|
500 |
* in <code>ActionEvent</code>s fired by this timer.
|
|
501 |
* <code>null</code> is an acceptable value.
|
|
502 |
*
|
|
503 |
* @param command the action command
|
|
504 |
* @since 1.6
|
|
505 |
*/
|
|
506 |
public void setActionCommand(String command) {
|
|
507 |
this.actionCommand = command;
|
|
508 |
}
|
|
509 |
|
|
510 |
|
|
511 |
/**
|
|
512 |
* Returns the string that will be delivered as the action command
|
|
513 |
* in <code>ActionEvent</code>s fired by this timer. May be
|
|
514 |
* <code>null</code>, which is also the default.
|
|
515 |
*
|
|
516 |
* @return the action command used in firing events
|
|
517 |
* @since 1.6
|
|
518 |
*/
|
|
519 |
public String getActionCommand() {
|
|
520 |
return actionCommand;
|
|
521 |
}
|
|
522 |
|
|
523 |
|
|
524 |
/**
|
|
525 |
* Starts the <code>Timer</code>,
|
|
526 |
* causing it to start sending action events
|
|
527 |
* to its listeners.
|
|
528 |
*
|
|
529 |
* @see #stop
|
|
530 |
*/
|
|
531 |
public void start() {
|
|
532 |
timerQueue().addTimer(this, getInitialDelay());
|
|
533 |
}
|
|
534 |
|
|
535 |
|
|
536 |
/**
|
|
537 |
* Returns <code>true</code> if the <code>Timer</code> is running.
|
|
538 |
*
|
|
539 |
* @see #start
|
|
540 |
*/
|
|
541 |
public boolean isRunning() {
|
|
542 |
return timerQueue().containsTimer(this);
|
|
543 |
}
|
|
544 |
|
|
545 |
|
|
546 |
/**
|
|
547 |
* Stops the <code>Timer</code>,
|
|
548 |
* causing it to stop sending action events
|
|
549 |
* to its listeners.
|
|
550 |
*
|
|
551 |
* @see #start
|
|
552 |
*/
|
|
553 |
public void stop() {
|
|
554 |
getLock().lock();
|
|
555 |
try {
|
|
556 |
cancelEvent();
|
|
557 |
timerQueue().removeTimer(this);
|
|
558 |
} finally {
|
|
559 |
getLock().unlock();
|
|
560 |
}
|
|
561 |
}
|
|
562 |
|
|
563 |
|
|
564 |
/**
|
|
565 |
* Restarts the <code>Timer</code>,
|
|
566 |
* canceling any pending firings and causing
|
|
567 |
* it to fire with its initial delay.
|
|
568 |
*/
|
|
569 |
public void restart() {
|
|
570 |
getLock().lock();
|
|
571 |
try {
|
|
572 |
stop();
|
|
573 |
start();
|
|
574 |
} finally {
|
|
575 |
getLock().unlock();
|
|
576 |
}
|
|
577 |
}
|
|
578 |
|
|
579 |
|
|
580 |
/**
|
|
581 |
* Resets the internal state to indicate this Timer shouldn't notify
|
|
582 |
* any of its listeners. This does not stop a repeatable Timer from
|
|
583 |
* firing again, use <code>stop</code> for that.
|
|
584 |
*/
|
|
585 |
void cancelEvent() {
|
|
586 |
notify.set(false);
|
|
587 |
}
|
|
588 |
|
|
589 |
|
|
590 |
void post() {
|
|
591 |
if (notify.compareAndSet(false, true) || !coalesce) {
|
|
592 |
SwingUtilities.invokeLater(doPostEvent);
|
|
593 |
}
|
|
594 |
}
|
|
595 |
|
|
596 |
Lock getLock() {
|
|
597 |
return lock;
|
|
598 |
}
|
|
599 |
|
|
600 |
/*
|
|
601 |
* We have to use readResolve because we can not initialize final
|
|
602 |
* fields for deserialized object otherwise
|
|
603 |
*/
|
|
604 |
private Object readResolve() {
|
|
605 |
Timer timer = new Timer(getDelay(), null);
|
|
606 |
timer.listenerList = listenerList;
|
|
607 |
timer.initialDelay = initialDelay;
|
|
608 |
timer.delay = delay;
|
|
609 |
timer.repeats = repeats;
|
|
610 |
timer.coalesce = coalesce;
|
|
611 |
timer.actionCommand = actionCommand;
|
|
612 |
return timer;
|
|
613 |
}
|
|
614 |
}
|