1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.conient; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import java.awt.Color; |
26 |
import javax.swing.JProgressBar; |
27 |
import javax.swing.JPanel; |
28 |
import javax.swing.JLabel; |
29 |
import javax.swing.BoxLayout; |
30 |
import javax.swing.SwingUtilities; |
31 |
|
32 |
/** |
33 |
* Shows a countdown timer for packet arrival times. |
34 |
* When constructed it takes an integer value relating |
35 |
* to the time in seconds between recieving packets. |
36 |
* |
37 |
* This then displays a JProgress bar which updates at |
38 |
* 10th of a second intervals. |
39 |
* |
40 |
* @author $Author: tdb $ |
41 |
* @version $Id: PacketTimer.java,v 1.8 2003/03/06 12:15:52 tdb Exp $ |
42 |
*/ |
43 |
public class PacketTimer extends JPanel implements Runnable { |
44 |
|
45 |
//---FINAL ATTRIBUTES--- |
46 |
|
47 |
/** |
48 |
* The current CVS revision of this class |
49 |
*/ |
50 |
public static final String REVISION = "$Revision: 1.8 $"; |
51 |
|
52 |
//---STATIC METHODS--- |
53 |
|
54 |
//---CONSTRUCTORS--- |
55 |
|
56 |
/** |
57 |
* Constructs a new PacketTimer |
58 |
* |
59 |
* @param timeBetweenPackets the time in seconds between packet arrival |
60 |
*/ |
61 |
public PacketTimer(String name, int timeBetweenPackets) { |
62 |
_timeBetweenPackets = timeBetweenPackets * 1000; |
63 |
_progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, _timeBetweenPackets); |
64 |
_barColour = _progressBar.getForeground(); |
65 |
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
66 |
JLabel label = new JLabel(name); |
67 |
this.add(_progressBar); |
68 |
this.add(label); |
69 |
} |
70 |
|
71 |
//---PUBLIC METHODS--- |
72 |
|
73 |
/** |
74 |
* Updates the progress bar every 10th of a second. |
75 |
*/ |
76 |
public void run() { |
77 |
while(_running) { |
78 |
try { |
79 |
Thread.sleep(100); |
80 |
} catch (InterruptedException e) { |
81 |
} |
82 |
|
83 |
SwingUtilities.invokeLater(new Runnable() { |
84 |
public void run() { |
85 |
long t = _nextTime - System.currentTimeMillis(); |
86 |
if(t >= 0) { |
87 |
_progressBar.setValue((int) t); |
88 |
} |
89 |
else { |
90 |
_progressBar.setValue((int) (t*-1)); |
91 |
_progressBar.setForeground(Color.red); |
92 |
} |
93 |
} |
94 |
}); |
95 |
} |
96 |
} |
97 |
|
98 |
/** |
99 |
* Resets the progress bar. |
100 |
*/ |
101 |
public void reset() { |
102 |
_nextTime = System.currentTimeMillis() + _timeBetweenPackets; |
103 |
SwingUtilities.invokeLater(new Runnable() { |
104 |
public void run() { |
105 |
_progressBar.setValue(_timeBetweenPackets); |
106 |
_progressBar.setForeground(_barColour); |
107 |
} |
108 |
}); |
109 |
} |
110 |
|
111 |
//---PRIVATE METHODS--- |
112 |
|
113 |
//---ACCESSOR/MUTATOR METHODS--- |
114 |
|
115 |
//---ATTRIBUTES--- |
116 |
|
117 |
/** |
118 |
* Holds the time between packets as used internally by this class |
119 |
*/ |
120 |
private int _timeBetweenPackets; |
121 |
private long _nextTime; |
122 |
private Color _barColour; |
123 |
|
124 |
/** |
125 |
* Used to indicate the state of the thread |
126 |
*/ |
127 |
private boolean _running = true; |
128 |
|
129 |
private JProgressBar _progressBar; |
130 |
|
131 |
//---STATIC ATTRIBUTES--- |
132 |
|
133 |
} |