1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.cms.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 |
import uk.org.iscream.cms.server.util.XMLPacket; |
10 |
|
11 |
/** |
12 |
* This is DataComponent specifically for |
13 |
* displaying storage, eg memory, disk and swap. |
14 |
* It displays a value/total type display using a JProgessBar. |
15 |
* |
16 |
* @author $Author: ajm4 $ |
17 |
* @version $Id: StorageDataComponent.java,v 1.3 2001/03/19 01:38:16 ajm4 Exp $ |
18 |
*/ |
19 |
public class StorageDataComponent extends VisibleDataComponent { |
20 |
|
21 |
//---FINAL ATTRIBUTES--- |
22 |
|
23 |
/** |
24 |
* The current CVS revision of this class |
25 |
*/ |
26 |
public final String REVISION = "$Revision: 1.3 $"; |
27 |
|
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 |
* @param attribute the data attribute we look after |
38 |
* @param maxAttribute the data attribute to obtain the maximum value from |
39 |
* @param unit the string representation of the units eg, "Mb" or "Kb" |
40 |
*/ |
41 |
public StorageDataComponent(String name, String attribute, String maxAttribute, String unit) { |
42 |
this(name, attribute, maxAttribute, unit, 1); |
43 |
} |
44 |
|
45 |
/** |
46 |
* Creates the component with a friendly name to be |
47 |
* used as label |
48 |
* |
49 |
* @param name the friendly name |
50 |
* @param attribute the data attribute we look after |
51 |
* @param maxAttribute the data attribute to obtain the maximum value from |
52 |
* @param unit the string representation of the units eg, "Mb" or "Kb" |
53 |
* @param divider the amount that the given value should be multiplied by to reach the given units |
54 |
*/ |
55 |
public StorageDataComponent(String name, String attribute, String maxAttribute, String unit, int divider) { |
56 |
_name = name; |
57 |
_attribute = attribute; |
58 |
_unit = unit; |
59 |
_divider = divider; |
60 |
_maxAttribute = maxAttribute; |
61 |
_label = new JLabel(name + ": "); |
62 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
63 |
setLayout(new GridLayout(1, 2)); |
64 |
_item.setString("-uninitialised-"); |
65 |
add(_label); |
66 |
add(_item); |
67 |
setVisible(false); |
68 |
} |
69 |
|
70 |
//---PUBLIC METHODS--- |
71 |
|
72 |
/** |
73 |
* This run method updates any Swing components |
74 |
* The setValue() method adds this component |
75 |
* to the Swing Event Dispatching Queue to |
76 |
* run this method. |
77 |
*/ |
78 |
public void run() { |
79 |
if(!isVisible()) { |
80 |
setVisible(true); |
81 |
} |
82 |
int max = new Double(_maxvalue).intValue(); |
83 |
_item.setMaximum(max); |
84 |
int value = new Double(_value).intValue(); |
85 |
_item.setValue(max - value); |
86 |
_item.setString((max - value) + _unit + "/" + max + _unit + " (" + new Double(_item.getPercentComplete() * 100).intValue() + "%)"); |
87 |
} |
88 |
|
89 |
/** |
90 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
91 |
* method to provide clean logging (every class should have this). |
92 |
* |
93 |
* @return the name of this class and its CVS revision |
94 |
*/ |
95 |
public String toString() { |
96 |
return _name + "(" + _attribute + ")"; |
97 |
} |
98 |
|
99 |
//---PRIVATE METHODS--- |
100 |
|
101 |
//---ACCESSOR/MUTATOR METHODS--- |
102 |
|
103 |
/** |
104 |
* This takes the packet to obtain the value from, it then performs all |
105 |
* approriate conversions and adds this class to the Swing Event |
106 |
* Dispatching queue. |
107 |
* |
108 |
* @param packet the XMLPacket to get the data from |
109 |
* @throws DataFormatException if there was a problem converting the data for display |
110 |
*/ |
111 |
public void setValue(XMLPacket packet) throws DataFormatException { |
112 |
String value = packet.getParam(_attribute); |
113 |
String maxvalue = packet.getParam(_maxAttribute); |
114 |
try { |
115 |
if((!maxvalue.equals("0")) && (!_cache.equals(value) || !_maxcache.equals(maxvalue))) { |
116 |
_cache = value; |
117 |
_maxcache = maxvalue; |
118 |
_value = Double.parseDouble(_cache) / _divider; |
119 |
_maxvalue = Double.parseDouble(_maxcache) / _divider; |
120 |
SwingUtilities.invokeLater(this); |
121 |
} |
122 |
} catch (Exception e) { |
123 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
124 |
} |
125 |
} |
126 |
|
127 |
/** |
128 |
* Returns the string showing the packet |
129 |
* attribute that the component is looking after |
130 |
* |
131 |
* @return the packet reference |
132 |
*/ |
133 |
public String getPacketAttribute() { |
134 |
return _attribute; |
135 |
} |
136 |
|
137 |
//---ATTRIBUTES--- |
138 |
|
139 |
/** |
140 |
* The friendly name of this component |
141 |
*/ |
142 |
protected String _name; |
143 |
|
144 |
/** |
145 |
* The attribute that this component is concerned with |
146 |
*/ |
147 |
protected String _attribute; |
148 |
|
149 |
/** |
150 |
* The max attribute that this component is concerned with |
151 |
*/ |
152 |
protected String _maxAttribute; |
153 |
|
154 |
/** |
155 |
* The string representation of the units the values |
156 |
* are being measured in. |
157 |
*/ |
158 |
protected String _unit; |
159 |
|
160 |
/** |
161 |
* The divider to multiply the value to |
162 |
* to reach the units. |
163 |
*/ |
164 |
protected int _divider; |
165 |
|
166 |
/** |
167 |
* Remebers what the last value was, so we |
168 |
* only update if we have to. |
169 |
*/ |
170 |
protected String _cache = ""; |
171 |
|
172 |
/** |
173 |
* Remebers what the last value was, so we |
174 |
* only update if we have to. |
175 |
*/ |
176 |
protected String _maxcache = ""; |
177 |
|
178 |
/** |
179 |
* Holds the integer value we are holding |
180 |
*/ |
181 |
protected double _value = 0; |
182 |
|
183 |
/** |
184 |
* Holds the integer value we are holding |
185 |
*/ |
186 |
protected double _maxvalue = 0; |
187 |
|
188 |
/** |
189 |
* The minimum value for the percentage |
190 |
*/ |
191 |
protected final int _min = 0; |
192 |
|
193 |
/** |
194 |
* The maximum value for the percentage |
195 |
*/ |
196 |
protected int _max = 0; |
197 |
|
198 |
/** |
199 |
* The friendly label for this component |
200 |
*/ |
201 |
protected JLabel _label; |
202 |
|
203 |
/** |
204 |
* The progress bar that we will display memory |
205 |
* usage data in |
206 |
*/ |
207 |
protected JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
208 |
{ |
209 |
_item.setStringPainted(true); |
210 |
} |
211 |
|
212 |
//---STATIC ATTRIBUTES--- |
213 |
|
214 |
} |