| 1 |
//---PACKAGE DECLARATION--- |
| 2 |
package uk.org.iscream.conient.datacomponents; |
| 3 |
|
| 4 |
//---IMPORTS--- |
| 5 |
import javax.swing.JLabel; |
| 6 |
import javax.swing.JTextField; |
| 7 |
import java.awt.Color; |
| 8 |
import java.awt.GridLayout; |
| 9 |
import javax.swing.SwingUtilities; |
| 10 |
import uk.org.iscream.util.XMLPacket; |
| 11 |
|
| 12 |
/** |
| 13 |
* This is the most basic of DataComponents. |
| 14 |
* |
| 15 |
* It simply displays the value as a String |
| 16 |
* in a JTextField. |
| 17 |
* |
| 18 |
* @author $Author: ajm4 $ |
| 19 |
* @version $Id: ServiceDataComponent.java,v 1.4 2001/03/18 16:28:53 ajm4 Exp $ |
| 20 |
*/ |
| 21 |
public class ServiceDataComponent extends VisibleDataComponent { |
| 22 |
|
| 23 |
//---FINAL ATTRIBUTES--- |
| 24 |
|
| 25 |
/** |
| 26 |
* The current CVS revision of this class |
| 27 |
*/ |
| 28 |
public final String REVISION = "$Revision: 1.4 $"; |
| 29 |
|
| 30 |
/** |
| 31 |
* The default length of the JTextField |
| 32 |
*/ |
| 33 |
protected final int DEFAULT_TEXT_LENGTH = 20; |
| 34 |
|
| 35 |
//---STATIC METHODS--- |
| 36 |
|
| 37 |
//---CONSTRUCTORS--- |
| 38 |
|
| 39 |
/** |
| 40 |
* Creates the component with a friendly name to be |
| 41 |
* used as label |
| 42 |
* |
| 43 |
* @param name the friendly name |
| 44 |
* @param attribute the data attribute we look after |
| 45 |
*/ |
| 46 |
public ServiceDataComponent(String name, String attribute, String statusAttribute) { |
| 47 |
_name = name; |
| 48 |
_statusAttribute = statusAttribute; |
| 49 |
_attribute = attribute; |
| 50 |
setLayout(new GridLayout(1,1)); |
| 51 |
_item = new JTextField("", DEFAULT_TEXT_LENGTH); |
| 52 |
_item.setHorizontalAlignment(JTextField.LEFT); |
| 53 |
_item.setEditable(false); |
| 54 |
_item.setText("-uninitialised-"); |
| 55 |
add(_item); |
| 56 |
setVisible(false); |
| 57 |
} |
| 58 |
|
| 59 |
//---PUBLIC METHODS--- |
| 60 |
|
| 61 |
/** |
| 62 |
* This run method updates any Swing components |
| 63 |
* The setValue() method adds this component |
| 64 |
* to the Swing Event Dispatching Queue to |
| 65 |
* run this method. |
| 66 |
*/ |
| 67 |
public void run() { |
| 68 |
if(!isVisible()) { |
| 69 |
setVisible(true); |
| 70 |
} |
| 71 |
if (_statusCache.equals("0")) { |
| 72 |
_item.setForeground(Color.black); |
| 73 |
_item.setText(_name + "[OK] : " + _cache); |
| 74 |
} else { |
| 75 |
_item.setForeground(Color.red); |
| 76 |
_item.setText(_name + "[FAILED] : " + _cache); |
| 77 |
} |
| 78 |
_item.setCaretPosition(0); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 83 |
* method to provide clean logging (every class should have this). |
| 84 |
* |
| 85 |
* @return the name of this class and its CVS revision |
| 86 |
*/ |
| 87 |
public String toString() { |
| 88 |
return _name + "(" + _attribute + ")"; |
| 89 |
} |
| 90 |
|
| 91 |
//---PRIVATE METHODS--- |
| 92 |
|
| 93 |
//---ACCESSOR/MUTATOR METHODS--- |
| 94 |
|
| 95 |
/** |
| 96 |
* This takes the packet to obtain the value from, it then performs all |
| 97 |
* approriate conversions and adds this class to the Swing Event |
| 98 |
* Dispatching queue. |
| 99 |
* |
| 100 |
* @param packet the XMLPacket to get the data from |
| 101 |
* @throws DataFormatException if there was a problem converting the data for display |
| 102 |
*/ |
| 103 |
public void setValue(XMLPacket packet) throws DataFormatException { |
| 104 |
String value = packet.getParam(_attribute); |
| 105 |
String statusvalue = packet.getParam(_statusAttribute); |
| 106 |
try { |
| 107 |
if(!_cache.equals(value) || !_statusCache.equals(statusvalue)) { |
| 108 |
_cache = value; |
| 109 |
_statusCache = statusvalue; |
| 110 |
SwingUtilities.invokeLater(this); |
| 111 |
} |
| 112 |
} catch (Exception e) { |
| 113 |
throw new DataFormatException(value + " is an invalid data type for " + toString()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Returns the string showing the packet |
| 119 |
* attribute that the component is looking after |
| 120 |
* |
| 121 |
* @return the packet reference |
| 122 |
*/ |
| 123 |
public String getPacketAttribute() { |
| 124 |
return _attribute; |
| 125 |
} |
| 126 |
|
| 127 |
//---ATTRIBUTES--- |
| 128 |
|
| 129 |
/** |
| 130 |
* The friendly name of this component |
| 131 |
*/ |
| 132 |
protected String _name; |
| 133 |
|
| 134 |
/** |
| 135 |
* The attribute that this component is concerned with |
| 136 |
*/ |
| 137 |
protected String _attribute; |
| 138 |
|
| 139 |
/** |
| 140 |
* The service status attribute that this component is concerned with |
| 141 |
*/ |
| 142 |
protected String _statusAttribute; |
| 143 |
|
| 144 |
/** |
| 145 |
* The friendly label for this component |
| 146 |
*/ |
| 147 |
protected JLabel _label; |
| 148 |
|
| 149 |
/** |
| 150 |
* Remembers what the last value was, so we |
| 151 |
* only update if we have to. |
| 152 |
*/ |
| 153 |
protected String _cache = ""; |
| 154 |
|
| 155 |
/** |
| 156 |
* Remembers what the last value was, so we |
| 157 |
* only update if we have to. |
| 158 |
*/ |
| 159 |
protected String _statusCache = ""; |
| 160 |
|
| 161 |
/** |
| 162 |
* The length of the JTextField |
| 163 |
*/ |
| 164 |
protected int _displayLength; |
| 165 |
|
| 166 |
/** |
| 167 |
* Just a normal label to display our value as a String |
| 168 |
*/ |
| 169 |
protected JTextField _item; |
| 170 |
|
| 171 |
//---STATIC ATTRIBUTES--- |
| 172 |
|
| 173 |
} |