OSDN Git Service

2004-06-17 Michael Koch <konqueror@gmx.de>
[pf3gnuchains/gcc-fork.git] / libjava / javax / swing / Timer.java
1 /* Timer.java -- 
2    Copyright (C) 2002, 2004  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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.
10
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.
15
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.
20
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.
25
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. */
37
38
39 package javax.swing;
40
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43 import java.io.Serializable;
44 import java.util.EventListener;
45
46 import javax.swing.event.EventListenerList;
47
48 public class Timer implements Serializable
49 {
50   private static final long serialVersionUID = -1116180831621385484L;
51   
52   protected EventListenerList listenerList = new EventListenerList();
53   
54   // This object manages a "queue" of virtual actionEvents, maintained as a
55   // simple long counter. When the timer expires, a new event is queued,
56   // and a dispatcher object is pushed into the system event queue. When
57   // the system thread runs the dispatcher, it will fire as many
58   // ActionEvents as have been queued, unless the timer is set to
59   // coalescing mode, in which case it will fire only one ActionEvent.
60
61   private long queue;
62   private Object queueLock = new Object();
63   private void queueEvent()
64   {
65     synchronized (queueLock)
66       {
67         queue++;
68         if (queue == 1)
69           SwingUtilities.invokeLater(new Runnable() { public void run() { drainEvents(); } });
70       }
71   }
72
73   private void drainEvents()
74   {
75     synchronized (queueLock)
76       {
77         if (isCoalesce())
78           {
79             if (queue > 0)
80               fireActionPerformed();
81           }
82         else
83           {
84             while(queue > 0)
85               {                  
86                 fireActionPerformed();
87                 queue--;
88               }          
89           }
90         queue = 0;
91       }
92   }
93   
94
95   static boolean logTimers;
96   boolean coalesce = true;
97   boolean repeats = true;
98   boolean running;
99   int ticks;
100   int delay;
101   int initialDelay;
102     
103   private class Waker 
104     extends Thread
105   {
106     public void run()
107     {
108       running = true;
109       try 
110         {
111
112           sleep(initialDelay);
113           
114           while (running)
115             {
116               sleep(delay);
117               
118               if (logTimers)
119                 System.out.println("javax.swing.Timer -> clocktick");
120               
121               if (! repeats)
122                 break;
123             }
124           running = false;
125       } 
126       catch (Exception e) 
127         {
128           System.out.println("swing.Timer::" + e);
129         }
130     }
131   }
132
133   public Timer(int d, ActionListener listener)
134   {
135     delay = d;
136
137     if (listener != null)
138       addActionListener(listener);
139   }
140
141   public void setCoalesce(boolean c)
142   {
143     coalesce = c;
144   }
145
146   public boolean isCoalesce()
147   {
148     return coalesce;
149   }
150
151   public void addActionListener(ActionListener listener)
152   {
153     listenerList.add (ActionListener.class, listener);
154   }
155   
156   public void removeActionListener(ActionListener listener)
157   {
158     listenerList.remove (ActionListener.class, listener);
159   }
160
161   /**
162    * @since 1.3
163    */
164   public EventListener[] getListeners (Class listenerType)
165   {
166     return listenerList.getListeners (listenerType);
167   }
168   
169   /**
170    * @since 1.4
171    */
172   public ActionListener[] getActionListeners ()
173   {
174     return (ActionListener[]) listenerList.getListeners (ActionListener.class);
175   }
176
177   protected void fireActionPerformed (ActionEvent event)
178   {
179     ActionListener[] listeners = getActionListeners();
180     
181     for (int i = 0; i < listeners.length; i++)
182       {
183         listeners [i].actionPerformed (event);
184       }
185   }
186
187   void fireActionPerformed ()
188   {
189     fireActionPerformed (new ActionEvent (this, ticks++, "Timer"));
190   }
191
192   public static void setLogTimers(boolean lt)
193   {
194     logTimers = lt;
195   }
196
197   public static boolean getLogTimers()
198   {
199     return logTimers;
200   }
201     
202   public void setDelay(int d)
203   {
204     delay = d;
205   }
206
207   public int getDelay()
208   {
209     return delay;
210   }
211
212   public void setInitialDelay(int i)
213   {
214     initialDelay = i;
215   }
216
217   public int getInitialDelay()
218   {
219     return initialDelay;
220   }
221
222   public void setRepeats(boolean r)
223   {
224     repeats = r;
225   }
226
227   public boolean isRepeats()
228   {
229     return repeats;
230   }
231
232   public boolean isRunning()
233   {
234     return running;
235   }
236
237   public void start()
238   {
239     if (isRunning())
240       {
241         System.err.println("attempt to start a running timer");
242         return;
243       }
244     new Waker().start();
245   }
246
247   public void restart()
248   {
249     synchronized (queueLock)
250       {
251         queue = 0;
252       }
253     start();
254   }
255
256   public void stop()
257   {
258     running = false;
259   }
260 }