1 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 ; 39 import java.awt.event.KeyEvent ; 40 import java.awt.event.KeyListener ; 41 import java.util.HashSet ; 42 import java.util.Set ; 43 import javax.sql.RowSetListener ; 44 import javax.swing.InputVerifier ; 45 import javax.swing.JCheckBox ; 46 import javax.swing.JComponent ; 47 import javax.swing.KeyStroke ; 48 49 53 public class SSBooleanField extends JCheckBox implements RowSetListener , KeyListener { 54 55 private java.awt.Color std_color = null; 56 private String columnName = null; 57 private int colType = -99; 58 private SSRowSet rowset = null; 59 private SSDataNavigator navigator = null; 60 61 62 public SSBooleanField() { 63 super(); 64 65 Set forwardKeys = getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); 66 Set newForwardKeys = new HashSet (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 (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 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: this.setSelected(rowset.getBoolean(columnName)); 108 break; 109 110 case java.sql.Types.BOOLEAN: this.setSelected(rowset.getBoolean(columnName)); 112 break; 113 114 case java.sql.Types.INTEGER: case java.sql.Types.BIGINT: case java.sql.Types.SMALLINT: case java.sql.Types.TINYINT: 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 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 sqe) { 140 System.out.println("bind error = " + sqe); 141 } 142 rowset.addRowSetListener(this); 143 DbToFm(); 144 } 145 146 public void rowSetChanged(javax.sql.RowSetEvent event) { 147 148 } 149 150 public void rowChanged(javax.sql.RowSetEvent event) { 151 152 } 153 154 public void cursorMoved(javax.sql.RowSetEvent event) { 155 DbToFm(); 156 } 157 158 159 public void keyTyped(KeyEvent e) { 160 } 161 162 public void keyReleased(KeyEvent e) { 163 } 164 165 169 public void keyPressed(KeyEvent 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 220 221 class internalVerifier extends InputVerifier { 222 223 public boolean verify(JComponent 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: rowset.updateBoolean(columnName, selected); 237 break; 238 239 case java.sql.Types.BOOLEAN: rowset.updateBoolean(columnName, selected); 241 break; 242 243 case java.sql.Types.INTEGER: case java.sql.Types.BIGINT: case java.sql.Types.SMALLINT: case java.sql.Types.TINYINT: 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 se) { 259 System.out.println("---> SQLException -----------> " + se); 260 } catch(java.lang.NullPointerException np) { 261 System.out.println("---> NullPointerException ---> " + np); 262 } 263 return true; 264 } 265 } 266 } 267 268 292 | Popular Tags |