home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / TickTock.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  1.8 KB  |  77 lines

  1.  
  2. package sunw.demo.misc;
  3.  
  4. // TickTock is an invisible bean that simply fires a property
  5. // change event at some regular specified interval.
  6.  
  7. import java.beans.*;
  8. import java.util.Date;
  9.  
  10. public class TickTock implements Runnable, java.io.Serializable {
  11.  
  12.     public TickTock() {
  13.     reset();
  14.     }
  15.  
  16.     private void reset() {
  17.     runner = new Thread(this);
  18.     runner.start();
  19.     }
  20.  
  21.     public int getSeconds() {
  22.     return (int)((new Date()).getTime()/1000);
  23.     }
  24.  
  25.     public int getInterval() {
  26.     return interval;
  27.     }
  28.  
  29.     public void setInterval(int seconds) {
  30.     interval = seconds;
  31.     if (runner != null) {
  32.         runner.interrupt();
  33.     }
  34.     }
  35.  
  36.     public void run() {
  37.      int oldSeconds = getSeconds();
  38.     for (;;) {
  39.         try {
  40.         Thread.sleep(interval * 1000);
  41.         } catch (InterruptedException ex) {
  42.         // Just drop through.
  43.         }
  44.         int newSeconds = getSeconds();
  45.         changes.firePropertyChange("seconds", new Integer(oldSeconds),
  46.                            new Integer(newSeconds));
  47.         oldSeconds = newSeconds;
  48.     }
  49.     }
  50.     
  51.     //----------------------------------------------------------------------
  52.     // Methods for registering listeners:
  53.  
  54.     public void addPropertyChangeListener(PropertyChangeListener l) {
  55.     changes.addPropertyChangeListener(l);
  56.     }
  57.  
  58.     public void removePropertyChangeListener(PropertyChangeListener l) {
  59.     changes.removePropertyChangeListener(l);
  60.     }
  61.  
  62.     //----------------------------------------------------------------------
  63.     // Support for serialization
  64.  
  65.     private void readObject(java.io.ObjectInputStream s)
  66.                 throws java.lang.ClassNotFoundException,
  67.                    java.io.IOException {
  68.     s.defaultReadObject();
  69.     // Compensate for missing constructor.
  70.     reset();
  71.     }
  72.  
  73.     private PropertyChangeSupport changes = new PropertyChangeSupport(this);
  74.     private int interval = 5;
  75.     transient Thread runner;
  76. }
  77.