Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / Timer.java
blob0906b8d93bb50b17695187a468d038c616b54ef2
1 /* Timer.java --
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)
9 any later version.
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
19 02111-1307 USA.
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
24 combination.
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. */
39 package javax.swing;
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;
48 /**
49 * DOCUMENT ME!
51 public class Timer implements Serializable
53 /** DOCUMENT ME! */
54 private static final long serialVersionUID = -1116180831621385484L;
56 /** DOCUMENT ME! */
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.
66 /** DOCUMENT ME! */
67 private long queue;
69 /** DOCUMENT ME! */
70 private Object queueLock = new Object();
72 /** DOCUMENT ME! */
73 private Waker waker;
75 private Runnable drainer = new Runnable()
77 public void run()
79 drainEvents();
83 /**
84 * DOCUMENT ME!
86 private void queueEvent()
88 synchronized (queueLock)
90 queue++;
91 if (queue == 1)
92 SwingUtilities.invokeLater(drainer);
96 /**
97 * DOCUMENT ME!
99 private void drainEvents()
101 synchronized (queueLock)
103 if (isCoalesce())
105 if (queue > 0)
106 fireActionPerformed();
108 else
110 while (queue > 0)
112 fireActionPerformed();
113 queue--;
116 queue = 0;
120 static boolean logTimers;
122 /** DOCUMENT ME! */
123 boolean coalesce = true;
125 /** DOCUMENT ME! */
126 boolean repeats = true;
128 /** DOCUMENT ME! */
129 boolean running;
131 /** DOCUMENT ME! */
132 int ticks;
134 /** DOCUMENT ME! */
135 int delay;
137 /** DOCUMENT ME! */
138 int initialDelay;
141 * DOCUMENT ME!
143 private class Waker extends Thread
146 * DOCUMENT ME!
148 public void run()
150 running = true;
153 sleep(initialDelay);
155 while (running)
159 sleep(delay);
161 catch (InterruptedException e)
163 return;
165 queueEvent();
167 if (logTimers)
168 System.out.println("javax.swing.Timer -> clocktick");
170 if (! repeats)
171 break;
173 running = false;
175 catch (Exception e)
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)
190 delay = d;
192 if (listener != null)
193 addActionListener(listener);
197 * DOCUMENT ME!
199 * @param c DOCUMENT ME!
201 public void setCoalesce(boolean c)
203 coalesce = c;
207 * DOCUMENT ME!
209 * @return DOCUMENT ME!
211 public boolean isCoalesce()
213 return coalesce;
217 * DOCUMENT ME!
219 * @param listener DOCUMENT ME!
221 public void addActionListener(ActionListener listener)
223 listenerList.add(ActionListener.class, listener);
227 * DOCUMENT ME!
229 * @param listener DOCUMENT ME!
231 public void removeActionListener(ActionListener listener)
233 listenerList.remove(ActionListener.class, listener);
237 * DOCUMENT ME!
239 * @param listenerType DOCUMENT ME!
241 * @return DOCUMENT ME!
243 * @since 1.3
245 public EventListener[] getListeners(Class listenerType)
247 return listenerList.getListeners(listenerType);
251 * DOCUMENT ME!
253 * @return DOCUMENT ME!
255 * @since 1.4
257 public ActionListener[] getActionListeners()
259 return (ActionListener[]) listenerList.getListeners(ActionListener.class);
263 * DOCUMENT ME!
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);
276 * DOCUMENT ME!
278 void fireActionPerformed()
280 fireActionPerformed(new ActionEvent(this, ticks++, "Timer"));
284 * DOCUMENT ME!
286 * @param lt DOCUMENT ME!
288 public static void setLogTimers(boolean lt)
290 logTimers = lt;
294 * DOCUMENT ME!
296 * @return DOCUMENT ME!
298 public static boolean getLogTimers()
300 return logTimers;
304 * DOCUMENT ME!
306 * @param d DOCUMENT ME!
308 public void setDelay(int d)
310 delay = d;
314 * DOCUMENT ME!
316 * @return DOCUMENT ME!
318 public int getDelay()
320 return delay;
324 * DOCUMENT ME!
326 * @param i DOCUMENT ME!
328 public void setInitialDelay(int i)
330 initialDelay = i;
334 * DOCUMENT ME!
336 * @return DOCUMENT ME!
338 public int getInitialDelay()
340 return initialDelay;
344 * DOCUMENT ME!
346 * @param r DOCUMENT ME!
348 public void setRepeats(boolean r)
350 repeats = r;
354 * DOCUMENT ME!
356 * @return DOCUMENT ME!
358 public boolean isRepeats()
360 return repeats;
364 * DOCUMENT ME!
366 * @return DOCUMENT ME!
368 public boolean isRunning()
370 return running;
374 * DOCUMENT ME!
376 public void start()
378 if (isRunning())
379 return;
380 waker = new Waker();
381 waker.start();
385 * DOCUMENT ME!
387 public void restart()
389 stop();
390 start();
394 * DOCUMENT ME!
396 public void stop()
398 running = false;
399 if (waker != null)
400 waker.interrupt();
401 synchronized (queueLock)
403 queue = 0;