author | rupashka |
Tue, 26 Aug 2008 15:12:54 +0400 | |
changeset 1301 | 15e81207e1f2 |
parent 2 | 90ce3da70b43 |
child 1639 | a97859015238 |
permissions | -rw-r--r-- |
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() { |
|
1301
15e81207e1f2
6727662: Code improvement and warnings removing from swing packages
rupashka
parents:
2
diff
changeset
|
273 |
return listenerList.getListeners(ActionListener.class); |
2 | 274 |
} |
275 |
||
276 |
||
277 |
/** |
|
278 |
* Notifies all listeners that have registered interest for |
|
279 |
* notification on this event type. |
|
280 |
* |
|
281 |
* @param e the action event to fire |
|
282 |
* @see EventListenerList |
|
283 |
*/ |
|
284 |
protected void fireActionPerformed(ActionEvent e) { |
|
285 |
// Guaranteed to return a non-null array |
|
286 |
Object[] listeners = listenerList.getListenerList(); |
|
287 |
||
288 |
// Process the listeners last to first, notifying |
|
289 |
// those that are interested in this event |
|
290 |
for (int i=listeners.length-2; i>=0; i-=2) { |
|
291 |
if (listeners[i]==ActionListener.class) { |
|
292 |
((ActionListener)listeners[i+1]).actionPerformed(e); |
|
293 |
} |
|
294 |
} |
|
295 |
} |
|
296 |
||
297 |
/** |
|
298 |
* Returns an array of all the objects currently registered as |
|
299 |
* <code><em>Foo</em>Listener</code>s |
|
300 |
* upon this <code>Timer</code>. |
|
301 |
* <code><em>Foo</em>Listener</code>s |
|
302 |
* are registered using the <code>add<em>Foo</em>Listener</code> method. |
|
303 |
* <p> |
|
304 |
* You can specify the <code>listenerType</code> argument |
|
305 |
* with a class literal, such as <code><em>Foo</em>Listener.class</code>. |
|
306 |
* For example, you can query a <code>Timer</code> |
|
307 |
* instance <code>t</code> |
|
308 |
* for its action listeners |
|
309 |
* with the following code: |
|
310 |
* |
|
311 |
* <pre>ActionListener[] als = (ActionListener[])(t.getListeners(ActionListener.class));</pre> |
|
312 |
* |
|
313 |
* If no such listeners exist, |
|
314 |
* this method returns an empty array. |
|
315 |
* |
|
316 |
* @param listenerType the type of listeners requested; |
|
317 |
* this parameter should specify an interface |
|
318 |
* that descends from <code>java.util.EventListener</code> |
|
319 |
* @return an array of all objects registered as |
|
320 |
* <code><em>Foo</em>Listener</code>s |
|
321 |
* on this timer, |
|
322 |
* or an empty array if no such |
|
323 |
* listeners have been added |
|
324 |
* @exception ClassCastException if <code>listenerType</code> doesn't |
|
325 |
* specify a class or interface that implements |
|
326 |
* <code>java.util.EventListener</code> |
|
327 |
* |
|
328 |
* @see #getActionListeners |
|
329 |
* @see #addActionListener |
|
330 |
* @see #removeActionListener |
|
331 |
* |
|
332 |
* @since 1.3 |
|
333 |
*/ |
|
334 |
public <T extends EventListener> T[] getListeners(Class<T> listenerType) { |
|
335 |
return listenerList.getListeners(listenerType); |
|
336 |
} |
|
337 |
||
338 |
/** |
|
339 |
* Returns the timer queue. |
|
340 |
*/ |
|
341 |
private TimerQueue timerQueue() { |
|
342 |
return TimerQueue.sharedInstance(); |
|
343 |
} |
|
344 |
||
345 |
||
346 |
/** |
|
347 |
* Enables or disables the timer log. When enabled, a message |
|
348 |
* is posted to <code>System.out</code> whenever the timer goes off. |
|
349 |
* |
|
350 |
* @param flag <code>true</code> to enable logging |
|
351 |
* @see #getLogTimers |
|
352 |
*/ |
|
353 |
public static void setLogTimers(boolean flag) { |
|
354 |
logTimers = flag; |
|
355 |
} |
|
356 |
||
357 |
||
358 |
/** |
|
359 |
* Returns <code>true</code> if logging is enabled. |
|
360 |
* |
|
361 |
* @return <code>true</code> if logging is enabled; otherwise, false |
|
362 |
* @see #setLogTimers |
|
363 |
*/ |
|
364 |
public static boolean getLogTimers() { |
|
365 |
return logTimers; |
|
366 |
} |
|
367 |
||
368 |
||
369 |
/** |
|
370 |
* Sets the <code>Timer</code>'s between-event delay, the number of milliseconds |
|
371 |
* between successive action events. This does not affect the initial delay |
|
372 |
* property, which can be set by the {@code setInitialDelay} method. |
|
373 |
* |
|
374 |
* @param delay the delay in milliseconds |
|
375 |
* @see #setInitialDelay |
|
376 |
*/ |
|
377 |
public void setDelay(int delay) { |
|
378 |
if (delay < 0) { |
|
379 |
throw new IllegalArgumentException("Invalid delay: " + delay); |
|
380 |
} |
|
381 |
else { |
|
382 |
this.delay = delay; |
|
383 |
} |
|
384 |
} |
|
385 |
||
386 |
||
387 |
/** |
|
388 |
* Returns the delay, in milliseconds, |
|
389 |
* between firings of action events. |
|
390 |
* |
|
391 |
* @see #setDelay |
|
392 |
* @see #getInitialDelay |
|
393 |
*/ |
|
394 |
public int getDelay() { |
|
395 |
return delay; |
|
396 |
} |
|
397 |
||
398 |
||
399 |
/** |
|
400 |
* Sets the <code>Timer</code>'s initial delay, the time |
|
401 |
* in milliseconds to wait after the timer is started |
|
402 |
* before firing the first event. Upon construction, this |
|
403 |
* is set to be the same as the between-event delay, |
|
404 |
* but then its value is independent and remains unaffected |
|
405 |
* by changes to the between-event delay. |
|
406 |
* |
|
407 |
* @param initialDelay the initial delay, in milliseconds |
|
408 |
* @see #setDelay |
|
409 |
*/ |
|
410 |
public void setInitialDelay(int initialDelay) { |
|
411 |
if (initialDelay < 0) { |
|
412 |
throw new IllegalArgumentException("Invalid initial delay: " + |
|
413 |
initialDelay); |
|
414 |
} |
|
415 |
else { |
|
416 |
this.initialDelay = initialDelay; |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
||
421 |
/** |
|
422 |
* Returns the <code>Timer</code>'s initial delay. |
|
423 |
* |
|
424 |
* @see #setInitialDelay |
|
425 |
* @see #setDelay |
|
426 |
*/ |
|
427 |
public int getInitialDelay() { |
|
428 |
return initialDelay; |
|
429 |
} |
|
430 |
||
431 |
||
432 |
/** |
|
433 |
* If <code>flag</code> is <code>false</code>, |
|
434 |
* instructs the <code>Timer</code> to send only one |
|
435 |
* action event to its listeners. |
|
436 |
* |
|
437 |
* @param flag specify <code>false</code> to make the timer |
|
438 |
* stop after sending its first action event |
|
439 |
*/ |
|
440 |
public void setRepeats(boolean flag) { |
|
441 |
repeats = flag; |
|
442 |
} |
|
443 |
||
444 |
||
445 |
/** |
|
446 |
* Returns <code>true</code> (the default) |
|
447 |
* if the <code>Timer</code> will send |
|
448 |
* an action event |
|
449 |
* to its listeners multiple times. |
|
450 |
* |
|
451 |
* @see #setRepeats |
|
452 |
*/ |
|
453 |
public boolean isRepeats() { |
|
454 |
return repeats; |
|
455 |
} |
|
456 |
||
457 |
||
458 |
/** |
|
459 |
* Sets whether the <code>Timer</code> coalesces multiple pending |
|
460 |
* <code>ActionEvent</code> firings. |
|
461 |
* A busy application may not be able |
|
462 |
* to keep up with a <code>Timer</code>'s event generation, |
|
463 |
* causing multiple |
|
464 |
* action events to be queued. When processed, |
|
465 |
* the application sends these events one after the other, causing the |
|
466 |
* <code>Timer</code>'s listeners to receive a sequence of |
|
467 |
* events with no delay between them. Coalescing avoids this situation |
|
468 |
* by reducing multiple pending events to a single event. |
|
469 |
* <code>Timer</code>s |
|
470 |
* coalesce events by default. |
|
471 |
* |
|
472 |
* @param flag specify <code>false</code> to turn off coalescing |
|
473 |
*/ |
|
474 |
public void setCoalesce(boolean flag) { |
|
475 |
boolean old = coalesce; |
|
476 |
coalesce = flag; |
|
477 |
if (!old && coalesce) { |
|
478 |
// We must do this as otherwise if the Timer once notified |
|
479 |
// in !coalese mode notify will be stuck to true and never |
|
480 |
// become false. |
|
481 |
cancelEvent(); |
|
482 |
} |
|
483 |
} |
|
484 |
||
485 |
||
486 |
/** |
|
487 |
* Returns <code>true</code> if the <code>Timer</code> coalesces |
|
488 |
* multiple pending action events. |
|
489 |
* |
|
490 |
* @see #setCoalesce |
|
491 |
*/ |
|
492 |
public boolean isCoalesce() { |
|
493 |
return coalesce; |
|
494 |
} |
|
495 |
||
496 |
||
497 |
/** |
|
498 |
* Sets the string that will be delivered as the action command |
|
499 |
* in <code>ActionEvent</code>s fired by this timer. |
|
500 |
* <code>null</code> is an acceptable value. |
|
501 |
* |
|
502 |
* @param command the action command |
|
503 |
* @since 1.6 |
|
504 |
*/ |
|
505 |
public void setActionCommand(String command) { |
|
506 |
this.actionCommand = command; |
|
507 |
} |
|
508 |
||
509 |
||
510 |
/** |
|
511 |
* Returns the string that will be delivered as the action command |
|
512 |
* in <code>ActionEvent</code>s fired by this timer. May be |
|
513 |
* <code>null</code>, which is also the default. |
|
514 |
* |
|
515 |
* @return the action command used in firing events |
|
516 |
* @since 1.6 |
|
517 |
*/ |
|
518 |
public String getActionCommand() { |
|
519 |
return actionCommand; |
|
520 |
} |
|
521 |
||
522 |
||
523 |
/** |
|
524 |
* Starts the <code>Timer</code>, |
|
525 |
* causing it to start sending action events |
|
526 |
* to its listeners. |
|
527 |
* |
|
528 |
* @see #stop |
|
529 |
*/ |
|
530 |
public void start() { |
|
531 |
timerQueue().addTimer(this, getInitialDelay()); |
|
532 |
} |
|
533 |
||
534 |
||
535 |
/** |
|
536 |
* Returns <code>true</code> if the <code>Timer</code> is running. |
|
537 |
* |
|
538 |
* @see #start |
|
539 |
*/ |
|
540 |
public boolean isRunning() { |
|
541 |
return timerQueue().containsTimer(this); |
|
542 |
} |
|
543 |
||
544 |
||
545 |
/** |
|
546 |
* Stops the <code>Timer</code>, |
|
547 |
* causing it to stop sending action events |
|
548 |
* to its listeners. |
|
549 |
* |
|
550 |
* @see #start |
|
551 |
*/ |
|
552 |
public void stop() { |
|
553 |
getLock().lock(); |
|
554 |
try { |
|
555 |
cancelEvent(); |
|
556 |
timerQueue().removeTimer(this); |
|
557 |
} finally { |
|
558 |
getLock().unlock(); |
|
559 |
} |
|
560 |
} |
|
561 |
||
562 |
||
563 |
/** |
|
564 |
* Restarts the <code>Timer</code>, |
|
565 |
* canceling any pending firings and causing |
|
566 |
* it to fire with its initial delay. |
|
567 |
*/ |
|
568 |
public void restart() { |
|
569 |
getLock().lock(); |
|
570 |
try { |
|
571 |
stop(); |
|
572 |
start(); |
|
573 |
} finally { |
|
574 |
getLock().unlock(); |
|
575 |
} |
|
576 |
} |
|
577 |
||
578 |
||
579 |
/** |
|
580 |
* Resets the internal state to indicate this Timer shouldn't notify |
|
581 |
* any of its listeners. This does not stop a repeatable Timer from |
|
582 |
* firing again, use <code>stop</code> for that. |
|
583 |
*/ |
|
584 |
void cancelEvent() { |
|
585 |
notify.set(false); |
|
586 |
} |
|
587 |
||
588 |
||
589 |
void post() { |
|
590 |
if (notify.compareAndSet(false, true) || !coalesce) { |
|
591 |
SwingUtilities.invokeLater(doPostEvent); |
|
592 |
} |
|
593 |
} |
|
594 |
||
595 |
Lock getLock() { |
|
596 |
return lock; |
|
597 |
} |
|
598 |
||
599 |
/* |
|
600 |
* We have to use readResolve because we can not initialize final |
|
601 |
* fields for deserialized object otherwise |
|
602 |
*/ |
|
603 |
private Object readResolve() { |
|
604 |
Timer timer = new Timer(getDelay(), null); |
|
605 |
timer.listenerList = listenerList; |
|
606 |
timer.initialDelay = initialDelay; |
|
607 |
timer.delay = delay; |
|
608 |
timer.repeats = repeats; |
|
609 |
timer.coalesce = coalesce; |
|
610 |
timer.actionCommand = actionCommand; |
|
611 |
return timer; |
|
612 |
} |
|
613 |
} |