1 |
//---PACKAGE DECLARATION--- |
2 |
package uk.org.iscream.conient.datacomponents; |
3 |
|
4 |
//---IMPORTS--- |
5 |
import javax.swing.JLabel; |
6 |
import javax.swing.JTextField; |
7 |
import java.awt.Color; |
8 |
import java.awt.GridLayout; |
9 |
import javax.swing.SwingUtilities; |
10 |
import uk.org.iscream.util.XMLPacket; |
11 |
|
12 |
/** |
13 |
* This is the most basic of DataComponents. |
14 |
* |
15 |
* It simply displays the value as a String |
16 |
* in a JTextField. |
17 |
* |
18 |
* @author $Author: ajm4 $ |
19 |
* @version $Id: ServiceDataComponent.java,v 1.2 2001/03/15 01:05:46 ajm4 Exp $ |
20 |
*/ |
21 |
public class ServiceDataComponent extends VisibleDataComponent { |
22 |
|
23 |
//---FINAL ATTRIBUTES--- |
24 |
|
25 |
/** |
26 |
* The current CVS revision of this class |
27 |
*/ |
28 |
public final String REVISION = "$Revision: 1.2 $"; |
29 |
|
30 |
/** |
31 |
* The default length of the JTextField |
32 |
*/ |
33 |
protected final int DEFAULT_TEXT_LENGTH = 20; |
34 |
|
35 |
//---STATIC METHODS--- |
36 |
|
37 |
//---CONSTRUCTORS--- |
38 |
|
39 |
/** |
40 |
* Creates the component with a friendly name to be |
41 |
* used as label |
42 |
* |
43 |
* @param name the friendly name |
44 |
* @param attribute the data attribute we look after |
45 |
*/ |
46 |
public ServiceDataComponent(String name, String attribute, String statusAttribute) { |
47 |
_name = name; |
48 |
_statusAttribute = statusAttribute; |
49 |
_attribute = attribute; |
50 |
setLayout(new GridLayout(1,1)); |
51 |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
52 |
_item.setHorizontalAlignment(JTextField.CENTER); |
53 |
_item.setEditable(false); |
54 |
_item.setText("-uninitialised-"); |
55 |
add(_item); |
56 |
} |
57 |
|
58 |
//---PUBLIC METHODS--- |
59 |
|
60 |
/** |
61 |
* This run method updates any Swing components |
62 |
* The setValue() method adds this component |
63 |
* to the Swing Event Dispatching Queue to |
64 |
* run this method. |
65 |
*/ |
66 |
public void run() { |
67 |
if (_statusCache.equals("0")) { |
68 |
_item.setBackground(Color.green); |
69 |
_item.setText(_name + "[OK] : " + _cache); |
70 |
} else { |
71 |
_item.setBackground(Color.red); |
72 |
_item.setText(_name + "[FAILED] : " + _cache); |
73 |
} |
74 |
} |
75 |
|
76 |
/** |
77 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
78 |
* method to provide clean logging (every class should have this). |
79 |
* |
80 |
* @return the name of this class and its CVS revision |
81 |
*/ |
82 |
public String toString() { |
83 |
return _name + "(" + _attribute + ")"; |
84 |
} |
85 |
|
86 |
//---PRIVATE METHODS--- |
87 |
|
88 |
//---ACCESSOR/MUTATOR METHODS--- |
89 |
|
90 |
/** |
91 |
* This takes the packet to obtain the value from, it then performs all |
92 |
* approriate conversions and adds this class to the Swing Event |
93 |
* Dispatching queue. |
94 |
* |
95 |
* @param packet the XMLPacket to get the data from |
96 |
* @throws DataFormatException if there was a problem converting the data for display |
97 |
*/ |
98 |
public void setValue(XMLPacket packet) throws DataFormatException { |
99 |
String value = packet.getParam(_attribute); |
100 |
String statusvalue = packet.getParam(_statusAttribute); |
101 |
try { |
102 |
if(!_cache.equals(value) || !_statusCache.equals(statusvalue)) { |
103 |
_cache = value; |
104 |
_statusCache = statusvalue; |
105 |
SwingUtilities.invokeLater(this); |
106 |
} |
107 |
} catch (Exception e) { |
108 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
109 |
} |
110 |
} |
111 |
|
112 |
/** |
113 |
* Returns the string showing the packet |
114 |
* attribute that the component is looking after |
115 |
* |
116 |
* @return the packet reference |
117 |
*/ |
118 |
public String getPacketAttribute() { |
119 |
return _attribute; |
120 |
} |
121 |
|
122 |
//---ATTRIBUTES--- |
123 |
|
124 |
/** |
125 |
* The friendly name of this component |
126 |
*/ |
127 |
protected String _name; |
128 |
|
129 |
/** |
130 |
* The attribute that this component is concerned with |
131 |
*/ |
132 |
protected String _attribute; |
133 |
|
134 |
/** |
135 |
* The service status attribute that this component is concerned with |
136 |
*/ |
137 |
protected String _statusAttribute; |
138 |
|
139 |
/** |
140 |
* The friendly label for this component |
141 |
*/ |
142 |
protected JLabel _label; |
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 |
* Remembers what the last value was, so we |
152 |
* only update if we have to. |
153 |
*/ |
154 |
protected String _statusCache = ""; |
155 |
|
156 |
/** |
157 |
* The length of the JTextField |
158 |
*/ |
159 |
protected int _displayLength; |
160 |
|
161 |
/** |
162 |
* Just a normal label to display our value as a String |
163 |
*/ |
164 |
protected JTextField _item; |
165 |
|
166 |
//---STATIC ATTRIBUTES--- |
167 |
|
168 |
} |