1 |
|
//---PACKAGE DECLARATION--- |
2 |
+ |
package 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 |
|
/** |
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_TEXT_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 |
> |
_name = name; |
45 |
> |
_attribute = attribute; |
46 |
> |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
47 |
> |
_item.setEditable(false); |
48 |
> |
_label = new JLabel(_name + ": "); |
49 |
|
_label.setHorizontalAlignment(JLabel.RIGHT); |
50 |
|
setLayout(new GridLayout(1, 2)); |
51 |
|
add(_label); |
54 |
|
|
55 |
|
//---PUBLIC METHODS--- |
56 |
|
|
57 |
+ |
/** |
58 |
+ |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
59 |
+ |
* method to provide clean logging (every class should have this). |
60 |
+ |
* |
61 |
+ |
* @return the name of this class and its CVS revision |
62 |
+ |
*/ |
63 |
+ |
public String toString() { |
64 |
+ |
return _name + "(" + _attribute + ")"; |
65 |
+ |
} |
66 |
+ |
|
67 |
|
//---PRIVATE METHODS--- |
68 |
|
|
69 |
|
//---ACCESSOR/MUTATOR METHODS--- |
78 |
|
*/ |
79 |
|
public void setValue(String value) throws DataFormatException { |
80 |
|
try { |
81 |
< |
_item.setText(value); |
81 |
> |
if(!_cache.equals(value)) { |
82 |
> |
_cache = value; |
83 |
> |
_item.setText(value); |
84 |
> |
} |
85 |
|
} catch (Exception e) { |
86 |
< |
throw new DataFormatException("invalid data type for component"); |
86 |
> |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
|
//---ATTRIBUTES--- |
91 |
|
|
92 |
|
/** |
93 |
+ |
* The friendly name of this component |
94 |
+ |
*/ |
95 |
+ |
private String _name; |
96 |
+ |
|
97 |
+ |
/** |
98 |
+ |
* The attribute that this component is concerned with |
99 |
+ |
*/ |
100 |
+ |
private String _attribute; |
101 |
+ |
|
102 |
+ |
/** |
103 |
|
* The friendly label for this component |
104 |
|
*/ |
105 |
|
protected JLabel _label; |
106 |
|
|
107 |
|
/** |
108 |
+ |
* Remebers what the last value was, so we |
109 |
+ |
* only update if we have to. |
110 |
+ |
*/ |
111 |
+ |
String _cache = ""; |
112 |
+ |
|
113 |
+ |
/** |
114 |
+ |
* The length of the JTextField |
115 |
+ |
*/ |
116 |
+ |
protected int _displayLength; |
117 |
+ |
|
118 |
+ |
/** |
119 |
|
* Just a normal label to display our value as a String |
120 |
|
*/ |
121 |
< |
protected JLabel _item = new JLabel(); |
121 |
> |
protected JTextField _item; |
122 |
|
|
123 |
|
//---STATIC ATTRIBUTES--- |
124 |
|
|