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 63 public class SSCheckBox extends JCheckBox { 64 65 68 protected JTextField textField = new JTextField(); 69 70 73 protected SSRowSet sSRowSet; 74 75 78 protected String columnName = ""; 79 80 83 protected int columnType = java.sql.Types.BIT; 84 85 88 private final MyCheckBoxListener checkBoxListener = new MyCheckBoxListener(); 89 90 93 private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener(); 94 95 98 protected int CHECKED = 1; 99 100 103 protected int UNCHECKED = 0; 104 105 108 protected static String BOOLEAN_CHECKED = "true"; 109 110 113 protected static String BOOLEAN_UNCHECKED = "false"; 114 115 118 public SSCheckBox() { 119 init(); 120 } 121 122 129 public SSCheckBox(SSRowSet _sSRowSet, String _columnName) throws java.sql.SQLException { 130 sSRowSet = _sSRowSet; 131 columnName = _columnName; 132 init(); 133 bind(); 134 } 135 136 142 public void setColumnName(String _columnName) throws java.sql.SQLException { 143 String oldValue = columnName; 144 columnName = _columnName; 145 firePropertyChange("columnName", oldValue, columnName); 146 bind(); 147 } 148 149 154 public String getColumnName() { 155 return columnName; 156 } 157 158 163 public void setSSRowSet(SSRowSet _sSRowSet) throws java.sql.SQLException { 164 SSRowSet oldValue = sSRowSet; 165 sSRowSet = _sSRowSet; 166 firePropertyChange("sSRowSet", oldValue, sSRowSet); 167 bind(); 168 } 169 170 175 public SSRowSet getSSRowSet() { 176 return sSRowSet; 177 } 178 179 185 public void bind(SSRowSet _sSRowSet, String _columnName) throws java.sql.SQLException { 186 SSRowSet oldValue = sSRowSet; 187 sSRowSet = _sSRowSet; 188 firePropertyChange("sSRowSet", oldValue, sSRowSet); 190 191 String oldValue2 = columnName; 192 columnName = _columnName; 193 firePropertyChange("columnName", oldValue2, columnName); 195 196 bind(); 197 } 198 199 202 protected void init() { 203 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 setPreferredSize(new Dimension(20,20)); 216 setMaximumSize(new Dimension(20,20)); 217 } 218 219 222 protected void bind() throws java.sql.SQLException { 223 224 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 226 return; 227 } 228 229 removeListeners(); 231 232 columnType = sSRowSet.getColumnType(columnName); 234 235 textField.setDocument(new SSTextDocument(sSRowSet, columnName)); 237 238 updateDisplay(); 240 241 addListeners(); 243 244 } 245 246 249 private void addListeners() { 250 textField.getDocument().addDocumentListener(textFieldDocumentListener); 251 addChangeListener(checkBoxListener); 253 } 254 255 258 private void removeListeners() { 259 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 260 removeChangeListener(checkBoxListener); 262 } 263 264 265 269 protected void updateDisplay() { 270 271 switch(columnType) { 273 case java.sql.Types.INTEGER: 274 case java.sql.Types.SMALLINT: 275 case java.sql.Types.TINYINT: 276 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 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 } 300 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 public void insertUpdate(DocumentEvent de) { 317 removeChangeListener(checkBoxListener); 318 319 updateDisplay(); 320 321 addChangeListener( checkBoxListener ); 322 } 323 324 public void removeUpdate(DocumentEvent de) { 327 removeChangeListener(checkBoxListener); 328 329 updateDisplay(); 330 331 addChangeListener( checkBoxListener ); 332 } 333 } 335 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 } 375 376 377 379 387 public SSCheckBox(JTextField _textField) { 388 textField = _textField; 389 } 390 391 397 public void execute() { 398 } 400 401 402 } 404 405 406 454 | Popular Tags |