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