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