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 JTextField. |
14 |
* |
15 |
* @author $Author: ajm4 $ |
16 |
* @version $Id: StringDataComponent.java,v 1.5 2001/01/24 01:54:43 ajm4 Exp $ |
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: 1.5 $"; |
26 |
|
27 |
/** |
28 |
* The default length of the JTextField |
29 |
*/ |
30 |
private final int DEFAULT_TEXT_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 |
super(); |
45 |
this(name, attribute, DEFAULT_TEXT_LENGTH); |
46 |
} |
47 |
|
48 |
/** |
49 |
* Creates the component with a friendly name to be |
50 |
* used as label |
51 |
* |
52 |
* @param name the friendly name |
53 |
* @param attribute the data attribute we look after |
54 |
* @param displayLength the length of the JTextField |
55 |
*/ |
56 |
public StringDataComponent(String name, String attribute, int displayLength) { |
57 |
_name = name; |
58 |
_attribute = attribute; |
59 |
_displayLength = displayLength; |
60 |
_item = new JTextField("", _displayLength); |
61 |
_item.setEditable(false); |
62 |
_label = new JLabel(_name + ": "); |
63 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
64 |
setLayout(new GridLayout(1, 2)); |
65 |
add(_label); |
66 |
add(_item); |
67 |
} |
68 |
|
69 |
//---PUBLIC METHODS--- |
70 |
|
71 |
/** |
72 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
73 |
* method to provide clean logging (every class should have this). |
74 |
* |
75 |
* @return the name of this class and its CVS revision |
76 |
*/ |
77 |
public String toString() { |
78 |
return _name + "(" + _attribute + ")"; |
79 |
} |
80 |
|
81 |
//---PRIVATE METHODS--- |
82 |
|
83 |
//---ACCESSOR/MUTATOR METHODS--- |
84 |
|
85 |
/** |
86 |
* This takes the String value of the parameter that this component |
87 |
* is monitoring direct from the packet, it then performs all |
88 |
* approriate conversions and displays the data. |
89 |
* |
90 |
* @param value the value for this data component |
91 |
* @throws DataFormatException if there was a problem converting the data for display |
92 |
*/ |
93 |
public void setValue(String value) throws DataFormatException { |
94 |
try { |
95 |
if(!_cache.equals(value)) { |
96 |
_cache = value; |
97 |
_item.setText(value); |
98 |
} |
99 |
} catch (Exception e) { |
100 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
101 |
} |
102 |
} |
103 |
|
104 |
//---ATTRIBUTES--- |
105 |
|
106 |
/** |
107 |
* The friendly name of this component |
108 |
*/ |
109 |
private String _name; |
110 |
|
111 |
/** |
112 |
* The attribute that this component is concerned with |
113 |
*/ |
114 |
private String _attribute; |
115 |
|
116 |
/** |
117 |
* The friendly label for this component |
118 |
*/ |
119 |
protected JLabel _label; |
120 |
|
121 |
/** |
122 |
* Remebers what the last value was, so we |
123 |
* only update if we have to. |
124 |
*/ |
125 |
String _cache = ""; |
126 |
|
127 |
/** |
128 |
* The length of the JTextField |
129 |
*/ |
130 |
protected int _displayLength; |
131 |
|
132 |
/** |
133 |
* Just a normal label to display our value as a String |
134 |
*/ |
135 |
protected JTextField _item; |
136 |
|
137 |
//---STATIC ATTRIBUTES--- |
138 |
|
139 |
} |