KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nqadmin > swingSet > formatting > SSBooleanField


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

33
34 package com.nqadmin.swingSet.formatting;
35
36 import com.nqadmin.swingSet.SSDataNavigator;
37 import com.nqadmin.swingSet.datasources.SSRowSet;
38 import java.awt.KeyboardFocusManager JavaDoc;
39 import java.awt.event.KeyEvent JavaDoc;
40 import java.awt.event.KeyListener JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Set JavaDoc;
43 import javax.sql.RowSetListener JavaDoc;
44 import javax.swing.InputVerifier JavaDoc;
45 import javax.swing.JCheckBox JavaDoc;
46 import javax.swing.JComponent JavaDoc;
47 import javax.swing.KeyStroke JavaDoc;
48
49 /**
50  *
51  * @author dags
52  */

53 public class SSBooleanField extends JCheckBox JavaDoc implements RowSetListener JavaDoc, KeyListener JavaDoc {
54     
55     private java.awt.Color JavaDoc std_color = null;
56     private String JavaDoc columnName = null;
57     private int colType = -99;
58     private SSRowSet rowset = null;
59     private SSDataNavigator navigator = null;
60     
61     /** Creates a new instance of SSBooleanField */
62     public SSBooleanField() {
63         super();
64         
65         Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS);
66         Set newForwardKeys = new HashSet JavaDoc(forwardKeys);
67         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0));
68         newForwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, java.awt.event.InputEvent.SHIFT_MASK ));
69         setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,newForwardKeys);
70         
71         Set backwardKeys = getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS);
72         Set newBackwardKeys = new HashSet JavaDoc(backwardKeys);
73         newBackwardKeys.add(KeyStroke.getKeyStroke(KeyEvent.VK_UP, java.awt.event.InputEvent.SHIFT_MASK ));
74         setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,newBackwardKeys);
75         
76         this.addKeyListener(this);
77         
78         this.setInputVerifier(new internalVerifier());
79     }
80     
81     public void setColumnName(String JavaDoc columnName) {
82         this.columnName = columnName;
83         bind();
84     }
85     
86     public void setRowSet(SSRowSet rowset) {
87         this.rowset = rowset;
88         bind();
89     }
90     
91     public void setNavigator(SSDataNavigator navigator) {
92         this.navigator = navigator;
93         setRowSet(navigator.getSSRowSet());
94     }
95     
96     public SSDataNavigator getNavigator() {
97         return this.navigator;
98     }
99     
100     private void DbToFm() {
101         
102         try {
103             
104             switch(colType) {
105                 
106                 case java.sql.Types.BIT://-7
107
this.setSelected(rowset.getBoolean(columnName));
108                     break;
109                     
110                 case java.sql.Types.BOOLEAN://16
111
this.setSelected(rowset.getBoolean(columnName));
112                     break;
113                     
114                 case java.sql.Types.INTEGER://4
115
case java.sql.Types.BIGINT://-5
116
case java.sql.Types.SMALLINT://5
117
case java.sql.Types.TINYINT://-6
118
if (rowset.getInt(columnName) == 1)
119                         this.setSelected(true);
120                     else
121                         this.setSelected(false);
122                     break;
123                     
124                 default:
125                     break;
126             }
127         } catch (java.sql.SQLException JavaDoc sqe) {
128             System.out.println("Error in DbToFm() = " + sqe);
129         }
130     }
131     
132     private void bind() {
133         
134         if (this.columnName == null) return;
135         if (this.rowset == null) return;
136         
137         try {
138             colType = rowset.getColumnType(columnName);
139         } catch(java.sql.SQLException JavaDoc sqe) {
140             System.out.println("bind error = " + sqe);
141         }
142         rowset.addRowSetListener(this);
143         DbToFm();
144     }
145     
146     public void rowSetChanged(javax.sql.RowSetEvent JavaDoc event) {
147         
148     }
149     
150     public void rowChanged(javax.sql.RowSetEvent JavaDoc event) {
151         
152     }
153     
154     public void cursorMoved(javax.sql.RowSetEvent JavaDoc event) {
155         DbToFm();
156     }
157     
158     
159     public void keyTyped(KeyEvent JavaDoc e) {
160     }
161     
162     public void keyReleased(KeyEvent JavaDoc e) {
163     }
164     
165     /**
166      * Catch severals keys, to implement some forms functionality (To be done).
167      *
168      */

