1 |
tdb |
1.12 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.15 |
* http://www.i-scream.org |
4 |
tdb |
1.12 |
* 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 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
22 |
tdb |
1.11 |
package uk.org.iscream.cms.conient.datacomponents; |
23 |
ajm |
1.1 |
|
24 |
|
|
//---IMPORTS--- |
25 |
|
|
import javax.swing.JLabel; |
26 |
|
|
import java.awt.GridLayout; |
27 |
|
|
import javax.swing.JProgressBar; |
28 |
ajm |
1.5 |
import javax.swing.SwingUtilities; |
29 |
tdb |
1.14 |
import uk.org.iscream.cms.util.XMLPacket; |
30 |
ajm |
1.1 |
|
31 |
|
|
/** |
32 |
|
|
* This is DataComponent specifically for |
33 |
|
|
* displaying CPU percentages. |
34 |
|
|
* |
35 |
|
|
* It uses the JProgressBar to display the value. |
36 |
|
|
* |
37 |
tdb |
1.12 |
* @author $Author: tdb $ |
38 |
tdb |
1.15 |
* @version $Id: CPUDataComponent.java,v 1.14 2003/02/05 19:35:04 tdb Exp $ |
39 |
ajm |
1.1 |
*/ |
40 |
ajm |
1.5 |
public class CPUDataComponent extends VisibleDataComponent { |
41 |
ajm |
1.1 |
|
42 |
|
|
//---FINAL ATTRIBUTES--- |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* The current CVS revision of this class |
46 |
|
|
*/ |
47 |
tdb |
1.15 |
public final String REVISION = "$Revision: 1.14 $"; |
48 |
ajm |
1.1 |
|
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 |
ajm |
1.3 |
* @param attribute the data attribute we look after |
59 |
ajm |
1.1 |
*/ |
60 |
ajm |
1.3 |
public CPUDataComponent(String name, String attribute) { |
61 |
|
|
_name = name; |
62 |
|
|
_attribute = attribute; |
63 |
|
|
_label = new JLabel(_name + ": "); |
64 |
ajm |
1.1 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
65 |
|
|
setLayout(new GridLayout(1, 2)); |
66 |
ajm |
1.7 |
_item.setString("-uninitialised-"); |
67 |
ajm |
1.1 |
add(_label); |
68 |
|
|
add(_item); |
69 |
ajm |
1.10 |
setVisible(false); |
70 |
ajm |
1.1 |
} |
71 |
|
|
|
72 |
|
|
//---PUBLIC METHODS--- |
73 |
|
|
|
74 |
ajm |
1.3 |
/** |
75 |
ajm |
1.5 |
* 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 |
ajm |
1.10 |
if(!isVisible()) { |
82 |
|
|
setVisible(true); |
83 |
|
|
} |
84 |
ajm |
1.5 |
_item.setString(_cache + "%"); |
85 |
|
|
_item.setValue(new Double(_cache).intValue()); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/** |
89 |
ajm |
1.3 |
* 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 |
ajm |
1.1 |
//---PRIVATE METHODS--- |
99 |
|
|
|
100 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
101 |
|
|
|
102 |
|
|
/** |
103 |
ajm |
1.9 |
* This takes the packet to obtain the value from, it then performs all |
104 |
ajm |
1.5 |
* approriate conversions and adds this class to the Swing Event |
105 |
|
|
* Dispatching queue. |
106 |
ajm |
1.1 |
* |
107 |
ajm |
1.9 |
* @param packet the XMLPacket to get the data from |
108 |
ajm |
1.2 |
* @throws DataFormatException if there was a problem converting the data for display |
109 |
ajm |
1.1 |
*/ |
110 |
ajm |
1.9 |
public void setValue(XMLPacket packet) throws DataFormatException { |
111 |
|
|
String value = packet.getParam(_attribute); |
112 |
ajm |
1.1 |
try { |
113 |
ajm |
1.3 |
if(!_cache.equals(value)) { |
114 |
|
|
_cache = value; |
115 |
ajm |
1.5 |
SwingUtilities.invokeLater(this); |
116 |
ajm |
1.3 |
} |
117 |
ajm |
1.1 |
} catch (Exception e) { |
118 |
ajm |
1.3 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
119 |
ajm |
1.1 |
} |
120 |
ajm |
1.7 |
} |
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 |
ajm |
1.1 |
|
132 |
|
|
//---ATTRIBUTES--- |
133 |
|
|
|
134 |
ajm |
1.3 |
/** |
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 |
ajm |
1.9 |
* Remembers what the last value was, so we |
146 |
ajm |
1.3 |
* only update if we have to. |
147 |
|
|
*/ |
148 |
ajm |
1.9 |
protected String _cache = ""; |
149 |
ajm |
1.3 |
|
150 |
ajm |
1.2 |
/** |
151 |
|
|
* The minimum value for the percentage |
152 |
|
|
*/ |
153 |
ajm |
1.1 |
private final int _min = 0; |
154 |
ajm |
1.2 |
|
155 |
|
|
/** |
156 |
|
|
* The maximum value for the percentage |
157 |
|
|
*/ |
158 |
ajm |
1.1 |
private final int _max = 100; |
159 |
ajm |
1.2 |
|
160 |
|
|
/** |
161 |
|
|
* The friendly label for this component |
162 |
|
|
*/ |
163 |
ajm |
1.1 |
private JLabel _label; |
164 |
ajm |
1.2 |
|
165 |
|
|
/** |
166 |
|
|
* The progress bar that we will display CPU |
167 |
|
|
* percentage data in |
168 |
|
|
*/ |
169 |
ajm |
1.1 |
private JProgressBar _item = new JProgressBar(JProgressBar.HORIZONTAL, _min, _max); |
170 |
|
|
{ |
171 |
|
|
_item.setStringPainted(true); |
172 |
|
|
} |
173 |
ajm |
1.2 |
|
174 |
|
|
//---STATIC ATTRIBUTES--- |
175 |
|
|
|
176 |
ajm |
1.5 |
} |