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