1 7 8 9 10 package javax.swing; 11 12 13 14 import java.util.*; 15 import java.awt.*; 16 import java.awt.event.*; 17 import java.io.Serializable ; 18 import javax.swing.event.EventListenerList ; 19 20 21 22 127 public class Timer implements Serializable 128 { 129 protected EventListenerList listenerList = new EventListenerList (); 130 131 private boolean notify = false; 145 146 int initialDelay, delay; 147 boolean repeats = true, coalesce = true; 148 149 Runnable doPostEvent = null; 150 151 private static boolean logTimers; 152 153 154 long expirationTime; 158 Timer nextTimer; 159 boolean running; 160 161 162 176 public Timer(int delay, ActionListener listener) { 177 super(); 178 this.delay = delay; 179 this.initialDelay = delay; 180 181 doPostEvent = new DoPostEvent(); 182 183 if (listener != null) { 184 addActionListener(listener); 185 } 186 } 187 188 189 194 class DoPostEvent implements Runnable , Serializable 195 { 196 public void run() { 197 if (logTimers) { 198 System.out.println("Timer ringing: " + Timer.this); 199 } 200 if(notify) { 201 fireActionPerformed(new ActionEvent(Timer.this, 0, null, 202 System.currentTimeMillis(), 203 0)); 204 if (coalesce) { 205 cancelEvent(); 206 } 207 } 208 } 209 210 Timer getTimer() { 211 return Timer.this; 212 } 213 } 214 215 216 223 public void addActionListener(ActionListener listener) { 224 listenerList.add(ActionListener.class, listener); 225 } 226 227 228 233 public void removeActionListener(ActionListener listener) { 234 listenerList.remove(ActionListener.class, listener); 235 } 236 237 238 250 public ActionListener[] getActionListeners() { 251 return (ActionListener[])listenerList.getListeners( 252 ActionListener.class); 253 } 254 255 256 263 protected void fireActionPerformed(ActionEvent e) { 264 Object [] listeners = listenerList.getListenerList(); 266 267 for (int i=listeners.length-2; i>=0; i-=2) { 270 if (listeners[i]==ActionListener.class) { 271 ((ActionListener)listeners[i+1]).actionPerformed(e); 272 } 273 } 274 } 275 276 313 public <T extends EventListener> T[] getListeners(Class <T> listenerType) { 314 return listenerList.getListeners(listenerType); 315 } 316 317 320 TimerQueue timerQueue() { 321 return TimerQueue.sharedInstance(); 322 } 323 324 325 332 public static void setLogTimers(boolean flag) { 333 logTimers = flag; 334 } 335 336 337 343 public static boolean getLogTimers() { 344 return logTimers; 345 } 346 347 348 355 public void setDelay(int delay) { 356 if (delay < 0) { 357 throw new IllegalArgumentException ("Invalid delay: " + delay); 358 } 359 else { 360 this.delay = delay; 361 } 362 } 363 364 365 372 public int getDelay() { 373 return delay; 374 } 375 376 377 391 public void setInitialDelay(int initialDelay) { 392 if (initialDelay < 0) { 393 throw new IllegalArgumentException ("Invalid initial delay: " + 394 initialDelay); 395 } 396 else { 397 this.initialDelay = initialDelay; 398 } 399 } 400 401 402 408 public int getInitialDelay() { 409 return initialDelay; 410 } 411 412 413 421 public void setRepeats(boolean flag) { 422 repeats = flag; 423 } 424 425 426 434 public boolean isRepeats() { 435 return repeats; 436 } 437 438 439 455 public void setCoalesce(boolean flag) { 456 boolean old = coalesce; 457 coalesce = flag; 458 if (!old && coalesce) { 459 cancelEvent(); 463 } 464 } 465 466 467 473 public boolean isCoalesce() { 474 return coalesce; 475 } 476 477 478 485 public void start() { 486 timerQueue().addTimer(this, 487 System.currentTimeMillis() + getInitialDelay()); 488 } 489 490 491 496 public boolean isRunning() { 497 return timerQueue().containsTimer(this); 498 } 499 500 501 508 public void stop() { 509 timerQueue().removeTimer(this); 510 cancelEvent(); 511 } 512 513 514 519 public void restart() { 520 stop(); 521 start(); 522 } 523 524 525 530 synchronized void cancelEvent() { 531 notify = false; 532 } 533 534 535 synchronized void post() { 536 if (notify == false || !coalesce) { 537 notify = true; 538 SwingUtilities.invokeLater(doPostEvent); 539 } 540 } 541 } 542 | Popular Tags |