home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161a.iso / handson / files / copyjava.exe / com / sun / java / swing / Timer.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-02-26  |  4.5 KB  |  183 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.swing.event.EventListenerList;
  4. import java.awt.Toolkit;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.io.Serializable;
  8.  
  9. public class Timer implements Serializable {
  10.    int initialDelay;
  11.    int delay;
  12.    boolean repeats = true;
  13.    boolean coalesce = true;
  14.    private DummyComponent dummyComponent;
  15.    protected EventListenerList listenerList = new EventListenerList();
  16.    TimerEvent queuedEvent;
  17.    static final int TIMER_ID = 2000;
  18.    private static boolean logTimers;
  19.    long expirationTime;
  20.    Timer nextTimer;
  21.    boolean running;
  22.    // $FF: synthetic field
  23.    static Class class$java$awt$event$ActionListener;
  24.  
  25.    public Timer(int var1, ActionListener var2) {
  26.       this.delay = var1;
  27.       this.initialDelay = var1;
  28.       this.dummyComponent = new DummyComponent(this);
  29.       if (var2 != null) {
  30.          this.addActionListener(var2);
  31.       }
  32.  
  33.    }
  34.  
  35.    public void addActionListener(ActionListener var1) {
  36.       this.listenerList.add(class$java$awt$event$ActionListener != null ? class$java$awt$event$ActionListener : (class$java$awt$event$ActionListener = class$("java.awt.event.ActionListener")), var1);
  37.    }
  38.  
  39.    public void removeActionListener(ActionListener var1) {
  40.       this.listenerList.remove(class$java$awt$event$ActionListener != null ? class$java$awt$event$ActionListener : (class$java$awt$event$ActionListener = class$("java.awt.event.ActionListener")), var1);
  41.    }
  42.  
  43.    protected void fireActionPerformed(ActionEvent var1) {
  44.       Object[] var2 = this.listenerList.getListenerList();
  45.  
  46.       for(int var3 = var2.length - 2; var3 >= 0; var3 -= 2) {
  47.          if (var2[var3] == (class$java$awt$event$ActionListener != null ? class$java$awt$event$ActionListener : (class$java$awt$event$ActionListener = class$("java.awt.event.ActionListener")))) {
  48.             ((ActionListener)var2[var3 + 1]).actionPerformed(var1);
  49.          }
  50.       }
  51.  
  52.    }
  53.  
  54.    TimerQueue timerQueue() {
  55.       return TimerQueue.sharedInstance();
  56.    }
  57.  
  58.    public static void setLogTimers(boolean var0) {
  59.       logTimers = var0;
  60.    }
  61.  
  62.    public static boolean getLogTimers() {
  63.       return logTimers;
  64.    }
  65.  
  66.    public void setDelay(int var1) {
  67.       if (var1 < 0) {
  68.          throw new RuntimeException("Invalid initial delay: " + var1);
  69.       } else {
  70.          this.delay = var1;
  71.          if (this.isRunning()) {
  72.             TimerQueue var2 = this.timerQueue();
  73.             var2.removeTimer(this);
  74.             this.cancelEvent();
  75.             var2.addTimer(this, System.currentTimeMillis() + (long)var1);
  76.          }
  77.  
  78.       }
  79.    }
  80.  
  81.    public int getDelay() {
  82.       return this.delay;
  83.    }
  84.  
  85.    public void setInitialDelay(int var1) {
  86.       if (var1 < 0) {
  87.          throw new RuntimeException("Invalid initial delay: " + var1);
  88.       } else {
  89.          this.initialDelay = var1;
  90.       }
  91.    }
  92.  
  93.    public int getInitialDelay() {
  94.       return this.initialDelay;
  95.    }
  96.  
  97.    public void setRepeats(boolean var1) {
  98.       this.repeats = var1;
  99.    }
  100.  
  101.    public boolean isRepeats() {
  102.       return this.repeats;
  103.    }
  104.  
  105.    public void setCoalesce(boolean var1) {
  106.       this.coalesce = var1;
  107.    }
  108.  
  109.    public boolean isCoalesce() {
  110.       return this.coalesce;
  111.    }
  112.  
  113.    public void start() {
  114.       this.timerQueue().addTimer(this, System.currentTimeMillis() + (long)this.getInitialDelay());
  115.    }
  116.  
  117.    public boolean isRunning() {
  118.       return this.timerQueue().containsTimer(this);
  119.    }
  120.  
  121.    public void stop() {
  122.       this.timerQueue().removeTimer(this);
  123.       this.cancelEvent();
  124.    }
  125.  
  126.    public void restart() {
  127.       this.stop();
  128.       this.start();
  129.    }
  130.  
  131.    synchronized void cancelEvent() {
  132.       if (this.queuedEvent != null) {
  133.          this.queuedEvent.setIsCanceled(true);
  134.          this.queuedEvent = null;
  135.       }
  136.  
  137.    }
  138.  
  139.    synchronized void post() {
  140.       if (!this.coalesce || this.queuedEvent == null) {
  141.          this.queuedEvent = new TimerEvent(this, this.dummyComponent, 2000);
  142.          Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(this.queuedEvent);
  143.       }
  144.  
  145.    }
  146.  
  147.    void fire() {
  148.       boolean var1 = false;
  149.       DummyComponent var2 = null;
  150.       TimerEvent var3 = null;
  151.       synchronized(this){}
  152.  
  153.       try {
  154.          if (!this.coalesce || this.queuedEvent == null) {
  155.             var3 = new TimerEvent(this, this.dummyComponent, 2000);
  156.             var2 = this.dummyComponent;
  157.             var1 = true;
  158.          }
  159.       } catch (Throwable var6) {
  160.          throw var6;
  161.       }
  162.  
  163.       if (var1) {
  164.          var2.fireEvent(var3);
  165.       }
  166.  
  167.    }
  168.  
  169.    // $FF: synthetic method
  170.    static Class class$(String var0) {
  171.       try {
  172.          return Class.forName(var0);
  173.       } catch (ClassNotFoundException var2) {
  174.          throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  175.       }
  176.    }
  177.  
  178.    // $FF: synthetic method
  179.    static boolean access$0() {
  180.       return logTimers;
  181.    }
  182. }
  183.