1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
/** The Timer JavaBean is a nonvisual component that sends an ActionEvent
* to the registered TimerListeners every "delay" property milliseconds.
* It can either send that event only once, or it can cycle (according to
* the "onceOnly" property).
*
* @version 1.01, Sep 02, 1998
*/
public class Timer extends Object
implements java.io.Serializable {
public static final String PROP_ONCE_ONLY = "onceOnly";
public static final String PROP_DELAY = "delay";
public static final long DEFAULT_DELAY = 1000;
public static final boolean DEFAULT_ONLY_ONCE = false;
static final long serialVersionUID =-7954930904657028678L;
/** Creates a new Timer */
public Timer() {
delay = DEFAULT_DELAY;
onceOnly = DEFAULT_ONLY_ONCE;
propertySupport = new PropertyChangeSupport(this);
}
public synchronized void start() {
if (running) return;
timerThread = new TimerThread();
running = true;
timerThread.start();
}
public synchronized void stop() {
if (!running) return;
timerThread.stop();
timerThread = null;
running = false;
}
/** Getter method for the delay property.
* @return Current delay value
*/
public long getDelay() {
return delay;
}
/** Setter method for the delay property.
* @param value New delay value
*/
public void setDelay(long value) {
if (delay == value) return;
long oldValue = delay;
delay = value;
propertySupport.firePropertyChange(PROP_DELAY,
new Long(oldValue),
new Long(delay));
}
/** Getter method for the onceOnly property.
* @return Current onceOnly value
*/
public boolean getOnceOnly() {
return onceOnly;
}
/** Setter method for the onceOnly property.
* @param value New onceOnly value
*/
public void setOnceOnly(boolean value) {
if (onceOnly == value) return;
onceOnly = value;
propertySupport.firePropertyChange(PROP_ONCE_ONLY,
!onceOnly ? Boolean.TRUE : Boolean.FALSE,
onceOnly ? Boolean.TRUE : Boolean.FALSE);
}
public void addPropertyChangeListener(PropertyChangeListener l) {
propertySupport.addPropertyChangeListener(l);
}
public void removePropertyChangeListener(PropertyChangeListener l) {
propertySupport.removePropertyChangeListener(l);
}
public void addTimerListener(TimerListener l) {
if (listeners == null)
listeners = new Vector();
listeners.addElement(l);
}
public void removeTimerListener(TimerListener l) {
if (listeners == null)
return;
listeners.removeElement(l);
}
private void fireTimerEvent() {
if (listeners == null) return;
Vector l;
synchronized (this) {
l = (Vector)listeners.clone();
}
for (Enumeration e = l.elements(); e.hasMoreElements();) {
TimerListener tl = (TimerListener) e.nextElement();
tl.onTime(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "onTime"));
}
}
class TimerThread extends Thread {
public void run() {
while (true) {
try {
sleep(delay);
} catch (InterruptedException e) {}
fireTimerEvent();
if (onceOnly) break;
}
running = false;
}
}
transient private TimerThread timerThread;
/** The timer listeners */
transient private Vector listeners;
/** The support for firing property changes */
private PropertyChangeSupport propertySupport;
/** The flag indicating whether the timer is running */
private boolean running;
/** If true, the timer stops after firing the first onTime, if false
* it keeps ticking until stopped */
private boolean onceOnly;
/** Delay in milliseconds */
private long delay;
} |
Partager