1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
uk.ac.ukc.iscream.conient.datacomponents; |
3 |
|
|
4 |
|
//---IMPORTS--- |
5 |
|
import javax.swing.JLabel; |
10 |
|
* This is the most basic of DataComponents. |
11 |
|
* |
12 |
|
* It simply displays the value as a String |
13 |
< |
* in a JLabel. |
13 |
> |
* in a JTextField. |
14 |
|
* |
15 |
|
* @author $Author$ |
16 |
|
* @version $Id$ |
17 |
|
*/ |
18 |
< |
public class StringDataComponent extends DataComponent { |
18 |
> |
public class StringDataComponent extends VisibleDataComponent implements DataComponent { |
19 |
|
|
20 |
|
//---FINAL ATTRIBUTES--- |
21 |
|
|
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 |
|
|
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) { |
44 |
< |
_label = new JLabel(name + ": "); |
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); |
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--- |
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 |
|
|
120 |
+ |
/** |
121 |
+ |
* Remebers what the last value was, so we |
122 |
+ |
* only update if we have to. |
123 |
+ |
*/ |
124 |
|
String _cache = ""; |
125 |
|
|
126 |
< |
protected int _displayLength = 20; |
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 = new JTextField("", _displayLength); |
84 |
< |
{ |
85 |
< |
_item.setEditable(false); |
86 |
< |
} |
134 |
> |
protected JTextField _item; |
135 |
|
|
136 |
|
//---STATIC ATTRIBUTES--- |
137 |
|
|