KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* $Id: SSCheckBox.java,v 1.15 2005/02/21 16:31:18 prasanth 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  * SSCheckBox.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 SSCheckBox
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 SSCheckBox returns a '1' to the database and
54  * an uncheck SSCheckBox 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  *
58  * Note that for naming consistency, SSCheckBox replaced SSDBCheckBox 01-10-2005.
59  *</pre><p>
60  * @author $Author: prasanth $
61  * @version $Revision: 1.15 $
62  */

63 public class SSCheckBox extends JCheckBox {
64
65     /**
66      * Text field bound to the SSRowSet.
67      */

68     protected JTextField textField = new JTextField();
69
70     /**
71      * SSRowSet from which component will get/set values.
72      */

73     protected SSRowSet sSRowSet;
74
75     /**
76      * SSRowSet column to which the component will be bound.
77      */

78     protected String JavaDoc columnName = "";
79
80     /**
81      * Column SQL data type.
82      */

83     protected int columnType = java.sql.Types.BIT;
84
85     /**
86      * Component listener.
87      */

88     private final MyCheckBoxListener checkBoxListener = new MyCheckBoxListener();
89     
90     /**
91      * Bound text field document listener.
92      */

93     private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener();
94
95     /**
96      * Checked value for numeric columns.
97      */

98     protected int CHECKED = 1;
99     
100     /**
101      * Unchecked value for numeric columns.
102      */

103     protected int UNCHECKED = 0;
104
105     /**
106      * Checked value for Boolean columns.
107      */

108     protected static String JavaDoc BOOLEAN_CHECKED = "true";
109     
110     /**
111      * Unchecked value for Boolean columns.
112      */

113     protected static String JavaDoc BOOLEAN_UNCHECKED = "false";
114
115     /**
116      * Creates an object of SSCheckBox.
117      */

118     public SSCheckBox() {
119         init();
120     }
121
122     /**
123      * Creates an object of SSCheckBox binding it so the specified column
124      * in the given SSRowSet.
125      *
126      * @param _sSRowSet datasource to be used.
127      * @param _columnName name of the column to which this check box should be bound
128      */

129     public SSCheckBox(SSRowSet _sSRowSet, String JavaDoc _columnName) throws java.sql.SQLException JavaDoc {
130         sSRowSet = _sSRowSet;
131         columnName = _columnName;
132         init();
133         bind();
134     }
135     
136     /**
137      * Sets the SSRowSet column name to which the component is bound.
138      *
139      * @param _columnName column name in the SSRowSet to which the component
140      * is bound
141      */

142     public void setColumnName(String JavaDoc _columnName) throws java.sql.SQLException JavaDoc {
143         String JavaDoc oldValue = columnName;
144         columnName = _columnName;
145         firePropertyChange("columnName", oldValue, columnName);
146         bind();
147     }
148     
149     /**
150      * Returns the SSRowSet column name to which the component is bound.
151      *
152      * @return column name to which the component is bound
153      */

154     public String JavaDoc getColumnName() {
155         return columnName;
156     }
157
158     /**
159      * Sets the SSRowSet to which the component is bound.
160      *
161      * @param _sSRowSet SSRowSet to which the component is bound
162      */

163     public void setSSRowSet(SSRowSet _sSRowSet) throws java.sql.SQLException JavaDoc {
164         SSRowSet oldValue = sSRowSet;
165         sSRowSet = _sSRowSet;
166         firePropertyChange("sSRowSet", oldValue, sSRowSet);
167         bind();
168     }
169     
170     /**
171      * Returns the SSRowSet to which the component is bound.
172      *
173      * @return SSRowSet to which the component is bound
174      */

175     public SSRowSet getSSRowSet() {
176         return sSRowSet;
177     }
178
179     /**
180      * Sets the SSRowSet and column name to which the component is to be bound.
181      *
182      * @param _sSRowSet datasource to be used.
183      * @param _columnName Name of the column to which this check box should be bound
184      */

185     public void bind(SSRowSet _sSRowSet, String JavaDoc _columnName) throws java.sql.SQLException JavaDoc {
186         SSRowSet oldValue = sSRowSet;
187         sSRowSet = _sSRowSet;
188         //pChangeSupport.firePropertyChange("sSRowSet", oldValue, sSRowSet);
189
firePropertyChange("sSRowSet", oldValue, sSRowSet);
190         
191         String JavaDoc oldValue2 = columnName;
192         columnName = _columnName;
193         //pChangeSupport.firePropertyChange("columnName", oldValue2, columnName);
194
firePropertyChange("columnName", oldValue2, columnName);
195         
196         bind();
197     }
198     
199     /**
200      * Initialization code.
201      */

202     protected void init() {
203         // ADD KEY LISTENER TO TRANSFER FOCUS TO NEXT ELEMENT WHEN ENTER
204
// KEY IS PRESSED.
205
//cmbDisplayed.addKeyListener(new KeyAdapter() {
206
addKeyListener(new KeyAdapter() {
207                 public void keyReleased(KeyEvent ke) {
208                     if (ke.getKeyCode() == KeyEvent.VK_ENTER) {
209                         ((Component)ke.getSource()).transferFocus();
210                     }
211                 }
212             });
213             
214         // SET PREFERRED AND MAXIMUM DIMENSIONS
215
setPreferredSize(new Dimension(20,20));
216             setMaximumSize(new Dimension(20,20));
217     }
218     
219     /**
220      * Method for handling binding of component to a SSRowSet column.
221      */

222     protected void bind() throws java.sql.SQLException JavaDoc {
223         
224         // CHECK FOR NULL COLUMN/ROWSET
225
if (columnName==null || columnName.trim().equals("") || sSRowSet==null) {
226                 return;
227             }
228             
229         // REMOVE LISTENERS TO PREVENT DUPLICATION
230
removeListeners();
231
232         // DETERMINE COLUMN TYPE
233
columnType = sSRowSet.getColumnType(columnName);
234
235         // BIND THE TEXT FIELD TO THE SPECIFIED COLUMN
236
textField.setDocument(new SSTextDocument(sSRowSet, columnName));
237
238         // SET THE COMBO BOX ITEM DISPLAYED
239
updateDisplay();
240             
241         // ADD BACK LISTENERS
242
addListeners();
243                
244     }
245     
246     /**
247      * Adds listeners for component and bound text field (where applicable).
248      */

249     private void addListeners() {
250         textField.getDocument().addDocumentListener(textFieldDocumentListener);
251         //cmbDisplayed.addActionListener(cmbListener);
252
addChangeListener(checkBoxListener);
253     }
254
255     /**
256      * Removes listeners for component and bound text field (where applicable).
257      */

258     private void removeListeners() {
259         textField.getDocument().removeDocumentListener(textFieldDocumentListener);
260         //cmbDisplayed.removeActionListener(cmbListener);
261
removeChangeListener(checkBoxListener);
262     }
263
264
265     /**
266      * Updates the value displayed in the component based on the SSRowSet column
267      * binding.
268      */

269     protected void updateDisplay() {
270             
271         // SELECT/DESELECT BASED ON UNDERLYING SQL TYPE
272
switch(columnType) {
273                 case java.sql.Types.INTEGER:
274                 case java.sql.Types.SMALLINT:
275                 case java.sql.Types.TINYINT:
276             // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
277
if (textField.getText().equals(String.valueOf(CHECKED))) {
278                         setSelected(true);
279                     } else {
280                         setSelected(false);
281                     }
282                     break;
283
284                 case java.sql.Types.BIT:
285                 case java.sql.Types.BOOLEAN:
286             // SET THE CHECK BOX BASED ON THE VALUE IN TEXT FIELD
287
if (textField.getText().equals(BOOLEAN_CHECKED)) {
288                         setSelected(true);
289                     } else {
290                         setSelected(false);
291                     }
292                     break;
293
294                 default:
295                     break;
296             }
297
298     } // end protected void updateDisplay() {
299

300     /**
301      * Listener(s) for the bound text field used to propigate values back to the
302      * component's value.
303      */

304     private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
305
306         public void changedUpdate(DocumentEvent de){
307             removeChangeListener(checkBoxListener);
308             
309             updateDisplay();
310             
311             addChangeListener(checkBoxListener);
312         }
313
314         // WHEN EVER THERE IS A CHANGE IN THE VALUE IN THE TEXT FIELD CHANGE THE CHECK BOX
315
// ACCORDINGLY.
316
public void insertUpdate(DocumentEvent de) {
317             removeChangeListener(checkBoxListener);
318             
319             updateDisplay();
320             
321             addChangeListener( checkBoxListener );
322         }
323
324         // IF A REMOVE UPDATE OCCURS ON THE TEXT FIELD CHECK THE CHANGE AND SET THE
325
// CHECK BOX ACCORDINGLY.
326
public void removeUpdate(DocumentEvent de) {
327             removeChangeListener(checkBoxListener);
328             
329             updateDisplay();
330             
331             addChangeListener( checkBoxListener );
332         }
333     } // end private class MyTextFieldDocumentListener implements DocumentListener, Serializable {
334

