KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > SSLabel


1 /* $Id: SSLabel.java,v 1.16 2005/02/21 16:31:33 prasanth Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2005, The Pangburn Company and Prasanth R. Pasala.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer. Redistributions in binary
13  * form must reproduce the above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or other materials
15  * provided with the distribution. The names of its contributors may not be
16  * used to endorse or promote products derived from this software without
17  * specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */

32
33 package com.nqadmin.swingSet;
34
35 import java.io.*;
36 import java.sql.*;
37 import com.nqadmin.swingSet.datasources.SSRowSet;
38 import java.awt.*;
39 import java.awt.event.*;
40 import javax.swing.*;
41 import javax.swing.border.*;
42 import javax.swing.text.*;
43 import javax.swing.event.*;
44 import java.beans.PropertyChangeListener JavaDoc;
45 import java.beans.PropertyChangeEvent JavaDoc;
46
47 /**
48  * SSLabel.java
49  *<p>
50  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
51  *<p><pre>
52  * Used to display database values in a read-only JLabel.
53  *</pre><p>
54  * @author $Author: prasanth $
55  * @version $Revision: 1.16 $
56  */

57 public class SSLabel extends JLabel {
58
59     /**
60      * Text field bound to the SSRowSet.
61      */

62     protected JTextField textField = new JTextField();
63
64     /**
65      * SSRowSet from which component will get/set values.
66      */

67     protected SSRowSet sSRowSet;
68
69     /**
70      * SSRowSet column to which the component will be bound.
71      */

72     protected String JavaDoc columnName = "";
73
74     /**
75      * Component listener.
76      */

77     private final MyLabelTextListener labelTextListener = new MyLabelTextListener();
78
79     /**
80      * Bound text field document listener.
81      */

82     private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener();
83
84     /**
85      * Empty constructor needed for deserialization. Creates a SSLabel instance
86      * with no image and with an empty string for the title.
87      */

88     public SSLabel() {
89         super("<label text here>");
90         init();
91     }
92
93     /**
94      * Creates a SSLabel instance with the specified image.
95      *
96      * @param _image specified image for label
97      */

98     public SSLabel(Icon _image) {
99         super(_image);
100         init();
101     }
102
103     /**
104      * Creates a SSLabel instance with the specified image and horizontal alignment.
105      *
106      * @param _image specified image for label
107      * @param _horizontalAlignment horizontal alignment
108      */

109     public SSLabel(Icon _image, int _horizontalAlignment) {
110         super(_image, _horizontalAlignment);
111         init();
112     }
113
114     /**
115      * Creates a SSLabel instance with no image and binds it to the specified
116      * SSRowSet column.
117      *
118      * @param _sSRowSet datasource to be used.
119      * @param _columnName name of the column to which this label should be bound
120      */

121     public SSLabel(SSRowSet _sSRowSet, String JavaDoc _columnName) {
122         sSRowSet = _sSRowSet;
123         columnName = _columnName;
124         init();
125         bind();
126     }
127
128     /**
129      * Sets the SSRowSet column name to which the component is bound.
130      *
131      * @param _columnName column name in the SSRowSet to which the component
132      * is bound
133      */

134     public void setColumnName(String JavaDoc _columnName) {
135         String JavaDoc oldValue = columnName;
136         columnName = _columnName;
137         firePropertyChange("columnName", oldValue, columnName);
138         bind();
139     }
140
141     /**
142      * Returns the SSRowSet column name to which the component is bound.
143      *
144      * @return column name to which the component is bound
145      */

146     public String JavaDoc getColumnName() {
147         return columnName;
148     }
149
150     /**
151      * Sets the SSRowSet to which the component is bound.
152      *
153      * @param _sSRowSet SSRowSet to which the component is bound
154      */

155     public void setSSRowSet(SSRowSet _sSRowSet) {
156         SSRowSet oldValue = sSRowSet;
157         sSRowSet = _sSRowSet;
158         firePropertyChange("sSRowSet", oldValue, sSRowSet);
159         bind();
160     }
161
162     /**
163      * Returns the SSRowSet to which the component is bound.
164      *
165      * @return SSRowSet to which the component is bound
166      */

167     public SSRowSet getSSRowSet() {
168         return sSRowSet;
169     }
170
171     /**
172      * Sets the SSRowSet and column name to which the component is to be bound.
173      *
174      * @param _sSRowSet datasource to be used.
175      * @param _columnName Name of the column to which this check box should be bound
176      */

177     public void bind(SSRowSet _sSRowSet, String JavaDoc _columnName) {
178         SSRowSet oldValue = sSRowSet;
179         sSRowSet = _sSRowSet;
180         firePropertyChange("sSRowSet", oldValue, sSRowSet);
181
182         String JavaDoc oldValue2 = columnName;
183         columnName = _columnName;
184         firePropertyChange("columnName", oldValue2, columnName);
185
186         bind();
187     }
188
189     /**
190      * Initialization code.
191      */

192     protected void init() {
193
194         // SET PREFERRED DIMENSIONS
195
setPreferredSize(new Dimension(200,20));
196     }
197
198     /**
199      * Method for handling binding of component to a SSRowSet column.
200      */

201     protected void bind() {
202
203         // CHECK FOR NULL COLUMN/ROWSET
204
if (columnName==null || columnName.trim().equals("") || sSRowSet==null) {
205                 return;
206             }
207
208         // REMOVE LISTENERS TO PREVENT DUPLICATION
209
removeListeners();
210
211         // BIND THE TEXT FIELD TO THE SPECIFIED COLUMN
212
textField.setDocument(new SSTextDocument(sSRowSet, columnName));
213
214         // SET THE LABEL DISPLAY
215
updateDisplay();
216
217         // ADD BACK LISTENERS
218
addListeners();;
219
220     }
221
222     /**
223      * Updates the value displayed in the component based on the SSRowSet column
224      * binding.
225      */

226     protected void updateDisplay() {
227
228         // SET THE LABEL BASED ON THE VALUE IN THE TEXT FIELD
229
setText(textField.getText());
230
231     } // end protected void updateDisplay() {
232

233     /**
234      * Adds listeners for component and bound text field (where applicable).
235      */

236     private void addListeners() {
237         textField.getDocument().addDocumentListener(textFieldDocumentListener);
238         addPropertyChangeListener("text", labelTextListener);
239     }
240
241     /**
242      * Removes listeners for component and bound text field (where applicable).
243      */

244     private void removeListeners() {
245         textField.getDocument().removeDocumentListener(textFieldDocumentListener);
246         removePropertyChangeListener("text", labelTextListener);
247     }
248
249     /**
250      * Listener(s) for the bound text field used to propigate values back to the
251      * component's value.
252      */

253     private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
254         public void changedUpdate(DocumentEvent de) {
255             removePropertyChangeListener("text", labelTextListener);
256
257             updateDisplay();
258
259             addPropertyChangeListener("text", labelTextListener);
260         }
261
262         // WHEN EVER THERE IS A CHANGE IN THE VALUE IN THE TEXT FIELD CHANGE THE LABEL
263
// ACCORDINGLY.
264
public void insertUpdate(DocumentEvent de) {
265             removePropertyChangeListener("text", labelTextListener);
266
267             updateDisplay();
268
269             addPropertyChangeListener("text", labelTextListener);
270         }
271
272         // IF A REMOVE UPDATE OCCURS ON THE TEXT FIELD CHECK THE CHANGE AND SET THE
273
// CHECK BOX ACCORDINGLY.
274
public void removeUpdate(DocumentEvent de) {
275             removePropertyChangeListener("text", labelTextListener);
276
277             updateDisplay();
278
279             addPropertyChangeListener("text", labelTextListener);
280         }
281     } // end private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
282

