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 |
|
9 |
/** |
10 |
* This is DataComponent specifically for |
11 |
* displaying CPU percentages. |
12 |
* |
13 |
* It uses the JProgressBar to display the value. |
14 |
* |
15 |
* @author $Author: ajm4 $ |
16 |
* @version $Id: CPUDataComponent.java,v 1.3 2001/01/24 01:54:43 ajm4 Exp $ |
17 |
*/ |
18 |
public class CPUDataComponent extends VisibleDataComponent implements DataComponent { |
19 |
|
20 |
//---FINAL ATTRIBUTES--- |
21 |
|
22 |
/** |
23 |
* The current CVS revision of this class |
24 |
*/ |
25 |
public final String REVISION = "$Revision: 1.3 $"; |
26 |
|
27 |
//---STATIC METHODS--- |
28 |
|
29 |
//---CONSTRUCTORS--- |
30 |
|
31 |
/** |
32 |
* Creates the component with a friendly name to be |
33 |
* used as label |
34 |
* |
35 |
* @param name the friendly name |
36 |
* @param attribute the data attribute we look after |
37 |
*/ |
38 |
public CPUDataComponent(String name, String attribute) { |
39 |
_name = name; |
40 |
_attribute = attribute; |
41 |
_label = new JLabel(_name + ": "); |
42 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
43 |
setLayout(new GridLayout(1, 2)); |
44 |
add(_label); |
45 |
add(_item); |
46 |
} |
47 |
|
48 |
//---PUBLIC METHODS--- |
49 |
|
50 |
/** |
51 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
52 |
* method to provide clean logging (every class should have this). |
53 |
* |
54 |
* @return the name of this class and its CVS revision |
55 |
*/ |
56 |
public String toString() { |
57 |
return _name + "(" + _attribute + ")"; |
58 |
} |
59 |
|
60 |
//---PRIVATE METHODS--- |
61 |
|
62 |
//---ACCESSOR/MUTATOR METHODS--- |
63 |
|
64 |
/** |
65 |
* This takes the String value of the parameter that this component |
66 |
* is monitoring direct from the packet, it then performs all |
67 |
* approriate conversions and displays the data. |
68 |
* |
69 |
* @param value the value for this data component |
70 |
* @throws DataFormatException if there was a problem converting the data for display |
71 |
*/ |
72 |
public void setValue(String value) throws DataFormatException { |
73 |
try { |
74 |
if(!_cache.equals(value)) { |
75 |
_cache = value; |
76 |
_item.setString(value + "%"); |
77 |
_item.setValue(new Double(value).intValue()); |
78 |
} |
79 |
} catch (Exception e) { |
80 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
81 |
} |
82 |
} |
83 |
|
84 |
//---ATTRIBUTES--- |
85 |
|
86 |
/** |
87 |
* The friendly name of this component |
88 |
*/ |
89 |
private String _name; |
90 |
|
91 |
/** |
92 |
* The attribute that this component is concerned with |
93 |
*/ |
94 |
private String _attribute; |
95 |
|
96 |
/** |
97 |
* Remebers what the last value was, so we |
98 |
* only update if we have to. |
99 |
*/ |
100 |
String _cache = ""; |
101 |
|
102 |
/** |
103 |
* The minimum value for the percentage |
104 |
*/ |
105 |
private final int _min = 0; |
106 |
|
107 |
/** |
108 |
* The maximum value for the percentage |
109 |
*/ |
110 |
private final int _max = 100; |
111 |
|
112 |
/** |
113 |
* The friendly label for this component |
114 |
*/ |
115 |
private JLabel _label; |
116 |
|
117 |
/** |
118 |
* The progress bar that we will display CPU |
119 |
* percentage data in |
120 |
*/ |
121 |
private JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
122 |
{ |
123 |
_item.setStringPainted(true); |
124 |
} |
125 |
|
126 |
//---STATIC ATTRIBUTES--- |
127 |
|
128 |
} |