335     /**
336      * Listener(s) for the component's value used to propigate changes back to
337      * bound text field.
338      */

339     private class MyCheckBoxListener implements ChangeListener, Serializable {
340
341         public void stateChanged(ChangeEvent ce) {
342             textField.getDocument().removeDocumentListener(textFieldDocumentListener);
343
344             if ( ((JCheckBox)ce.getSource()).isSelected() ) {
345                 switch(columnType) {
346                     case java.sql.Types.INTEGER:
347                     case java.sql.Types.SMALLINT:
348                     case java.sql.Types.TINYINT:
349                         textField.setText(String.valueOf(CHECKED));
350                         break;
351                     case java.sql.Types.BIT:
352                     case java.sql.Types.BOOLEAN:
353                         textField.setText(BOOLEAN_CHECKED);
354                         break;
355                 }
356             } else {
357                 switch(columnType) {
358                     case java.sql.Types.INTEGER:
359                     case java.sql.Types.SMALLINT:
360                     case java.sql.Types.TINYINT:
361                         textField.setText(String.valueOf(UNCHECKED));
362                         break;
363                     case java.sql.Types.BIT:
364                     case java.sql.Types.BOOLEAN:
365                         textField.setText(BOOLEAN_UNCHECKED);
366                         break;
367                 }
368             }
369
370             textField.getDocument().addDocumentListener(textFieldDocumentListener);
371         }
372
373     } // end private class MyCheckBoxListener implements ChangeListener, Serializable {
374

