KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > planetamessenger > system > JTimer


1 /*
2     =========================================================================
3     Package system - System routines.
4
5     This module is developed and maintained by PlanetaMessenger.org.
6     Specs, New and updated versions can be found in
7     http://www.planetamessenger.org
8     If you want contact the Team please send a email to Project Manager
9     Leidson Campos Alves Ferreira at leidson@planetamessenger.org
10
11     Copyright (C) since 2001 by PlanetaMessenger.org
12     
13     This library is free software; you can redistribute it and/or
14     modify it under the terms of the GNU Lesser General Public
15     License as published by the Free Software Foundation; either
16     version 2.1 of the License, or (at your option) any later version.
17
18     This library is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21     Lesser General Public License for more details.
22
23     You should have received a copy of the GNU Lesser General Public
24     License along with this library; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
27     =========================================================================
28 */

29 /**
30  *
31  * $Id: JTimer.java,v 1.8 2007/01/28 17:39:22 popolony2k Exp $
32  * $Author: popolony2k $
33  * $Name: $
34  * $Revision: 1.8 $
35  * $State: Exp $
36  *
37  */

38
39 package org.planetamessenger.system;
40
41 import java.util.*;
42 import java.awt.event.*;
43
44
45 public class JTimer implements Runnable JavaDoc {
46
47   private Thread JavaDoc timerThread = null;
48   private ArrayList<JTimerListener> timerListener = null;
49   private long nIntervalMilli = 1000;
50   private boolean bStopped;
51   
52   
53   
54   // Public Declarations
55
/**
56    * Constructor. Initializes all class
57    * data.
58    */

59   public JTimer() {
60   
61     init();
62   }
63   
64   /**
65    * Constructor. Initializes all class data
66    * and constructs the object changing the
67    * default timer interval.
68    * @param nIntervalMilli The new timer interval
69    * in milliseconds;
70    * @param listener The listener that will be
71    * attached in this class.
72    */

73   public JTimer( int nIntervalMilli, JTimerListener listener ) {
74   
75     this.nIntervalMilli = nIntervalMilli;
76     init();
77     timerListener.add( listener );
78   }
79
80   /**
81     * Performs the general class data
82     * Initialization.
83     */

84   void init() {
85     
86     bStopped = true;
87     timerListener = new ArrayList<JTimerListener>();
88     timerThread = new Thread JavaDoc( this );
89   }
90
91   /**
92    * Notify the the main thread to unlock
93    * the waiting object synchro.
94    */

95   private synchronized void notifyMainThread() {
96
97     try {
98       System.err.println( "JTimer.notifyMainThread() - Normal Thread exit." );
99       notifyAll();
100     } catch( java.lang.IllegalMonitorStateException JavaDoc me ) {
101       System.err.println( "JTimer.notifyMainThread() - " + me );
102     }
103   }
104   
105   /**
106    * Dispatch the actionPerfomed event
107    * through listener.
108    */

109   private void fireActionPerformed() {
110   
111     ActionEvent evt = new ActionEvent( this, ActionEvent.ACTION_PERFORMED, "JTimer" );
112     ArrayList<JTimerListener> clone;
113
114     
115     synchronized( this ) {
116       clone = ( ArrayList<JTimerListener> ) timerListener.clone();
117     }
118         
119     // Dispatch the event through the listener
120
if( clone.size() > 0 ) {
121       for( int nCount = 0; nCount < clone.size(); nCount++ )
122         ( ( JTimerListener ) clone.get( nCount ) ).actionPerformed( evt );
123     }
124   }
125
126   /**
127    * Set the timer interval to
128    * throws a timer event.
129    * @param nInterval Event interval;
130    */

131   public void setInterval( long nInterval ) {
132   
133     nIntervalMilli = nInterval;
134   }
135
136   /**
137    * Gets the Event interval of
138    * this class.
139    */

140   public long getInterval() {
141   
142     return nIntervalMilli;
143   }
144
145   /**
146    * Activate or deactivate the timer.
147    * @param nStatus Status of this timer;
148    */

149   public synchronized void setActive( boolean bStatus ) {
150   
151     if( bStatus ) {
152       if( !timerThread.isAlive() ) {
153         bStopped = false;
154         timerThread.start();
155       }
156     }
157     else {
158       if( timerThread.isAlive() ) {
159         timerThread.interrupt();
160         bStopped = true;
161           
162         try {
163           wait();
164           System.err.println( "JTimer.setActive( false ) performed" );
165         } catch( java.lang.InterruptedException JavaDoc ie ) {
166           System.err.println( "JTimer.setActive( false ) - " + ie );
167         } catch( java.lang.IllegalMonitorStateException JavaDoc me ) {
168           System.err.println( "JTimer.setActive( false ) - " + me );
169         }
170       }
171     }
172   }
173
174   /**
175    * Gets the Timer status;
176    */

177   public boolean getActive() {
178   
179     boolean bRet;
180     
181    
182     bRet = !timerThread.isInterrupted();
183     
184     return bRet;
185   }
186
187   /**
188    * Start the timer. Created only to
189    * compability with the swing timer.
190    */

191   public void start() {
192     
193     setActive( true );
194   }
195
196   /**
197    * Stop the timer. Created only to
198    * compatibility with the swing timer.
199    */

200   public void stop() {
201    
202     setActive( false );
203   }
204
205   /**
206    * Set the timer listener to this class.
207    * @param listener The new timer listener;
208    */

209   public synchronized void addTimerListener( JTimerListener listener ) {
210     
211     timerListener.add( listener );
212   }
213     
214   public synchronized void removeTimerListener( JTimerListener listener ) {
215   /**
216    * Removes the timer listener of this
217    * class.
218    * @param listener The timer listener that will
219    * be removed;
220    */

221   
222     timerListener.remove( listener );
223   }
224
225   /**
226    * Thread Entry-point. Manages the
227    * Thread status and events.
228    */

229   public void run() {
230
231     while( true ) {
232       try {
233       
234         // Check exit notification
235
if( bStopped )
236           break;
237         
238         Thread.sleep( nIntervalMilli );
239         fireActionPerformed();
240       } catch( InterruptedException JavaDoc e ) {
241         System.err.println( "JTimer.run() - " + e );
242         break;
243       }
244     }
245
246     // Notify the main thread to exit
247
notifyMainThread();
248   }
249 }
250
251 // JTimer class
252
Popular Tags