2 Copyright (C) 2002, 2004 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;
51 public class Timer implements Serializable
54 private static final long serialVersionUID = -1116180831621385484L;
57 protected EventListenerList listenerList = new EventListenerList();
59 // This object manages a "queue" of virtual actionEvents, maintained as a
60 // simple long counter. When the timer expires, a new event is queued,
61 // and a dispatcher object is pushed into the system event queue. When
62 // the system thread runs the dispatcher, it will fire as many
63 // ActionEvents as have been queued, unless the timer is set to
64 // coalescing mode, in which case it will fire only one ActionEvent.
70 private Object queueLock = new Object();
75 private Runnable drainer = new Runnable()
86 private void queueEvent()
88 synchronized (queueLock)
92 SwingUtilities.invokeLater(drainer);
99 private void drainEvents()
101 synchronized (queueLock)
106 fireActionPerformed();
112 fireActionPerformed();
120 static boolean logTimers;
123 boolean coalesce = true;
126 boolean repeats = true;
143 private class Waker extends Thread
161 catch (InterruptedException e)
168 System.out.println("javax.swing.Timer -> clocktick");
177 // System.out.println("swing.Timer::" + e);
183 * Creates a new Timer object.
185 * @param d DOCUMENT ME!
186 * @param listener DOCUMENT ME!
188 public Timer(int d, ActionListener listener)
192 if (listener != null)
193 addActionListener(listener);
199 * @param c DOCUMENT ME!
201 public void setCoalesce(boolean c)
209 * @return DOCUMENT ME!
211 public boolean isCoalesce()
219 * @param listener DOCUMENT ME!
221 public void addActionListener(ActionListener listener)
223 listenerList.add(ActionListener.class, listener);
229 * @param listener DOCUMENT ME!
231 public void removeActionListener(ActionListener listener)
233 listenerList.remove(ActionListener.class, listener);
239 * @param listenerType DOCUMENT ME!
241 * @return DOCUMENT ME!
245 public EventListener[] getListeners(Class listenerType)
247 return listenerList.getListeners(listenerType);
253 * @return DOCUMENT ME!
257 public ActionListener[] getActionListeners()
259 return (ActionListener[]) listenerList.getListeners(ActionListener.class);
265 * @param event DOCUMENT ME!
267 protected void fireActionPerformed(ActionEvent event)
269 ActionListener[] listeners = getActionListeners();
271 for (int i = 0; i < listeners.length; i++)
272 listeners[i].actionPerformed(event);
278 void fireActionPerformed()
280 fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
286 * @param lt DOCUMENT ME!
288 public static void setLogTimers(boolean lt)
296 * @return DOCUMENT ME!
298 public static boolean getLogTimers()
306 * @param d DOCUMENT ME!
308 public void setDelay(int d)
316 * @return DOCUMENT ME!
318 public int getDelay()
326 * @param i DOCUMENT ME!
328 public void setInitialDelay(int i)
336 * @return DOCUMENT ME!
338 public int getInitialDelay()
346 * @param r DOCUMENT ME!
348 public void setRepeats(boolean r)
356 * @return DOCUMENT ME!
358 public boolean isRepeats()
366 * @return DOCUMENT ME!
368 public boolean isRunning()
387 public void restart()
401 synchronized (queueLock)