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/StringDataComponent.java
Revision: 1.18
Committed: Sat May 18 18:15:56 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.17: +22 -3 lines
Log Message:
i-scream is now licensed under the GPL. I've added the GPL headers to every
source file, and put a full copy of the license in the appropriate places.
I think I've covered everything. This is going to be a mad commit ;)

File Contents

# Content
1 /*
2 * i-scream central monitoring system
3 * Copyright (C) 2000-2002 i-scream
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 //---PACKAGE DECLARATION---
21 package uk.org.iscream.cms.conient.datacomponents;
22
23 //---IMPORTS---
24 import javax.swing.JLabel;
25 import javax.swing.JTextField;
26 import java.awt.GridLayout;
27 import javax.swing.SwingUtilities;
28 import uk.org.iscream.cms.server.util.XMLPacket;
29
30 /**
31 * This is the most basic of DataComponents.
32 *
33 * It simply displays the value as a String
34 * in a JTextField.
35 *
36 * @author $Author: tdb $
37 * @version $Id: StringDataComponent.java,v 1.17 2001/05/29 17:41:32 tdb Exp $
38 */
39 public class StringDataComponent extends VisibleDataComponent {
40
41 //---FINAL ATTRIBUTES---
42
43 /**
44 * The current CVS revision of this class
45 */
46 public final String REVISION = "$Revision: 1.17 $";
47
48 /**
49 * The default length of the JTextField
50 */
51 protected final int DEFAULT_TEXT_LENGTH = 20;
52
53 //---STATIC METHODS---
54
55 //---CONSTRUCTORS---
56
57 /**
58 * Creates the component with a friendly name to be
59 * used as label
60 *
61 * @param name the friendly name
62 * @param attribute the data attribute we look after
63 */
64 public StringDataComponent(String name, String attribute) {
65 _name = name;
66 _attribute = attribute;
67 _item = new JTextField("", DEFAULT_TEXT_LENGTH);
68 _item.setEditable(false);
69 _label = new JLabel(_name + ": ");
70 _label.setHorizontalAlignment(JLabel.RIGHT);
71 setLayout(new GridLayout(1, 2));
72 _item.setText("-uninitialised-");
73 add(_label);
74 add(_item);
75 setVisible(false);
76 }
77
78 //---PUBLIC METHODS---
79
80 /**
81 * This run method updates any Swing components
82 * The setValue() method adds this component
83 * to the Swing Event Dispatching Queue to
84 * run this method.
85 */
86 public void run() {
87 if(!isVisible()) {
88 setVisible(true);
89 }
90 _item.setText(_cache);
91 }
92
93 /**
94 * Overrides the {@link java.lang.Object#toString() Object.toString()}
95 * method to provide clean logging (every class should have this).
96 *
97 * @return the name of this class and its CVS revision
98 */
99 public String toString() {
100 return _name + "(" + _attribute + ")";
101 }
102
103 //---PRIVATE METHODS---
104
105 //---ACCESSOR/MUTATOR METHODS---
106
107 /**
108 * This takes the packet to obtain the value from, it then performs all
109 * approriate conversions and adds this class to the Swing Event
110 * Dispatching queue.
111 *
112 * @param packet the XMLPacket to get the data from
113 * @throws DataFormatException if there was a problem converting the data for display
114 */
115 public void setValue(XMLPacket packet) throws DataFormatException {
116 String value = packet.getParam(_attribute);
117 try {
118 if(!_cache.equals(value)) {
119 _cache = value;
120 SwingUtilities.invokeLater(this);
121 }
122 } catch (Exception e) {
123 throw new DataFormatException(value + " is an invalid data type for " + toString());
124 }
125 }
126
127 /**
128 * Returns the string showing the packet
129 * attribute that the component is looking after
130 *
131 * @return the packet reference
132 */
133 public String getPacketAttribute() {
134 return _attribute;
135 }
136
137 /**
138 * Returns the string showing the packet
139 * data that the component is looking after
140 *
141 * @return the packet reference
142 */
143 public String getValue() {
144 return _cache;
145 }
146
147 //---ATTRIBUTES---
148
149 /**
150 * The friendly name of this component
151 */
152 protected String _name;
153
154 /**
155 * The attribute that this component is concerned with
156 */
157 protected String _attribute;
158
159 /**
160 * The friendly label for this component
161 */
162 protected JLabel _label;
163
164 /**
165 * Remembers what the last value was, so we
166 * only update if we have to.
167 */
168 protected String _cache = "";
169
170 /**
171 * The length of the JTextField
172 */
173 protected int _displayLength;
174
175 /**
176 * Just a normal label to display our value as a String
177 */
178 protected JTextField _item;
179
180 //---STATIC ATTRIBUTES---
181
182 }