summaryrefslogtreecommitdiff
path: root/javax/swing/Timer.java
blob: f225e8cd9337e3e4ef3ce97a3c544687e0d6e8c2 (plain)
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
package javax.swing;

import java.awt.event.*;
import java.util.*;


public class Timer
{
  int ticks;
  static boolean verbose;
  boolean running;
  boolean repeat_ticks = true;
  long interval, init_delay;
  Vector actions = new Vector();
    
  class Waker extends Thread
  {
    public void run()
    {
      running = true;
      try {
	sleep(init_delay);
		
	while (running)
	  {
	    sleep(interval);

	    if (verbose)
	      {
		System.out.println("javax.swing.Timer -> clocktick");
	      }

	    ticks++;
	    fireActionPerformed();
  
	    if (! repeat_ticks)
	      break;
	  }
	running = false;
      } catch (Exception e) {
	System.out.println("swing.Timer::" + e);
      }
    }
  }

  public void addActionListener(ActionListener listener)
  {
    actions.addElement(listener);
  }
  public void removeActionListener(ActionListener listener)
  {
    actions.removeElement(listener);
  }

  void fireActionPerformed()
  {
    for (int i=0;i<actions.size();i++)
      {
	ActionListener a = (ActionListener) actions.elementAt(i);
	a.actionPerformed(new ActionEvent(this, ticks, "Timer"));
      }
  }
  


  public static void setLogTimers(boolean flag)
  {
    verbose = flag;
  }

  public static boolean getLogTimers()
  {
    return verbose;
  }
    

  public void setDelay(int delay)
  {
    interval = delay;
  }

  public int getDelay()
  {
    return (int)interval;
  }


  public void setInitialDelay(int initialDelay)
  {
    init_delay = initialDelay;
  }

  public void setRepeats(boolean flag)
  {
    repeat_ticks = flag;
  }

  boolean isRunning()
  {
    return running;
  }

  void start()
  {
    if (isRunning())
      {
	System.err.println("attempt to start a running timer");
	return;
      }
    new Waker().start();
  }

  void stop()
  {
    running = false;
  }
}