283     /**
284      * Listener(s) for the component's value used to propigate changes back to
285      * bound text field.
286      */

287     private class MyLabelTextListener implements PropertyChangeListener JavaDoc, Serializable {
288         public void propertyChange(PropertyChangeEvent JavaDoc pce) {
289             textField.getDocument().removeDocumentListener(textFieldDocumentListener);
290
291             textField.setText(getText());
292
293             textField.getDocument().addDocumentListener(textFieldDocumentListener);
294         }
295
296     } // end private class MyLabelTextListener implements ChangeListener, Serializable {
297

298 } // end public class SSLabel extends JLabel {
299

300
301
302 /*
303  * $Log: SSLabel.java,v $
304  * Revision 1.16 2005/02/21 16:31:33 prasanth
305  * In bind checking for empty columnName before binding the component.
306  *
307  * Revision 1.15 2005/02/13 15:38:20 yoda2
308  * Removed redundant PropertyChangeListener and VetoableChangeListener class variables and methods from components with JComponent as an ancestor.
309  *
310  * Revision 1.14 2005/02/12 03:29:26 yoda2
311  * Added bound properties (for beans).
312  *
313  * Revision 1.13 2005/02/11 22:59:46 yoda2
314  * Imported PropertyVetoException and added some bound properties.
315  *
316  * Revision 1.12 2005/02/11 20:16:05 yoda2
317  * Added infrastructure to support property & vetoable change listeners (for beans).
318  *
319  * Revision 1.11 2005/02/10 21:10:23 yoda2
320  * Added default label text to empty constructor so that label will be visible in BDK.
321  *
322  * Revision 1.10 2005/02/10 20:13:03 yoda2
323  * Setter/getter cleanup & method reordering for consistency.
324  *
325  * Revision 1.9 2005/02/10 03:46:47 yoda2
326  * Replaced all setDisplay() methods & calls with updateDisplay() methods & calls to prevent any setter/getter confusion.
327  *
328  * Revision 1.8 2005/02/09 17:29:55 yoda2
329  * JavaDoc cleanup.
330  *
331  * Revision 1.7 2005/02/07 20:36:38 yoda2
332  * Made private listener data members final.
333  *
334  * Revision 1.6 2005/02/05 05:16:33 yoda2
335  * API cleanup.
336  *
337  * Revision 1.5 2005/02/04 22:48:54 yoda2
338  * API cleanup & updated Copyright info.
339  *
340  * Revision 1.4 2005/02/01 17:32:38 yoda2
341  * API cleanup.
342  *
343  * Revision 1.3 2005/01/03 02:58:03 yoda2
344  * Added appropriate super() calls to non-empty constructors.
345  *
346  * Revision 1.2 2005/01/02 18:33:48 yoda2
347  * Added back empty constructor needed for deserialization along with other potentially useful constructors from parent classes.
348  *
349  * Revision 1.1 2005/01/01 05:05:47 yoda2
350  * Adding preliminary SwingSet implementations for JLabel & JSlider.
351  *
352  */

353
Popular Tags