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

# User Rev Content
1 tdb 1.18 /*
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 ajm 1.2 //---PACKAGE DECLARATION---
21 tdb 1.17 package uk.org.iscream.cms.conient.datacomponents;
22 ajm 1.2
23     //---IMPORTS---
24 ajm 1.1 import javax.swing.JLabel;
25 ajm 1.3 import javax.swing.JTextField;
26 ajm 1.1 import java.awt.GridLayout;
27 ajm 1.8 import javax.swing.SwingUtilities;
28 tdb 1.17 import uk.org.iscream.cms.server.util.XMLPacket;
29 ajm 1.1
30 ajm 1.2 /**
31     * This is the most basic of DataComponents.
32     *
33     * It simply displays the value as a String
34 ajm 1.5 * in a JTextField.
35 ajm 1.2 *
36 tdb 1.18 * @author $Author: tdb $
37     * @version $Id: StringDataComponent.java,v 1.17 2001/05/29 17:41:32 tdb Exp $
38 ajm 1.2 */
39 ajm 1.8 public class StringDataComponent extends VisibleDataComponent {
40 ajm 1.2
41     //---FINAL ATTRIBUTES---
42    
43     /**
44     * The current CVS revision of this class
45     */
46 tdb 1.18 public final String REVISION = "$Revision: 1.17 $";
47 ajm 1.5
48     /**
49     * The default length of the JTextField
50     */
51 ajm 1.11 protected final int DEFAULT_TEXT_LENGTH = 20;
52 ajm 1.2
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 ajm 1.5 * @param attribute the data attribute we look after
63     */
64     public StringDataComponent(String name, String attribute) {
65     _name = name;
66     _attribute = attribute;
67 ajm 1.7 _item = new JTextField("", DEFAULT_TEXT_LENGTH);
68 ajm 1.5 _item.setEditable(false);
69     _label = new JLabel(_name + ": ");
70 ajm 1.1 _label.setHorizontalAlignment(JLabel.RIGHT);
71     setLayout(new GridLayout(1, 2));
72 ajm 1.11 _item.setText("-uninitialised-");
73 ajm 1.1 add(_label);
74     add(_item);
75 ajm 1.16 setVisible(false);
76 ajm 1.1 }
77    
78 ajm 1.2 //---PUBLIC METHODS---
79    
80 ajm 1.5 /**
81 ajm 1.8 * 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 ajm 1.16 if(!isVisible()) {
88     setVisible(true);
89     }
90 ajm 1.15 _item.setText(_cache);
91 ajm 1.8 }
92    
93     /**
94 ajm 1.5 * 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 ajm 1.2 //---PRIVATE METHODS---
104    
105     //---ACCESSOR/MUTATOR METHODS---
106    
107     /**
108 ajm 1.15 * This takes the packet to obtain the value from, it then performs all
109 ajm 1.8 * approriate conversions and adds this class to the Swing Event
110     * Dispatching queue.
111 ajm 1.2 *
112 ajm 1.15 * @param packet the XMLPacket to get the data from
113 ajm 1.2 * @throws DataFormatException if there was a problem converting the data for display
114     */
115 ajm 1.15 public void setValue(XMLPacket packet) throws DataFormatException {
116     String value = packet.getParam(_attribute);
117 ajm 1.1 try {
118 ajm 1.4 if(!_cache.equals(value)) {
119     _cache = value;
120 ajm 1.12 SwingUtilities.invokeLater(this);
121 ajm 1.4 }
122 ajm 1.1 } catch (Exception e) {
123 ajm 1.5 throw new DataFormatException(value + " is an invalid data type for " + toString());
124 ajm 1.1 }
125 ajm 1.11 }
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 ajm 1.13 }
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 ajm 1.11 }
146 ajm 1.2
147     //---ATTRIBUTES---
148    
149     /**
150 ajm 1.5 * The friendly name of this component
151     */
152 ajm 1.11 protected String _name;
153 ajm 1.5
154     /**
155     * The attribute that this component is concerned with
156     */
157 ajm 1.11 protected String _attribute;
158 ajm 1.5
159     /**
160 ajm 1.2 * The friendly label for this component
161     */
162     protected JLabel _label;
163 ajm 1.4
164 ajm 1.5 /**
165 ajm 1.15 * Remembers what the last value was, so we
166 ajm 1.5 * only update if we have to.
167     */
168 ajm 1.15 protected String _cache = "";
169 ajm 1.1
170 ajm 1.5 /**
171     * The length of the JTextField
172     */
173     protected int _displayLength;
174 ajm 1.3
175 ajm 1.2 /**
176     * Just a normal label to display our value as a String
177     */
178 ajm 1.5 protected JTextField _item;
179 ajm 1.2
180     //---STATIC ATTRIBUTES---
181    
182 ajm 1.8 }