2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.io.Serializable;
44 import java.util.EventListener;
46 import javax.swing.event.EventListenerList;
49 * Fires one or more action events after the specified delay.
50 * @author Ronald Veldema
51 * @author Audrius Meskauskas (audriusa@Bionformatics.org) - bug fixes
52 * and documentation comments
55 implements Serializable
64 * Fires events, pausing for required intervals.
79 catch (InterruptedException e)
86 System.out.println("javax.swing.Timer -> clocktick");
100 * Use serialVersionUID for interoperability.
102 private static final long serialVersionUID = -1116180831621385484L;
105 * The encloding class, used with {@link SwingUtilities#invokeLater}
106 * to invoke the {@link #drainEvents()}.
108 private Runnable drainer = new Runnable()
117 * If <code>true</code>, the timer prints a message to
118 * {@link System#out} when firing each event.
120 static boolean logTimers;
123 * A field to store all listeners who are listening to this timer.
125 protected EventListenerList listenerList = new EventListenerList();
128 * <code>true</code> if the timer coalesces events.
130 boolean coalesce = true;
133 * <code>true</code> if the timer is firing repetetive events.
135 boolean repeats = true;
138 * <code>true</code> if the timer is currently active, firing events
144 * The delay between subsequent repetetive events.
149 * The initial delay before the first event.
154 * The number of events that have been already fired by this timer.
155 * This is used as a numeric identifier for the next event that would
161 * Stores the thread that posts events to the queue at required time
167 * This object manages a "queue" of virtual actionEvents, maintained as a
168 * simple long counter. When the timer expires, a new event is queued,
169 * and a dispatcher object is pushed into the system event queue. When
170 * the system thread runs the dispatcher, it will fire as many
171 * ActionEvents as have been queued, unless the timer is set to
172 * coalescing mode, in which case it will fire only one ActionEvent.
177 * <code>synchronized(queueLock)</code> replaces
178 * <code>synchronized(queue)</code> that is not supported by this language.
180 private Object queueLock = new Object();
183 * Creates a new Timer object.
185 * @param d the default value for both initial and between event delay, in
187 * @param listener the first action listener, can be <code>null</code>.
189 public Timer(int d, ActionListener listener)
193 if (listener != null)
194 addActionListener(listener);
198 * Get the array of action listeners.
200 * @return the array of action listeners that are listening for the events,
201 * fired by this timer
205 public ActionListener[] getActionListeners()
207 return (ActionListener[]) listenerList.getListeners(ActionListener.class);
211 * Sets whether the Timer coalesces multiple pending event firings.
212 * If the coalescing is enabled, the multiple events that have not been
213 * fired on time are replaced by the single event. The events may not
214 * be fired on time if the application is busy.
216 * @param c <code>true</code> (default) to enable the event coalescing,
217 * <code>false</code> otherwise
219 public void setCoalesce(boolean c)
225 * Checks if the Timer coalesces multiple pending event firings.
226 * If the coalescing is enabled, the multiple events that have not been
227 * fired on time are replaced by the single event. The events may not
228 * be fired on time if the application is busy.
230 * @return <code>true</code> if the coalescing is enabled,
231 * <code>false</code> otherwise
233 public boolean isCoalesce()
239 * Get the event listeners of the given type that are listening for the
240 * events, fired by this timer.
242 * @param listenerType the listener type (for example, ActionListener.class)
244 * @return the array of event listeners that are listening for the events,
245 * fired by this timer
248 public EventListener[] getListeners(Class listenerType)
250 return listenerList.getListeners(listenerType);
254 * Set the timer logging state. If it is set to <code>true</code>, the
255 * timer prints a message to {@link System#out} when firing each
258 * @param lt <code>true</code> if logging is enabled, <code>false</code>
259 * (default value) otherwise
261 public static void setLogTimers(boolean lt)
267 * Return the logging state.
269 * @return <code>true</code> if the timer is printing a message to
271 * when firing each action event
273 public static boolean getLogTimers()
279 * Set the delay between firing the subsequent events.
280 * This parameter does not change the value of the initial delay before
281 * firing the first event.
283 * @param d The time gap between the subsequent events, in milliseconds
285 public void setDelay(int d)
291 * Get the delay between firing the subsequent events.
293 * @return The delay between subsequent events, in milliseconds
295 public int getDelay()
301 * Set the intial delay before firing the first event since calling
302 * the {@link #start()} method. If the initial delay has not been
303 * set, it is assumed having the same value as the delay between the
306 * @param i the initial delay, in milliseconds
308 public void setInitialDelay(int i)
314 * Get the intial delay before firing the first event since calling
315 * the {@link #start()} method. If the initial delay has not been
316 * set, returns the same value as {@link #getDelay()}.
318 * @return the initial delay before firing the first action event.
320 public int getInitialDelay()
326 * Enable firing the repetetive events.
328 * @param r <code>true</code> (default value) to fire repetetive events.
329 * <code>false</code> to fire
330 * only one event after the initial delay
332 public void setRepeats(boolean r)
338 * Check is this timer fires repetetive events.
340 * @return <code>true</code> if the timer fires repetetive events,
341 * <code>false</code> if it fires
342 * only one event after the initial delay
344 public boolean isRepeats()
350 * Get the timer state.
352 * @return <code>true</code> if the timer has been started and is firing
353 * the action events as scheduled. <code>false</code>
354 * if the timer is inactive.
356 public boolean isRunning()
362 * Add the action listener
364 * @param listener the action listener to add
366 public void addActionListener(ActionListener listener)
368 listenerList.add(ActionListener.class, listener);
372 * Remove the action listener.
374 * @param listener the action listener to remove
376 public void removeActionListener(ActionListener listener)
378 listenerList.remove(ActionListener.class, listener);
382 * Cancel all pending tasks and fire the first event after the initial
385 public void restart()
392 * Start firing the action events.
403 * Stop firing the action events.
410 synchronized (queueLock)
417 * Fire the given action event to the action listeners.
419 * @param event the event to fire
421 protected void fireActionPerformed(ActionEvent event)
423 ActionListener[] listeners = getActionListeners();
425 for (int i = 0; i < listeners.length; i++)
426 listeners [ i ].actionPerformed(event);
430 * Fire the action event, named "Timer" and having the numeric
431 * identifier, equal to the numer of events that have been
432 * already fired before.
434 void fireActionPerformed()
436 fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
440 * Fire the queued action events.
441 * In the coalescing mode, a single event is fired as a replacement
442 * for all queued events. In non coalescing mode, a series of
443 * all queued events is fired.
444 * This is package-private to avoid an accessor method.
448 synchronized (queueLock)
453 fireActionPerformed();
459 fireActionPerformed();
468 * Post a scheduled event to the event queue.
469 * Package-private to avoid an accessor method.
471 private void queueEvent()
473 synchronized (queueLock)
477 SwingUtilities.invokeLater(drainer);