375         
376         
377 // DEPRECATED STUFF....................
378

379     /**
380      * Creates a object of SSCheckBox which synchronizes with the value in the specified
381      * text field.
382      *
383      * @param _textField the text field with which the check box will be in sync.
384      *
385      * @deprecated
386      */

387     public SSCheckBox(JTextField _textField) {
388         textField = _textField;
389     }
390
391     /**
392      * Initializes the check box by getting the value corresponding to
393      * specified column from the SSRowSet.
394      *
395      * @deprecated
396      */

397     public void execute() {
398         //init();
399
}
400         
401
402 } // end public class SSCheckBox extends JCheckBox {
403

404
405
406 /*
407  * $Log: SSCheckBox.java,v $
408  * Revision 1.15 2005/02/21 16:31:18 prasanth
409  * In bind checking for empty columnName before binding the component.
410  *
411  * Revision 1.14 2005/02/13 15:38:19 yoda2
412  * Removed redundant PropertyChangeListener and VetoableChangeListener class variables and methods from components with JComponent as an ancestor.
413  *
414  * Revision 1.13 2005/02/11 22:59:23 yoda2
415  * Imported PropertyVetoException and added some bound properties.
416  *
417  * Revision 1.12 2005/02/11 20:15:57 yoda2
418  * Added infrastructure to support property & vetoable change listeners (for beans).
419  *
420  * Revision 1.11 2005/02/10 20:12:36 yoda2
421  * Setter/getter cleanup & method reordering for consistency.
422  *
423  * Revision 1.10 2005/02/10 03:46:47 yoda2
424  * Replaced all setDisplay() methods & calls with updateDisplay() methods & calls to prevent any setter/getter confusion.
425  *
426  * Revision 1.9 2005/02/07 20:36:33 yoda2
427  * Made private listener data members final.
428  *
429  * Revision 1.8 2005/02/05 18:13:29 yoda2
430  * JavaDoc cleanup.
431  *
432  * Revision 1.7 2005/02/05 05:16:33 yoda2
433  * API cleanup.
434  *
435  * Revision 1.6 2005/02/04 22:48:52 yoda2
436  * API cleanup & updated Copyright info.
437  *
438  * Revision 1.5 2005/02/02 23:37:19 yoda2
439  * API cleanup.
440  *
441  * Revision 1.4 2005/02/01 17:32:37 yoda2
442  * API cleanup.
443  *
444  * Revision 1.3 2005/01/21 22:55:00 yoda2
445  * API cleanup.
446  *
447  * Revision 1.2 2005/01/19 20:54:43 yoda2
448  * API cleanup.
449  *
450  * Revision 1.1 2005/01/10 15:09:11 yoda2
451  * Added to replace deprecated SSDBCheckBox to match naming conventions.
452  *
453  */

454
Popular Tags