1 |
|
//---PACKAGE DECLARATION--- |
2 |
< |
uk.ac.ukc.iscream.conient.datacomponents; |
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. |
17 |
|
* @author $Author$ |
18 |
|
* @version $Id$ |
19 |
|
*/ |
20 |
< |
public class StringDataComponent extends VisibleDataComponent implements DataComponent { |
20 |
> |
public class StringDataComponent extends VisibleDataComponent { |
21 |
|
|
22 |
|
//---FINAL ATTRIBUTES--- |
23 |
|
|
29 |
|
/** |
30 |
|
* The default length of the JTextField |
31 |
|
*/ |
32 |
< |
private final int DEFAULT_DISPLAY_LENGTH = 20; |
32 |
> |
protected final int DEFAULT_TEXT_LENGTH = 20; |
33 |
|
|
34 |
|
//---STATIC METHODS--- |
35 |
|
|
43 |
|
* @param attribute the data attribute we look after |
44 |
|
*/ |
45 |
|
public StringDataComponent(String name, String attribute) { |
44 |
– |
this(name, attribute, DEFAULT_DISPLAY_LENGTH); |
45 |
– |
} |
46 |
– |
|
47 |
– |
/** |
48 |
– |
* Creates the component with a friendly name to be |
49 |
– |
* used as label |
50 |
– |
* |
51 |
– |
* @param name the friendly name |
52 |
– |
* @param attribute the data attribute we look after |
53 |
– |
* @param displayLength the length of the JTextField |
54 |
– |
*/ |
55 |
– |
public StringDataComponent(String name, String attribute, int displayLength) { |
46 |
|
_name = name; |
47 |
|
_attribute = attribute; |
48 |
< |
_displayLength = displayLength; |
59 |
< |
_item = new JTextField("", _displayLength); |
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 |
|
* |
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 |
|
if(!_cache.equals(value)) { |
100 |
|
_cache = value; |
101 |
< |
_item.setText(value); |
101 |
> |
SwingUtilities.invokeLater(this); |
102 |
|
} |
103 |
|
} catch (Exception e) { |
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 |
< |
private String _name; |
133 |
> |
protected String _name; |
134 |
|
|
135 |
|
/** |
136 |
|
* The attribute that this component is concerned with |
137 |
|
*/ |
138 |
< |
private String _attribute; |
138 |
> |
protected String _attribute; |
139 |
|
|
140 |
|
/** |
141 |
|
* The friendly label for this component |
143 |
|
protected JLabel _label; |
144 |
|
|
145 |
|
/** |
146 |
< |
* Remebers what the last value was, so we |
146 |
> |
* Remembers what the last value was, so we |
147 |
|
* only update if we have to. |
148 |
|
*/ |
149 |
< |
String _cache = ""; |
149 |
> |
protected String _cache = ""; |
150 |
|
|
151 |
|
/** |
152 |
|
* The length of the JTextField |
160 |
|
|
161 |
|
//---STATIC ATTRIBUTES--- |
162 |
|
|
163 |
< |
} |
163 |
> |
} |