| 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 |
|
|
|
| 9 |
ajm |
1.2 |
/** |
| 10 |
|
|
* This is the most basic of DataComponents. |
| 11 |
|
|
* |
| 12 |
|
|
* It simply displays the value as a String |
| 13 |
ajm |
1.5 |
* in a JTextField. |
| 14 |
ajm |
1.2 |
* |
| 15 |
|
|
* @author $Author: ajm4 $ |
| 16 |
ajm |
1.6 |
* @version $Id: StringDataComponent.java,v 1.5 2001/01/24 01:54:43 ajm4 Exp $ |
| 17 |
ajm |
1.2 |
*/ |
| 18 |
ajm |
1.5 |
public class StringDataComponent extends VisibleDataComponent implements DataComponent { |
| 19 |
ajm |
1.2 |
|
| 20 |
|
|
//---FINAL ATTRIBUTES--- |
| 21 |
|
|
|
| 22 |
|
|
/** |
| 23 |
|
|
* The current CVS revision of this class |
| 24 |
|
|
*/ |
| 25 |
ajm |
1.6 |
public final String REVISION = "$Revision: 1.5 $"; |
| 26 |
ajm |
1.5 |
|
| 27 |
|
|
/** |
| 28 |
|
|
* The default length of the JTextField |
| 29 |
|
|
*/ |
| 30 |
ajm |
1.6 |
private final int DEFAULT_TEXT_LENGTH = 20; |
| 31 |
ajm |
1.2 |
|
| 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 |
ajm |
1.5 |
* @param attribute the data attribute we look after |
| 42 |
|
|
*/ |
| 43 |
|
|
public StringDataComponent(String name, String attribute) { |
| 44 |
ajm |
1.6 |
super(); |
| 45 |
|
|
this(name, attribute, DEFAULT_TEXT_LENGTH); |
| 46 |
ajm |
1.5 |
} |
| 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 |
ajm |
1.2 |
*/ |
| 56 |
ajm |
1.5 |
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 |
ajm |
1.1 |
_label.setHorizontalAlignment(JLabel.RIGHT); |
| 64 |
|
|
setLayout(new GridLayout(1, 2)); |
| 65 |
|
|
add(_label); |
| 66 |
|
|
add(_item); |
| 67 |
|
|
} |
| 68 |
|
|
|
| 69 |
ajm |
1.2 |
//---PUBLIC METHODS--- |
| 70 |
|
|
|
| 71 |
ajm |
1.5 |
/** |
| 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 |
ajm |
1.2 |
//---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 |
ajm |
1.1 |
public void setValue(String value) throws DataFormatException { |
| 94 |
|
|
try { |
| 95 |
ajm |
1.4 |
if(!_cache.equals(value)) { |
| 96 |
|
|
_cache = value; |
| 97 |
|
|
_item.setText(value); |
| 98 |
|
|
} |
| 99 |
ajm |
1.1 |
} catch (Exception e) { |
| 100 |
ajm |
1.5 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
| 101 |
ajm |
1.1 |
} |
| 102 |
|
|
} |
| 103 |
ajm |
1.2 |
|
| 104 |
|
|
//---ATTRIBUTES--- |
| 105 |
|
|
|
| 106 |
|
|
/** |
| 107 |
ajm |
1.5 |
* 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 |
ajm |
1.2 |
* The friendly label for this component |
| 118 |
|
|
*/ |
| 119 |
|
|
protected JLabel _label; |
| 120 |
ajm |
1.4 |
|
| 121 |
ajm |
1.5 |
/** |
| 122 |
|
|
* Remebers what the last value was, so we |
| 123 |
|
|
* only update if we have to. |
| 124 |
|
|
*/ |
| 125 |
ajm |
1.4 |
String _cache = ""; |
| 126 |
ajm |
1.1 |
|
| 127 |
ajm |
1.5 |
/** |
| 128 |
|
|
* The length of the JTextField |
| 129 |
|
|
*/ |
| 130 |
|
|
protected int _displayLength; |
| 131 |
ajm |
1.3 |
|
| 132 |
ajm |
1.2 |
/** |
| 133 |
|
|
* Just a normal label to display our value as a String |
| 134 |
|
|
*/ |
| 135 |
ajm |
1.5 |
protected JTextField _item; |
| 136 |
ajm |
1.2 |
|
| 137 |
|
|
//---STATIC ATTRIBUTES--- |
| 138 |
|
|
|
| 139 |
ajm |
1.1 |
} |