1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
//---PACKAGE DECLARATION--- |
22 |
package uk.org.iscream.cms.conient.datacomponents; |
23 |
|
24 |
//---IMPORTS--- |
25 |
import javax.swing.JLabel; |
26 |
import java.awt.GridLayout; |
27 |
import javax.swing.JProgressBar; |
28 |
import javax.swing.SwingUtilities; |
29 |
import uk.org.iscream.cms.util.XMLPacket; |
30 |
|
31 |
/** |
32 |
* This is DataComponent specifically for |
33 |
* displaying CPU percentages. |
34 |
* |
35 |
* It uses the JProgressBar to display the value. |
36 |
* |
37 |
* @author $Author: tdb $ |
38 |
* @version $Id: CPUDataComponent.java,v 1.14 2003/02/05 19:35:04 tdb Exp $ |
39 |
*/ |
40 |
public class CPUDataComponent extends VisibleDataComponent { |
41 |
|
42 |
//---FINAL ATTRIBUTES--- |
43 |
|
44 |
/** |
45 |
* The current CVS revision of this class |
46 |
*/ |
47 |
public final String REVISION = "$Revision: 1.14 $"; |
48 |
|
49 |
//---STATIC METHODS--- |
50 |
|
51 |
//---CONSTRUCTORS--- |
52 |
|
53 |
/** |
54 |
* Creates the component with a friendly name to be |
55 |
* used as label |
56 |
* |
57 |
* @param name the friendly name |
58 |
* @param attribute the data attribute we look after |
59 |
*/ |
60 |
public CPUDataComponent(String name, String attribute) { |
61 |
_name = name; |
62 |
_attribute = attribute; |
63 |
_label = new JLabel(_name + ": "); |
64 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
65 |
setLayout(new GridLayout(1, 2)); |
66 |
_item.setString("-uninitialised-"); |
67 |
add(_label); |
68 |
add(_item); |
69 |
setVisible(false); |
70 |
} |
71 |
|
72 |
//---PUBLIC METHODS--- |
73 |
|
74 |
/** |
75 |
* This run method updates any Swing components |
76 |
* The setValue() method adds this component |
77 |
* to the Swing Event Dispatching Queue to |
78 |
* run this method. |
79 |
*/ |
80 |
public void run() { |
81 |
if(!isVisible()) { |
82 |
setVisible(true); |
83 |
} |
84 |
_item.setString(_cache + "%"); |
85 |
_item.setValue(new Double(_cache).intValue()); |
86 |
} |
87 |
|
88 |
/** |
89 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
90 |
* method to provide clean logging (every class should have this). |
91 |
* |
92 |
* @return the name of this class and its CVS revision |
93 |
*/ |
94 |
public String toString() { |
95 |
return _name + "(" + _attribute + ")"; |
96 |
} |
97 |
|
98 |
//---PRIVATE METHODS--- |
99 |
|
100 |
//---ACCESSOR/MUTATOR METHODS--- |
101 |
|
102 |
/** |
103 |
* This takes the packet to obtain the value from, it then performs all |
104 |
* approriate conversions and adds this class to the Swing Event |
105 |
* Dispatching queue. |
106 |
* |
107 |
* @param packet the XMLPacket to get the data from |
108 |
* @throws DataFormatException if there was a problem converting the data for display |
109 |
*/ |
110 |
public void setValue(XMLPacket packet) throws DataFormatException { |
111 |
String value = packet.getParam(_attribute); |
112 |
try { |
113 |
if(!_cache.equals(value)) { |
114 |
_cache = value; |
115 |
SwingUtilities.invokeLater(this); |
116 |
} |
117 |
} catch (Exception e) { |
118 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
119 |
} |
120 |
} |
121 |
|
122 |
/** |
123 |
* Returns the string showing the packet |
124 |
* attribute that the component is looking after |
125 |
* |
126 |
* @return the packet reference |
127 |
*/ |
128 |
public String getPacketAttribute() { |
129 |
return _attribute; |
130 |
} |
131 |
|
132 |
//---ATTRIBUTES--- |
133 |
|
134 |
/** |
135 |
* The friendly name of this component |
136 |
*/ |
137 |
private String _name; |
138 |
|
139 |
/** |
140 |
* The attribute that this component is concerned with |
141 |
*/ |
142 |
private String _attribute; |
143 |
|
144 |
/** |
145 |
* Remembers what the last value was, so we |
146 |
* only update if we have to. |
147 |
*/ |
148 |
protected String _cache = ""; |
149 |
|
150 |
/** |
151 |
* The minimum value for the percentage |
152 |
*/ |
153 |
private final int _min = 0; |
154 |
|
155 |
/** |
156 |
* The maximum value for the percentage |
157 |
*/ |
158 |
private final int _max = 100; |
159 |
|
160 |
/** |
161 |
* The friendly label for this component |
162 |
*/ |
163 |
private JLabel _label; |
164 |
|
165 |
/** |
166 |
* The progress bar that we will display CPU |
167 |
* percentage data in |
168 |
*/ |
169 |
private JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
170 |
{ |
171 |
_item.setStringPainted(true); |
172 |
} |
173 |
|
174 |
//---STATIC ATTRIBUTES--- |
175 |
|
176 |
} |