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