1 |
+ |
//---PACKAGE DECLARATION--- |
2 |
+ |
uk.ac.ukc.iscream.conient.datacomponents; |
3 |
+ |
|
4 |
+ |
//---IMPORTS--- |
5 |
|
import javax.swing.JLabel; |
6 |
+ |
import javax.swing.JTextField; |
7 |
|
import java.awt.GridLayout; |
8 |
|
|
9 |
< |
public class StringDataComponent extends DataComponent { |
10 |
< |
public StringDataComponent(String name) { |
11 |
< |
_label = new JLabel(name + ": "); |
9 |
> |
/** |
10 |
> |
* This is the most basic of DataComponents. |
11 |
> |
* |
12 |
> |
* It simply displays the value as a String |
13 |
> |
* in a JTextField. |
14 |
> |
* |
15 |
> |
* @author $Author$ |
16 |
> |
* @version $Id$ |
17 |
> |
*/ |
18 |
> |
public class StringDataComponent extends VisibleDataComponent implements DataComponent { |
19 |
> |
|
20 |
> |
//---FINAL ATTRIBUTES--- |
21 |
> |
|
22 |
> |
/** |
23 |
> |
* The current CVS revision of this class |
24 |
> |
*/ |
25 |
> |
public final String REVISION = "$Revision$"; |
26 |
> |
|
27 |
> |
/** |
28 |
> |
* The default length of the JTextField |
29 |
> |
*/ |
30 |
> |
private final int DEFAULT_DISPLAY_LENGTH = 20; |
31 |
> |
|
32 |
> |
//---STATIC METHODS--- |
33 |
> |
|
34 |
> |
//---CONSTRUCTORS--- |
35 |
> |
|
36 |
> |
/** |
37 |
> |
* Creates the component with a friendly name to be |
38 |
> |
* used as label |
39 |
> |
* |
40 |
> |
* @param name the friendly name |
41 |
> |
* @param attribute the data attribute we look after |
42 |
> |
*/ |
43 |
> |
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) { |
56 |
> |
_name = name; |
57 |
> |
_attribute = attribute; |
58 |
> |
_displayLength = displayLength; |
59 |
> |
_item = new JTextField("", _displayLength); |
60 |
> |
_item.setEditable(false); |
61 |
> |
_label = new JLabel(_name + ": "); |
62 |
|
_label.setHorizontalAlignment(JLabel.RIGHT); |
63 |
|
setLayout(new GridLayout(1, 2)); |
64 |
|
add(_label); |
65 |
|
add(_item); |
66 |
|
} |
67 |
|
|
68 |
+ |
//---PUBLIC METHODS--- |
69 |
+ |
|
70 |
+ |
/** |
71 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
72 |
+ |
* method to provide clean logging (every class should have this). |
73 |
+ |
* |
74 |
+ |
* @return the name of this class and its CVS revision |
75 |
+ |
*/ |
76 |
+ |
public String toString() { |
77 |
+ |
return _name + "(" + _attribute + ")"; |
78 |
+ |
} |
79 |
+ |
|
80 |
+ |
//---PRIVATE METHODS--- |
81 |
+ |
|
82 |
+ |
//---ACCESSOR/MUTATOR METHODS--- |
83 |
+ |
|
84 |
+ |
/** |
85 |
+ |
* This takes the String value of the parameter that this component |
86 |
+ |
* is monitoring direct from the packet, it then performs all |
87 |
+ |
* approriate conversions and displays the data. |
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 |
> |
_item.setText(value); |
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 |
|
} |
102 |
+ |
|
103 |
+ |
//---ATTRIBUTES--- |
104 |
+ |
|
105 |
+ |
/** |
106 |
+ |
* The friendly name of this component |
107 |
+ |
*/ |
108 |
+ |
private String _name; |
109 |
|
|
110 |
+ |
/** |
111 |
+ |
* The attribute that this component is concerned with |
112 |
+ |
*/ |
113 |
+ |
private String _attribute; |
114 |
+ |
|
115 |
+ |
/** |
116 |
+ |
* The friendly label for this component |
117 |
+ |
*/ |
118 |
|
protected JLabel _label; |
119 |
< |
protected JLabel _item = new JLabel(); |
119 |
> |
|
120 |
> |
/** |
121 |
> |
* Remebers what the last value was, so we |
122 |
> |
* only update if we have to. |
123 |
> |
*/ |
124 |
> |
String _cache = ""; |
125 |
> |
|
126 |
> |
/** |
127 |
> |
* The length of the JTextField |
128 |
> |
*/ |
129 |
> |
protected int _displayLength; |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* Just a normal label to display our value as a String |
133 |
> |
*/ |
134 |
> |
protected JTextField _item; |
135 |
> |
|
136 |
> |
//---STATIC ATTRIBUTES--- |
137 |
> |
|
138 |
|
} |