1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.8 |
package uk.org.iscream.conient.datacomponents; |
3 |
ajm |
1.1 |
|
4 |
|
|
//---IMPORTS--- |
5 |
|
|
import javax.swing.JLabel; |
6 |
|
|
import java.awt.GridLayout; |
7 |
|
|
import javax.swing.JProgressBar; |
8 |
ajm |
1.5 |
import javax.swing.SwingUtilities; |
9 |
ajm |
1.1 |
|
10 |
|
|
/** |
11 |
|
|
* This is DataComponent specifically for |
12 |
|
|
* displaying CPU percentages. |
13 |
|
|
* |
14 |
|
|
* It uses the JProgressBar to display the value. |
15 |
|
|
* |
16 |
|
|
* @author $Author: ajm4 $ |
17 |
ajm |
1.8 |
* @version $Id: CPUDataComponent.java,v 1.7 2001/02/04 00:06:17 ajm4 Exp $ |
18 |
ajm |
1.1 |
*/ |
19 |
ajm |
1.5 |
public class CPUDataComponent extends VisibleDataComponent { |
20 |
ajm |
1.1 |
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.8 |
public final String REVISION = "$Revision: 1.7 $"; |
27 |
ajm |
1.1 |
|
28 |
|
|
//---STATIC METHODS--- |
29 |
|
|
|
30 |
|
|
//---CONSTRUCTORS--- |
31 |
|
|
|
32 |
|
|
/** |
33 |
|
|
* Creates the component with a friendly name to be |
34 |
|
|
* used as label |
35 |
|
|
* |
36 |
|
|
* @param name the friendly name |
37 |
ajm |
1.3 |
* @param attribute the data attribute we look after |
38 |
ajm |
1.1 |
*/ |
39 |
ajm |
1.3 |
public CPUDataComponent(String name, String attribute) { |
40 |
|
|
_name = name; |
41 |
|
|
_attribute = attribute; |
42 |
|
|
_label = new JLabel(_name + ": "); |
43 |
ajm |
1.1 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
44 |
|
|
setLayout(new GridLayout(1, 2)); |
45 |
ajm |
1.7 |
_item.setString("-uninitialised-"); |
46 |
ajm |
1.1 |
add(_label); |
47 |
|
|
add(_item); |
48 |
|
|
} |
49 |
|
|
|
50 |
|
|
//---PUBLIC METHODS--- |
51 |
|
|
|
52 |
ajm |
1.3 |
/** |
53 |
ajm |
1.5 |
* This run method updates any Swing components |
54 |
|
|
* The setValue() method adds this component |
55 |
|
|
* to the Swing Event Dispatching Queue to |
56 |
|
|
* run this method. |
57 |
|
|
*/ |
58 |
|
|
public void run() { |
59 |
|
|
_item.setString(_cache + "%"); |
60 |
|
|
_item.setValue(new Double(_cache).intValue()); |
61 |
|
|
} |
62 |
|
|
|
63 |
|
|
/** |
64 |
ajm |
1.3 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
65 |
|
|
* method to provide clean logging (every class should have this). |
66 |
|
|
* |
67 |
|
|
* @return the name of this class and its CVS revision |
68 |
|
|
*/ |
69 |
|
|
public String toString() { |
70 |
|
|
return _name + "(" + _attribute + ")"; |
71 |
|
|
} |
72 |
|
|
|
73 |
ajm |
1.1 |
//---PRIVATE METHODS--- |
74 |
|
|
|
75 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
76 |
|
|
|
77 |
|
|
/** |
78 |
ajm |
1.2 |
* This takes the String value of the parameter that this component |
79 |
ajm |
1.1 |
* is monitoring direct from the packet, it then performs all |
80 |
ajm |
1.5 |
* approriate conversions and adds this class to the Swing Event |
81 |
|
|
* Dispatching queue. |
82 |
ajm |
1.1 |
* |
83 |
|
|
* @param value the value for this data component |
84 |
ajm |
1.2 |
* @throws DataFormatException if there was a problem converting the data for display |
85 |
ajm |
1.1 |
*/ |
86 |
|
|
public void setValue(String value) throws DataFormatException { |
87 |
|
|
try { |
88 |
ajm |
1.3 |
if(!_cache.equals(value)) { |
89 |
|
|
_cache = value; |
90 |
ajm |
1.5 |
SwingUtilities.invokeLater(this); |
91 |
ajm |
1.3 |
} |
92 |
ajm |
1.1 |
} catch (Exception e) { |
93 |
ajm |
1.3 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
94 |
ajm |
1.1 |
} |
95 |
ajm |
1.7 |
} |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Returns the string showing the packet |
99 |
|
|
* attribute that the component is looking after |
100 |
|
|
* |
101 |
|
|
* @return the packet reference |
102 |
|
|
*/ |
103 |
|
|
public String getPacketAttribute() { |
104 |
|
|
return _attribute; |
105 |
|
|
} |
106 |
ajm |
1.1 |
|
107 |
|
|
//---ATTRIBUTES--- |
108 |
|
|
|
109 |
ajm |
1.3 |
/** |
110 |
|
|
* The friendly name of this component |
111 |
|
|
*/ |
112 |
|
|
private String _name; |
113 |
|
|
|
114 |
|
|
/** |
115 |
|
|
* The attribute that this component is concerned with |
116 |
|
|
*/ |
117 |
|
|
private String _attribute; |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Remebers what the last value was, so we |
121 |
|
|
* only update if we have to. |
122 |
|
|
*/ |
123 |
|
|
String _cache = ""; |
124 |
|
|
|
125 |
ajm |
1.2 |
/** |
126 |
|
|
* The minimum value for the percentage |
127 |
|
|
*/ |
128 |
ajm |
1.1 |
private final int _min = 0; |
129 |
ajm |
1.2 |
|
130 |
|
|
/** |
131 |
|
|
* The maximum value for the percentage |
132 |
|
|
*/ |
133 |
ajm |
1.1 |
private final int _max = 100; |
134 |
ajm |
1.2 |
|
135 |
|
|
/** |
136 |
|
|
* The friendly label for this component |
137 |
|
|
*/ |
138 |
ajm |
1.1 |
private JLabel _label; |
139 |
ajm |
1.2 |
|
140 |
|
|
/** |
141 |
|
|
* The progress bar that we will display CPU |
142 |
|
|
* percentage data in |
143 |
|
|
*/ |
144 |
ajm |
1.1 |
private JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
145 |
|
|
{ |
146 |
|
|
_item.setStringPainted(true); |
147 |
|
|
} |
148 |
ajm |
1.2 |
|
149 |
|
|
//---STATIC ATTRIBUTES--- |
150 |
|
|
|
151 |
ajm |
1.5 |
} |