ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/datacomponents/CPUDataComponent.java
Revision: 1.9
Committed: Sun Mar 18 14:43:39 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
Changes since 1.8: +9 -8 lines
Log Message:
Lots of changes to allow components to have multiple attributes.

This now means that data for memory/swap/disks/services are all correctly updated on the display the first time they get data.  Rather than before, when they had to have at least two packets to gain all the data and would be slightly wrong.

Now it is all fine and dandy...

File Contents

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