1 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 65 public class SSDBCheckBox extends JCheckBox { 66 67 private JTextField textField = new JTextField(); 69 70 private String columnName; 71 private int columnType = java.sql.Types.BIT; 72 73 private MyCheckBoxListener checkBoxListener = new MyCheckBoxListener(); 75 private MyTextFieldListener textFieldListener = new MyTextFieldListener(); 76 77 private int CHECKED = 1; 79 private int UNCHECKED = 0; 80 81 private static String BOOLEAN_CHECKED = "true"; 83 private static String BOOLEAN_UNCHECKED = "false"; 84 85 93 public SSDBCheckBox(JTextField _textField) { 94 textField = _textField; 95 } 96 97 100 public SSDBCheckBox() { 101 textField = new JTextField(); 102 } 103 104 111 public SSDBCheckBox(SSRowSet _rowset, String _columnName) throws java.sql.SQLException { 112 columnName = _columnName; 113 textField.setDocument(new SSTextDocument(_rowset, _columnName)); 114 columnType = _rowset.getColumnType(_columnName); 115 } 116 117 125 public void setTextField(JTextField _textField) { 126 if (textField != null) { 130 textField.getDocument().removeDocumentListener(textFieldListener); 131 } 132 133 textField = _textField; 134 } 135 136 143 public void bind(SSRowSet _rowset, String _columnName) throws java.sql.SQLException { 144 columnName = _columnName; 145 textField.setDocument(new SSTextDocument(_rowset, _columnName)); 146 columnType = _rowset.getColumnType(_columnName); 147 } 148 149 154 public String getColumnName() { 155 return columnName; 156 } 157 158 165 public JTextField getTextField() { 166 return textField; 167 } 168 169 173 public void execute() { 174 initCheckBox(); 175 } 176 177 178 private void initCheckBox() { 180 181 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 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 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 removeChangeListener(checkBoxListener); 214 addChangeListener( checkBoxListener ); 215 } 217 private class MyTextFieldListener implements DocumentListener, Serializable { 219 private void readObject(ObjectInputStream objIn) throws IOException, ClassNotFoundException { 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 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 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 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 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 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 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 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 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 } 325 private class MyCheckBoxListener implements ChangeListener, Serializable { 329 330 private void readObject(ObjectInputStream objIn) throws IOException, ClassNotFoundException { 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 } 372 } 374 375 376 419 | Popular Tags |