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.19
Committed: Tue May 21 16:47:10 2002 UTC (22 years ago) by tdb
Branch: MAIN
Changes since 1.18: +3 -2 lines
Log Message:
Added URL to GPL headers.

File Contents

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