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
(Generate patch)

Comparing projects/cms/source/conient/uk/org/iscream/cms/conient/datacomponents/StringDataComponent.java (file contents):
Revision 1.2 by ajm, Mon Jan 22 12:32:14 2001 UTC vs.
Revision 1.15 by ajm, Sun Mar 18 14:43:39 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines