1 |
ajm |
1.2 |
//---PACKAGE DECLARATION--- |
2 |
ajm |
1.6 |
package uk.ac.ukc.iscream.conient.datacomponents; |
3 |
ajm |
1.2 |
|
4 |
|
|
//---IMPORTS--- |
5 |
ajm |
1.1 |
import javax.swing.JLabel; |
6 |
ajm |
1.3 |
import javax.swing.JTextField; |
7 |
ajm |
1.1 |
import java.awt.GridLayout; |
8 |
ajm |
1.8 |
import javax.swing.SwingUtilities; |
9 |
ajm |
1.1 |
|
10 |
ajm |
1.2 |
/** |
11 |
|
|
* This is the most basic of DataComponents. |
12 |
|
|
* |
13 |
|
|
* It simply displays the value as a String |
14 |
ajm |
1.5 |
* in a JTextField. |
15 |
ajm |
1.2 |
* |
16 |
|
|
* @author $Author: ajm4 $ |
17 |
ajm |
1.11 |
* @version $Id: StringDataComponent.java,v 1.10 2001/01/29 13:58:45 ajm4 Exp $ |
18 |
ajm |
1.2 |
*/ |
19 |
ajm |
1.8 |
public class StringDataComponent extends VisibleDataComponent { |
20 |
ajm |
1.2 |
|
21 |
|
|
//---FINAL ATTRIBUTES--- |
22 |
|
|
|
23 |
|
|
/** |
24 |
|
|
* The current CVS revision of this class |
25 |
|
|
*/ |
26 |
ajm |
1.11 |
public final String REVISION = "$Revision: 1.10 $"; |
27 |
ajm |
1.5 |
|
28 |
|
|
/** |
29 |
|
|
* The default length of the JTextField |
30 |
|
|
*/ |
31 |
ajm |
1.11 |
protected final int DEFAULT_TEXT_LENGTH = 20; |
32 |
ajm |
1.2 |
|
33 |
|
|
//---STATIC METHODS--- |
34 |
|
|
|
35 |
|
|
//---CONSTRUCTORS--- |
36 |
|
|
|
37 |
|
|
/** |
38 |
|
|
* Creates the component with a friendly name to be |
39 |
|
|
* used as label |
40 |
|
|
* |
41 |
|
|
* @param name the friendly name |
42 |
ajm |
1.5 |
* @param attribute the data attribute we look after |
43 |
|
|
*/ |
44 |
|
|
public StringDataComponent(String name, String attribute) { |
45 |
|
|
_name = name; |
46 |
|
|
_attribute = attribute; |
47 |
ajm |
1.7 |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
48 |
ajm |
1.5 |
_item.setEditable(false); |
49 |
|
|
_label = new JLabel(_name + ": "); |
50 |
ajm |
1.1 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
51 |
|
|
setLayout(new GridLayout(1, 2)); |
52 |
ajm |
1.11 |
_item.setText("-uninitialised-"); |
53 |
ajm |
1.1 |
add(_label); |
54 |
|
|
add(_item); |
55 |
|
|
} |
56 |
|
|
|
57 |
ajm |
1.2 |
//---PUBLIC METHODS--- |
58 |
|
|
|
59 |
ajm |
1.5 |
/** |
60 |
ajm |
1.8 |
* 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 |
ajm |
1.5 |
* 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 |
ajm |
1.2 |
//---PRIVATE METHODS--- |
80 |
|
|
|
81 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
82 |
|
|
|
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 |
ajm |
1.8 |
* approriate conversions and adds this class to the Swing Event |
87 |
|
|
* Dispatching queue. |
88 |
ajm |
1.2 |
* |
89 |
|
|
* @param value the value for this data component |
90 |
|
|
* @throws DataFormatException if there was a problem converting the data for display |
91 |
|
|
*/ |
92 |
ajm |
1.1 |
public void setValue(String value) throws DataFormatException { |
93 |
|
|
try { |
94 |
ajm |
1.4 |
if(!_cache.equals(value)) { |
95 |
|
|
_cache = value; |
96 |
ajm |
1.9 |
SwingUtilities.invokeLater(this); |
97 |
ajm |
1.4 |
} |
98 |
ajm |
1.1 |
} catch (Exception e) { |
99 |
ajm |
1.5 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
100 |
ajm |
1.1 |
} |
101 |
ajm |
1.11 |
} |
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 |
ajm |
1.2 |
|
113 |
|
|
//---ATTRIBUTES--- |
114 |
|
|
|
115 |
|
|
/** |
116 |
ajm |
1.5 |
* The friendly name of this component |
117 |
|
|
*/ |
118 |
ajm |
1.11 |
protected String _name; |
119 |
ajm |
1.5 |
|
120 |
|
|
/** |
121 |
|
|
* The attribute that this component is concerned with |
122 |
|
|
*/ |
123 |
ajm |
1.11 |
protected String _attribute; |
124 |
ajm |
1.5 |
|
125 |
|
|
/** |
126 |
ajm |
1.2 |
* The friendly label for this component |
127 |
|
|
*/ |
128 |
|
|
protected JLabel _label; |
129 |
ajm |
1.4 |
|
130 |
ajm |
1.5 |
/** |
131 |
|
|
* Remebers what the last value was, so we |
132 |
|
|
* only update if we have to. |
133 |
|
|
*/ |
134 |
ajm |
1.4 |
String _cache = ""; |
135 |
ajm |
1.1 |
|
136 |
ajm |
1.5 |
/** |
137 |
|
|
* The length of the JTextField |
138 |
|
|
*/ |
139 |
|
|
protected int _displayLength; |
140 |
ajm |
1.3 |
|
141 |
ajm |
1.2 |
/** |
142 |
|
|
* Just a normal label to display our value as a String |
143 |
|
|
*/ |
144 |
ajm |
1.5 |
protected JTextField _item; |
145 |
ajm |
1.2 |
|
146 |
|
|
//---STATIC ATTRIBUTES--- |
147 |
|
|
|
148 |
ajm |
1.8 |
} |