169     public void keyPressed(KeyEvent JavaDoc e) {
170         
171         if (e.getKeyCode() == KeyEvent.VK_F1) {
172         }
173         
174         if (e.getKeyCode() == KeyEvent.VK_F2) {
175         }
176         
177         if (e.getKeyCode() == KeyEvent.VK_F3) {
178             
179         }
180         
181         if (e.getKeyCode() == KeyEvent.VK_F4) {
182             System.out.println("F4 ");
183         }
184         
185         if (e.getKeyCode() == KeyEvent.VK_F5) {
186             System.out.println("F5 = PROCESS");
187             navigator.doCommitButtonClick();
188         }
189         
190         if (e.getKeyCode() == KeyEvent.VK_F6) {
191             System.out.println("F6 = DELETE");
192             navigator.doDeleteButtonClick();
193         }
194         
195         if (e.getKeyCode() == KeyEvent.VK_F8) {
196             System.out.println("F8 ");
197             navigator.doUndoButtonClick();
198         }
199         
200         if (e.getKeyCode() == KeyEvent.VK_END) {
201             System.out.println("END ");
202         }
203         
204         if (e.getKeyCode() == KeyEvent.VK_DELETE) {
205             System.out.println("DELETE ");
206         }
207         
208         if (e.getKeyCode() == KeyEvent.VK_HOME) {
209             System.out.println("HOME ");
210         }
211         
212     }
213     
214     
215     /**
216      * This method should implements validation AND, most important for our purposes
217      * implements actual rowset fields updates.
218      *
219      */

220     
221     class internalVerifier extends InputVerifier JavaDoc {
222         
223         public boolean verify(JComponent JavaDoc input) {
224             
225             SSBooleanField tf = (SSBooleanField) input;
226             boolean selected = tf.isSelected();
227             
228             setBackground(java.awt.Color.WHITE);
229             
230             try {
231                 rowset.removeRowSetListener(tf);
232                 
233                 switch(colType) {
234                     
235                     case java.sql.Types.BIT://-7
236
rowset.updateBoolean(columnName, selected);
237                         break;
238                         
239                     case java.sql.Types.BOOLEAN://16
240
rowset.updateBoolean(columnName, selected);
241                         break;
242                         
243                     case java.sql.Types.INTEGER: //4
244
case java.sql.Types.BIGINT: //-5
245
case java.sql.Types.SMALLINT: //5
246
case java.sql.Types.TINYINT: //-6
247
if (selected == true) {
248                             rowset.updateInt(columnName, 1);
249                         } else {
250                             rowset.updateInt(columnName, 0);
251                         }
252                         break;
253                         
254                     default:
255                         break;
256                 }
257                 rowset.addRowSetListener(tf);
258             } catch (java.sql.SQLException JavaDoc se) {
259                 System.out.println("---> SQLException -----------> " + se);
260             } catch(java.lang.NullPointerException JavaDoc np) {
261                 System.out.println("---> NullPointerException ---> " + np);
262             }
263             return true;
264         }
265     }
266 }
267
268 /*
269  * $Log: SSBooleanField.java,v $
270  * Revision 1.8 2005/02/22 15:14:34 yoda2
271  * Fixed some JavaDoc & deprecation errors/warnings.
272  *
273  * Revision 1.7 2005/02/04 22:42:06 yoda2
274  * Updated Copyright info.
275  *
276  * Revision 1.6 2005/01/19 19:12:26 dags
277  * bind refactoring
278  *
279  * Revision 1.5 2005/01/14 00:06:42 dags
280  * Deep Refactoring
281  *
282  * Revision 1.4 2004/12/21 05:07:02 dags
283  * Remove SSFormattedTextField dependency. Simplified, I hope.
284  *
285  * Revision 1.3 2004/12/13 20:50:16 dags
286  * Fix package name
287  *
288  * Revision 1.2 2004/12/13 18:46:13 prasanth
289  * Added License.
290  *
291  */

292
Popular Tags