KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SSSlider.java,v 1.15 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
45 /**
46  * SSSlider.java
47  *<p>
48  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
49  *<p><pre>
50  * Used to link a JSlider to a numeric column in a database.
51  *</pre><p>
52  * @author $Author: prasanth $
53  * @version $Revision: 1.15 $
54  */

55 public class SSSlider extends JSlider {
56
57     /**
58      * Text field bound to the SSRowSet.
59      */

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

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

70     protected String JavaDoc columnName = "";
71
72     /**
73      * Column SQL data type.
74      */

75     protected int columnType;
76
77     /**
78      * Component listener.
79      */

80     private final MySliderListener sliderListener = new MySliderListener();
81
82     /**
83      * Bound text field document listener.
84      */

85     private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener();
86
87     /**
88      * Empty constructor needed for deserialization. Creates a horizontal
89      * slider with the range 0 to 100.
90      */

91     public SSSlider() {
92         init();
93     }
94
95     /**
96      * Creates a slider using the specified orientation with the range 0 to 100.
97      *
98      * @param _orientation slider spatial orientation
99      */

100     public SSSlider(int _orientation) {
101         super(_orientation);
102         init();
103     }
104
105     /**
106      * Creates a horizontal slider using the specified min and max.
107      *
108      * @param _min minimum slider value
109      * @param _max maximum slider value
110      */

111     public SSSlider(int _min, int _max) {
112         super(_min, _max);
113         init();
114     }
115
116     /**
117      * Creates a horizontal slider with the range 0 to 100 and binds it
118      * to the specified SSRowSet column.
119      *
120      * @param _sSRowSet datasource to be used.
121      * @param _columnName name of the column to which this slider should be bound
122      */

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

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

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

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

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

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

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

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

231     protected void updateDisplay() {
232
233         // SET THE SLIDER BASED ON THE VALUE IN THE TEXT FIELD
234
switch(columnType) {
235                 case java.sql.Types.INTEGER:
236                 case java.sql.Types.SMALLINT:
237                 case java.sql.Types.TINYINT:
238                 case java.sql.Types.BIGINT:
239                 case java.sql.Types.FLOAT:
240                 case java.sql.Types.DOUBLE:
241                 case java.sql.Types.NUMERIC:
242                 // SET THE SLIDER BASED ON THE VALUE IN TEXT FIELD
243
setValue(Integer.parseInt(textField.getText()));
244                     break;
245
246                 default:
247                     break;
248             }
249
250     } // end protected void updateDisplay() {
251

252     /**
253      * Adds listeners for component and bound text field (where applicable).
254      */

255     private void addListeners() {
256         textField.getDocument().addDocumentListener(textFieldDocumentListener);
257         addChangeListener(sliderListener);
258     }
259
260     /**
261      * Removes listeners for component and bound text field (where applicable).
262      */

263     private void removeListeners() {
264         textField.getDocument().removeDocumentListener(textFieldDocumentListener);
265         removeChangeListener(sliderListener);
266     }
267
268     /**
269      * Listener(s) for the bound text field used to propigate values back to the
270      * component's value.
271      */

272     private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
273
274         public void changedUpdate(DocumentEvent de) {
275             removeChangeListener(sliderListener);
276
277             updateDisplay();
278
279             addChangeListener(sliderListener);
280         }
281
282         // WHEN EVER THERE IS A CHANGE IN THE VALUE IN THE TEXT FIELD CHANGE THE SLIDER
283
// ACCORDINGLY.
284
public void insertUpdate(DocumentEvent de) {
285             removeChangeListener(sliderListener);
286
287             updateDisplay();
288
289             addChangeListener(sliderListener);
290         }
291
292         // IF A REMOVE UPDATE OCCURS ON THE TEXT FIELD CHECK THE CHANGE AND SET THE
293
// SLIDER ACCORDINGLY.
294
public void removeUpdate(DocumentEvent de) {
295             removeChangeListener(sliderListener);
296
297             updateDisplay();
298
299             addChangeListener(sliderListener);
300         }
301
302     } // end private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
303

304     /**
305      * Listener(s) for the component's value used to propigate changes back to
306      * bound text field.
307      */

308     private class MySliderListener implements ChangeListener, Serializable {
309
310         public void stateChanged(ChangeEvent ce) {
311             textField.getDocument().removeDocumentListener(textFieldDocumentListener);
312
313             textField.setText(String.valueOf(getValue()));
314
315             textField.getDocument().addDocumentListener(textFieldDocumentListener);
316         }
317
318     } // end private class MySliderListener implements ChangeListener, Serializable {
319

320 } // end public class SSSlider extends JSlider {
321

322
323
324 /*
325  * $Log: SSSlider.java,v $
326  * Revision 1.15 2005/02/21 16:31:33 prasanth
327  * In bind checking for empty columnName before binding the component.
328  *
329  * Revision 1.14 2005/02/13 15:38:20 yoda2
330  * Removed redundant PropertyChangeListener and VetoableChangeListener class variables and methods from components with JComponent as an ancestor.
331  *
332  * Revision 1.13 2005/02/12 03:29:26 yoda2
333  * Added bound properties (for beans).
334  *
335  * Revision 1.12 2005/02/11 22:59:46 yoda2
336  * Imported PropertyVetoException and added some bound properties.
337  *
338  * Revision 1.11 2005/02/11 20:16:05 yoda2
339  * Added infrastructure to support property & vetoable change listeners (for beans).
340  *
341  * Revision 1.10 2005/02/10 20:13:03 yoda2
342  * Setter/getter cleanup & method reordering for consistency.
343  *
344  * Revision 1.9 2005/02/10 03:46:47 yoda2
345  * Replaced all setDisplay() methods & calls with updateDisplay() methods & calls to prevent any setter/getter confusion.
346  *
347  * Revision 1.8 2005/02/09 19:01:59 yoda2
348  * JavaDoc cleanup.
349  *
350  * Revision 1.7 2005/02/07 20:36:40 yoda2
351  * Made private listener data members final.
352  *
353  * Revision 1.6 2005/02/05 05:16:33 yoda2
354  * API cleanup.
355  *
356  * Revision 1.5 2005/02/04 23:05:10 yoda2
357  * no message
358  *
359  * Revision 1.4 2005/02/04 22:48:54 yoda2
360  * API cleanup & updated Copyright info.
361  *
362  * Revision 1.3 2005/01/03 02:58:04 yoda2
363  * Added appropriate super() calls to non-empty constructors.
364  *
365  * Revision 1.2 2005/01/02 18:33:49 yoda2
366  * Added back empty constructor needed for deserialization along with other potentially useful constructors from parent classes.
367  *
368  * Revision 1.1 2005/01/01 05:05:48 yoda2
369  * Adding preliminary SwingSet implementations for JLabel & JSlider.
370  *
371  */

372
Popular Tags