KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SSDBCheckBox.java,v 1.12 2005/02/04 22:48:53 yoda2 Exp $
2  *
3  * Tab Spacing = 4
4  *
5  * Copyright (c) 2003-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  * SSDBCheckBox.java
47  *<p>
48  * SwingSet - Open Toolkit For Making Swing Controls Database-Aware
49  *<p><pre>
50  * Used to display the boolean values stored in the database. The SSDBCheckBox
51  * can be bound to a numeric or boolean database column. Currently, binding to
52  * a boolean column has been tested only with postgresql. If bound to a numeric
53  * database column, a checked SSDBCheckBox returns a '1' to the database and
54  * an uncheck SSDBCheckBox will returns a '0'. In the future an option may be
55  * added to allow the user to specify the values returned for the checked and
56  * unchecked checkbox states.
57  *</pre><p>
58  *
59  * @deprecated
60  * @see SSCheckBox
61  *
62  * @author $Author: yoda2 $
63  * @version $Revision: 1.12 $
64  */

65 public class SSDBCheckBox extends JCheckBox {
66
67     // TEXT FIELD BOUND TO THE DATABASE
68
private JTextField textField = new JTextField();
69
70     private String JavaDoc columnName;
71     private int columnType = java.sql.Types.BIT;
72
73     // LISTENER FOR CHECK BOX AND TEXT FEILD
74
private MyCheckBoxListener checkBoxListener = new MyCheckBoxListener();
75     private MyTextFieldListener textFieldListener = new MyTextFieldListener();
76
77     // INITIALIZE CHECKED AND UNCHECKED VALUES
78
private int CHECKED = 1;
79     private int UNCHECKED = 0;
80
81     // INITIALIZE CHECKED AND UNCHECKED VALUES FOR BOOLEAN COLUMN
82
private static String JavaDoc BOOLEAN_CHECKED = "true";
83     private static String JavaDoc BOOLEAN_UNCHECKED = "false";
84
85     /**
86      * Creates a object of SSDBCheckBox which synchronizes with the value in the specified
87      * text field.
88      *
89      * @param _textField the text field with which the check box will be in sync.
90      *
91      * @deprecated
92      */

93     public SSDBCheckBox(JTextField _textField) {
94         textField = _textField;
95     }
96
97     /**
98      * Creates an object of SSDBCheckBox.
99      */

100     public SSDBCheckBox() {
101         textField = new JTextField();
102     }
103
104     /**
105      * Creates an object of SSDBCheckBox binding it so the specified column
106      * in the given SSRowSet.
107      *
108      * @param _rowset datasource to be used.
109      * @param _columnName name of the column to which this check box should be bound
110      */

111     public SSDBCheckBox(SSRowSet _rowset, String JavaDoc _columnName) throws java.sql.SQLException JavaDoc {
112         columnName = _columnName;
113         textField.setDocument(new SSTextDocument(_rowset, _columnName));
114         columnType = _rowset.getColumnType(_columnName);
115     }
116
117     /**
118      * Sets the text field with which the check box has to be synchronized.
119      *
120      * @param _textField the text field with which the check box will be in sync.
121      *
122      * @deprecated
123      * @see #bind
124      */

125     public void setTextField(JTextField _textField) {
126         // IF THE OLD ONE IS NOT NULL REMOVE ANY LISTENERS BEING ADDED.
127
// IT DOES NOT HURT TO CALL REMOVE IF WE ADDED ONE THEN IT WILL BE DELETED.
128
// ELSE NOTHING HAPPENS
129
if (textField != null) {
130                 textField.getDocument().removeDocumentListener(textFieldListener);
131             }
132
133             textField = _textField;
134     }
135
136     /**
137      * Sets the datasource and the columnName in the datasource to which the
138      * SSCheckBox has to be bound to.
139      *
140      * @param _rowset datasource to be used.
141      * @param _columnName Name of the column to which this check box should be bound
142      */

143     public void bind(SSRowSet _rowset, String JavaDoc _columnName) throws java.sql.SQLException JavaDoc {
144         columnName = _columnName;
145         textField.setDocument(new SSTextDocument(_rowset, _columnName));
146         columnType = _rowset.getColumnType(_columnName);
147     }
148
149     /**
150      * returns the column name to which this check box is bound to.
151      *
152      * @return column name to which the check box is bound.
153      */

154     public String JavaDoc getColumnName() {
155         return columnName;
156     }
157
158     /**
159      * Returns the text field with which the check box is synchronizing.
160      *
161      * @return returns a JTextField which is used to set the check box.
162      *
163      * @deprecated
164      */

165     public JTextField getTextField() {
166         return textField;
167     }
168
169     /**
170      * Initializes the check box by getting the value corresponding to
171      * specified column from the SSRowSet.
172      */

173     public void execute() {
174         initCheckBox();
175     }
176
177
178     // INITIALIZES THE CHECK BOX.
179
private void initCheckBox() {
180
181         // ADD LISTENER FOR THE TEXT FIELD
182
textField.getDocument().addDocumentListener(textFieldListener);
183
184             switch(columnType) {
185                 case java.sql.Types.INTEGER:
186                 case java.sql.Types.SMALLINT:
187                 case java.sql.Types.TINYINT:
188             // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
189
if ( textField.getText().equals(String.valueOf(CHECKED)) ) {
190                         setSelected(true);
191                     } else {
192                         setSelected(false);
193                     }
194                     break;
195
196                 case java.sql.Types.BIT:
197                 case java.sql.Types.BOOLEAN:
198             // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
199
if ( textField.getText().equals(BOOLEAN_CHECKED) ) {
200                         setSelected(true);
201                     } else {
202                         setSelected(false);
203                     }
204                     break;
205
206                 default:
207                     break;
208             }
209
210         //ADD LISTENER FOR THE CHECK BOX.
211
// REMOVE HAS TO BE CALLED SO MAKE SURE THAT YOU ARE NOT STACKING UP
212
// LISTENERS WHEN EXECUTE IS CALLED MULTIPLE TIMES.
213
removeChangeListener(checkBoxListener);
214             addChangeListener( checkBoxListener );
215     } // end private void initCheckBox() {
216

217     // LISTENER FOR THE TEXT FIELD
218
private class MyTextFieldListener implements DocumentListener, Serializable {
219         private void readObject(ObjectInputStream objIn) throws IOException, ClassNotFoundException JavaDoc {
220             objIn.defaultReadObject();
221         }
222
223         private void writeObject(ObjectOutputStream objOut) throws IOException {
224             objOut.defaultWriteObject();
225         }
226
227         public void changedUpdate(DocumentEvent de){
228             removeChangeListener(checkBoxListener);
229
230             switch(columnType) {
231                 case java.sql.Types.INTEGER:
232                 case java.sql.Types.SMALLINT:
233                 case java.sql.Types.TINYINT:
234                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
235
if ( textField.getText().equals(String.valueOf(CHECKED)) ) {
236                         setSelected(true);
237                     } else {
238                         setSelected(false);
239                     }
240                     break;
241
242                 case java.sql.Types.BIT:
243                 case java.sql.Types.BOOLEAN:
244                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
245
if ( textField.getText().equals(BOOLEAN_CHECKED) ) {
246                         setSelected(true);
247                     } else {
248                         setSelected(false);
249                     }
250                     break;
251
252                 default:
253                     break;
254             }
255             addChangeListener( checkBoxListener );
256         }
257
258         // WHEN EVER THERE IS A CHANGE IN THE VALUE IN THE TEXT FIELD CHANGE THE CHECK BOX
259
// ACCORDINGLY.
260
public void insertUpdate(DocumentEvent de) {
261             removeChangeListener( checkBoxListener );
262
263             switch(columnType) {
264                 case java.sql.Types.INTEGER:
265                 case java.sql.Types.SMALLINT:
266                 case java.sql.Types.TINYINT:
267                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
268
if ( textField.getText().equals(String.valueOf(CHECKED)) ) {
269                         setSelected(true);
270                     } else {
271                         setSelected(false);
272                     }
273                     break;
274
275                 case java.sql.Types.BIT:
276                 case java.sql.Types.BOOLEAN:
277                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
278
if ( textField.getText().equals(BOOLEAN_CHECKED) ) {
279                         setSelected(true);
280                     } else {
281                         setSelected(false);
282                     }
283                     break;
284
285                 default:
286                     break;
287             }
288             addChangeListener( checkBoxListener );
289         }
290
291         // IF A REMOVE UPDATE OCCURS ON THE TEXT FIELD CHECK THE CHANGE AND SET THE
292
// CHECK BOX ACCORDINGLY.
293
public void removeUpdate(DocumentEvent de) {
294             removeChangeListener( checkBoxListener );
295
296             switch(columnType) {
297                 case java.sql.Types.INTEGER:
298                 case java.sql.Types.SMALLINT:
299                 case java.sql.Types.TINYINT:
300                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
301
if ( textField.getText().equals(String.valueOf(CHECKED)) ) {
302                         setSelected(true);
303                     } else {
304                         setSelected(false);
305                     }
306                     break;
307
308                 case java.sql.Types.BIT:
309                 case java.sql.Types.BOOLEAN:
310                 // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
311
if ( textField.getText().equals(BOOLEAN_CHECKED) ) {
312                         setSelected(true);
313                     } else {
314                         setSelected(false);
315                     }
316                     break;
317
318                 default:
319                     break;
320             }
321             addChangeListener( checkBoxListener );
322         }
323     } // end private class MyTextFieldListener implements DocumentListener, Serializable {
324

325     // LISTENER FOR THE CHECK BOX.
326
// ANY CHANGES MADE TO THE CHECK BOX BY THE USER ARE PROPOGATED BACK TO THE
327
// TEXT FIELD FOR FURTHER PROPOGATION TO THE UNDERLYING STORAGE STRUCTURE.
328
private class MyCheckBoxListener implements ChangeListener, Serializable {
329
330         private void readObject(ObjectInputStream objIn) throws IOException, ClassNotFoundException JavaDoc {
331             objIn.defaultReadObject();
332         }
333
334         private void writeObject(ObjectOutputStream objOut) throws IOException {
335             objOut.defaultWriteObject();
336         }
337
338         public void stateChanged(ChangeEvent ce) {
339             textField.getDocument().removeDocumentListener(textFieldListener);
340
341             if ( ((JCheckBox)ce.getSource()).isSelected() ) {
342                 switch(columnType) {
343                     case java.sql.Types.INTEGER:
344                     case java.sql.Types.SMALLINT:
345                     case java.sql.Types.TINYINT:
346                         textField.setText(String.valueOf(CHECKED));
347                         break;
348                     case java.sql.Types.BIT:
349                     case java.sql.Types.BOOLEAN:
350                         textField.setText(BOOLEAN_CHECKED);
351                         break;
352                 }
353             } else {
354                 switch(columnType) {
355                     case java.sql.Types.INTEGER:
356                     case java.sql.Types.SMALLINT:
357                     case java.sql.Types.TINYINT:
358                         textField.setText(String.valueOf(UNCHECKED));
359                         break;
360                     case java.sql.Types.BIT:
361                     case java.sql.Types.BOOLEAN:
362                         textField.setText(BOOLEAN_UNCHECKED);
363                         break;
364                 }
365             }
366
367             textField.getDocument().addDocumentListener(textFieldListener);
368         }
369
370     } // end private class MyCheckBoxListener implements ChangeListener, Serializable {
371

372 } // end public class SSDBCheckBox extends JCheckBox {
373

