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