Statistics
| Revision:

root / branches / v2_0_0_prep / frameworks / _fwAndami / src / org / gvsig / andami / ui / splash / Timer.java @ 29593

History | View | Annotate | Download (2.89 KB)

1
package org.gvsig.andami.ui.splash;
2
/**
3
 * This class implements an interval timer. It calls
4
 * the tick method in the callback interface after
5
 * a fixed number of milliseconds (indicated by the
6
 * interval variable). It measures the amount of time spent
7
 * in the tick method and adjusts for it.
8
 * To start up a timer with this class, create it with
9
 * a callback and the number of milliseconds in the interval
10
 * and then call the start method:
11
 * <PRE>
12
 *
13
 *    Timer timer = new Timer(this, 2000);
14
        // 2 second interval
15
 *    timer.start();
16
 *
17
 * </PRE>
18
 *
19
 * @author Mark Wutka
20
 */
21

    
22
public class Timer extends Object implements Runnable
23
{
24

    
25
     protected Thread timerThread;
26

    
27
/** The number of milliseconds in the interval*/
28
     protected long interval;
29

    
30
/** The callback interface containing the tick method */
31
     protected TimerCallBack callback;
32

    
33
     public Timer()
34
     {
35
     }
36

    
37
     public Timer(TimerCallBack callback)
38
     {
39
          this.callback = callback;
40
     }
41

    
42
     public Timer(long interval)
43
     {
44
          this.interval = interval;
45
     }
46

    
47
     public Timer(TimerCallBack callback, long interval)
48
     {
49
          this.callback = callback;
50
          this.interval = interval;
51
     }
52

    
53
/** returns the number of milliseconds in the interval */
54
     public long getInterval()
55
     {
56
          return interval;
57
     }
58

    
59
/** sets the number of milliseconds in the interval
60
 * @param newInterval the new number of milliseconds
61
 */
62
     public void setInterval(long newInterval)
63
     {
64
          interval = newInterval;
65
     }
66

    
67
/** returns the callback interface */
68
     public TimerCallBack getCallback()
69
     {
70
          return callback;
71
     }
72

    
73
/** changes the callback interface
74
 * @param callback the new callback
75
 */
76
     public void setCallback(TimerCallBack callback)
77
     {
78
          this.callback = callback;
79
     }
80

    
81
/** starts the timer */
82
     public void start()
83
     {
84
          timerThread = new Thread(this);
85
          timerThread.start();
86
     }
87

    
88
/** stops the timer */
89
     public void stop()
90
     {
91
              timerThread.interrupt();
92
              timerThread = null;
93
     }
94

    
95
     public void run()
96
     {
97
          while (true)
98
          {
99
// Check the current time
100
               long startTime = System.currentTimeMillis();
101

    
102
// If there is a callback, call it
103
               if (callback != null)
104
               {
105
                    callback.tick();
106
               }
107

    
108
// Check the time again
109
               long endTime = System.currentTimeMillis();
110

    
111
// The amount of time to sleep is the interval minus the time spent
112
// in the tick routine
113
               long sleepTime = interval - (endTime - startTime);
114

    
115
// If you've passed the next interval, hurry up and call the next tick
116
               if (sleepTime <= 0) continue;
117

    
118
               try {
119
                      Thread.sleep(sleepTime);
120
               } catch (Exception insomnia) {
121
               } catch (Throwable insomnia) {
122
               }
123
          }
124
     }
125
}
126