374
375
376 /*
377  * $Log: SSDBCheckBox.java,v $
378  * Revision 1.12 2005/02/04 22:48:53 yoda2
379  * API cleanup & updated Copyright info.
380  *
381  * Revision 1.11 2005/01/10 15:08:05 yoda2
382  * deprecated in favor of SSCheckBox to match naming conventions.
383  *
384  * Revision 1.10 2004/11/11 14:45:48 yoda2
385  * Using TextPad, converted all tabs to "soft" tabs comprised of four actual spaces.
386  *
387  * Revision 1.9 2004/11/10 22:45:50 dags
388  * Added support for boolean bound column. Tested only in postgresql.
389  *
390  * Revision 1.8 2004/11/01 15:53:30 yoda2
391  * Fixed various JavaDoc errors.
392  *
393  * Revision 1.7 2004/10/25 22:03:18 yoda2
394  * Updated JavaDoc for new datasource abstraction layer in 0.9.0 release.
395  *
396  * Revision 1.6 2004/10/25 19:51:03 prasanth
397  * Modified to use the new SSRowSet instead of RowSet.
398  *
399  * Revision 1.5 2004/08/10 22:06:59 yoda2
400  * Added/edited JavaDoc, made code layout more uniform across classes, made various small coding improvements suggested by PMD.
401  *
402  * Revision 1.4 2004/08/02 15:13:43 prasanth
403  * 1. Deprecated getTextField, setTextField and constructor which takes a
404  * TextField.
405  * 2. Added constructor which takes a rowset and column name.
406  * 3. Also added bind(rowset, columnname).
407  * 4. Class implements Serializable.
408  *
409  * Revision 1.3 2004/03/08 16:43:37 prasanth
410  * Updated copy right year.
411  *
412  * Revision 1.2 2003/09/25 14:27:45 yoda2
413  * Removed unused Import statements and added preformatting tags to JavaDoc descriptions.
414  *
415  * Revision 1.1.1.1 2003/09/25 13:56:43 yoda2
416  * Initial CVS import for SwingSet.
417  *
418  */

419
Popular Tags