1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package uk.ac.ukc.iscream.conient.datacomponents; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import javax.swing.JLabel; |
6 |
|
import java.awt.GridLayout; |
7 |
|
import javax.swing.JProgressBar; |
8 |
+ |
import javax.swing.SwingUtilities; |
9 |
|
|
10 |
|
/** |
11 |
|
* This is DataComponent specifically for |
16 |
|
* @author $Author$ |
17 |
|
* @version $Id$ |
18 |
|
*/ |
19 |
< |
public class CPUDataComponent extends DataComponent { |
19 |
> |
public class CPUDataComponent extends VisibleDataComponent { |
20 |
|
|
21 |
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
34 |
|
* used as label |
35 |
|
* |
36 |
|
* @param name the friendly name |
37 |
+ |
* @param attribute the data attribute we look after |
38 |
|
*/ |
39 |
< |
public CPUDataComponent(String name) { |
40 |
< |
_label = new JLabel(name + ": "); |
39 |
> |
public CPUDataComponent(String name, String attribute) { |
40 |
> |
_name = name; |
41 |
> |
_attribute = attribute; |
42 |
> |
_label = new JLabel(_name + ": "); |
43 |
|
_label.setHorizontalAlignment(JLabel.RIGHT); |
44 |
|
setLayout(new GridLayout(1, 2)); |
45 |
+ |
_item.setString("-uninitialised-"); |
46 |
|
add(_label); |
47 |
|
add(_item); |
48 |
|
} |
49 |
|
|
50 |
|
//---PUBLIC METHODS--- |
51 |
|
|
52 |
+ |
/** |
53 |
+ |
* 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 |
+ |
* 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 |
|
//---PRIVATE METHODS--- |
74 |
|
|
75 |
|
//---ACCESSOR/MUTATOR METHODS--- |
76 |
|
|
77 |
|
/** |
78 |
< |
* This takes the String value of the parammeter that this component |
78 |
> |
* This takes the String value of the parameter that this component |
79 |
|
* is monitoring direct from the packet, it then performs all |
80 |
< |
* approriate conversions and displays the data. |
80 |
> |
* approriate conversions and adds this class to the Swing Event |
81 |
> |
* Dispatching queue. |
82 |
|
* |
83 |
|
* @param value the value for this data component |
84 |
+ |
* @throws DataFormatException if there was a problem converting the data for display |
85 |
|
*/ |
86 |
|
public void setValue(String value) throws DataFormatException { |
87 |
|
try { |
88 |
< |
_item.setString(value + "%"); |
89 |
< |
_item.setValue(new Double(value).intValue()); |
88 |
> |
if(!_cache.equals(value)) { |
89 |
> |
_cache = value; |
90 |
> |
SwingUtilities.invokeLater(this); |
91 |
> |
} |
92 |
|
} catch (Exception e) { |
93 |
< |
throw new DataFormatException("invalid data type for component"); |
93 |
> |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
94 |
|
} |
95 |
|
} |
96 |
|
|
97 |
|
//---ATTRIBUTES--- |
98 |
|
|
99 |
+ |
/** |
100 |
+ |
* The friendly name of this component |
101 |
+ |
*/ |
102 |
+ |
private String _name; |
103 |
+ |
|
104 |
+ |
/** |
105 |
+ |
* The attribute that this component is concerned with |
106 |
+ |
*/ |
107 |
+ |
private String _attribute; |
108 |
+ |
|
109 |
+ |
/** |
110 |
+ |
* Remebers what the last value was, so we |
111 |
+ |
* only update if we have to. |
112 |
+ |
*/ |
113 |
+ |
String _cache = ""; |
114 |
+ |
|
115 |
+ |
/** |
116 |
+ |
* The minimum value for the percentage |
117 |
+ |
*/ |
118 |
|
private final int _min = 0; |
119 |
+ |
|
120 |
+ |
/** |
121 |
+ |
* The maximum value for the percentage |
122 |
+ |
*/ |
123 |
|
private final int _max = 100; |
124 |
+ |
|
125 |
+ |
/** |
126 |
+ |
* The friendly label for this component |
127 |
+ |
*/ |
128 |
|
private JLabel _label; |
129 |
+ |
|
130 |
+ |
/** |
131 |
+ |
* The progress bar that we will display CPU |
132 |
+ |
* percentage data in |
133 |
+ |
*/ |
134 |
|
private JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
135 |
|
{ |
136 |
|
_item.setStringPainted(true); |
137 |
|
} |
138 |
< |
} |
138 |
> |
|
139 |
> |
//---STATIC ATTRIBUTES--- |
140 |
> |
|
141 |
> |
} |