ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/conient/uk/org/iscream/cms/conient/datacomponents/ServiceDataComponent.java
Revision: 1.6
Committed: Mon Mar 19 13:56:22 2001 UTC (23 years, 2 months ago) by ajm
Branch: MAIN
CVS Tags: PROJECT_COMPLETION
Changes since 1.5: +8 -5 lines
Log Message:
Some minor but essential changes ;)

File Contents

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