1 |
tdb |
1.8 |
/* |
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 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
21 |
tdb |
1.7 |
package uk.org.iscream.cms.conient.datacomponents; |
22 |
ajm |
1.1 |
|
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 |
tdb |
1.7 |
import uk.org.iscream.cms.server.util.XMLPacket; |
30 |
ajm |
1.1 |
|
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 |
tdb |
1.8 |
* @author $Author: tdb $ |
38 |
|
|
* @version $Id: ServiceDataComponent.java,v 1.7 2001/05/29 17:41:32 tdb Exp $ |
39 |
ajm |
1.1 |
*/ |
40 |
|
|
public class ServiceDataComponent extends VisibleDataComponent { |
41 |
|
|
|
42 |
|
|
//---FINAL ATTRIBUTES--- |
43 |
|
|
|
44 |
|
|
/** |
45 |
|
|
* The current CVS revision of this class |
46 |
|
|
*/ |
47 |
tdb |
1.8 |
public final String REVISION = "$Revision: 1.7 $"; |
48 |
ajm |
1.1 |
|
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 |
ajm |
1.3 |
public ServiceDataComponent(String name, String attribute, String statusAttribute) { |
66 |
ajm |
1.1 |
_name = name; |
67 |
ajm |
1.3 |
_statusAttribute = statusAttribute; |
68 |
ajm |
1.1 |
_attribute = attribute; |
69 |
ajm |
1.6 |
setLayout(new GridLayout(1,2)); |
70 |
|
|
_label = new JLabel(_name + ": "); |
71 |
|
|
_label.setHorizontalAlignment(JLabel.RIGHT); |
72 |
ajm |
1.1 |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
73 |
ajm |
1.5 |
_item.setHorizontalAlignment(JTextField.LEFT); |
74 |
ajm |
1.1 |
_item.setEditable(false); |
75 |
|
|
_item.setText("-uninitialised-"); |
76 |
ajm |
1.6 |
add(_label); |
77 |
ajm |
1.1 |
add(_item); |
78 |
ajm |
1.4 |
setVisible(false); |
79 |
ajm |
1.1 |
} |
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 |
ajm |
1.4 |
if(!isVisible()) { |
91 |
|
|
setVisible(true); |
92 |
|
|
} |
93 |
ajm |
1.3 |
if (_statusCache.equals("0")) { |
94 |
ajm |
1.5 |
_item.setForeground(Color.black); |
95 |
ajm |
1.6 |
_item.setText("[OK] : " + _cache); |
96 |
ajm |
1.1 |
} else { |
97 |
ajm |
1.5 |
_item.setForeground(Color.red); |
98 |
ajm |
1.6 |
_item.setText("[FAILED] : " + _cache); |
99 |
ajm |
1.1 |
} |
100 |
ajm |
1.5 |
_item.setCaretPosition(0); |
101 |
ajm |
1.1 |
} |
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 |
ajm |
1.3 |
* This takes the packet to obtain the value from, it then performs all |
119 |
ajm |
1.1 |
* approriate conversions and adds this class to the Swing Event |
120 |
|
|
* Dispatching queue. |
121 |
|
|
* |
122 |
ajm |
1.3 |
* @param packet the XMLPacket to get the data from |
123 |
ajm |
1.1 |
* @throws DataFormatException if there was a problem converting the data for display |
124 |
|
|
*/ |
125 |
ajm |
1.3 |
public void setValue(XMLPacket packet) throws DataFormatException { |
126 |
|
|
String value = packet.getParam(_attribute); |
127 |
|
|
String statusvalue = packet.getParam(_statusAttribute); |
128 |
ajm |
1.1 |
try { |
129 |
ajm |
1.3 |
if(!_cache.equals(value) || !_statusCache.equals(statusvalue)) { |
130 |
ajm |
1.1 |
_cache = value; |
131 |
ajm |
1.3 |
_statusCache = statusvalue; |
132 |
ajm |
1.1 |
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 |
ajm |
1.3 |
* The service status attribute that this component is concerned with |
163 |
|
|
*/ |
164 |
|
|
protected String _statusAttribute; |
165 |
|
|
|
166 |
|
|
/** |
167 |
ajm |
1.1 |
* The friendly label for this component |
168 |
|
|
*/ |
169 |
|
|
protected JLabel _label; |
170 |
|
|
|
171 |
|
|
/** |
172 |
ajm |
1.3 |
* Remembers what the last value was, so we |
173 |
ajm |
1.1 |
* only update if we have to. |
174 |
|
|
*/ |
175 |
ajm |
1.3 |
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 |
ajm |
1.1 |
|
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 |
|
|
} |