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