1 32 33 package com.nqadmin.swingSet; 34 35 import java.io.*; 36 import java.sql.*; 37 import java.awt.*; 38 import java.awt.event.*; 39 import javax.swing.*; 40 import javax.swing.border.*; 41 import javax.swing.text.*; 42 import javax.swing.event.*; 43 import java.beans.*; 44 import javax.sql.*; 45 import com.nqadmin.swingSet.datasources.SSRowSet; 46 47 87 public class SSComboBox extends JComboBox { 89 90 92 93 96 public static final int NON_SELECTED = (int)((Math.pow(2, 32) -1)/(-2)); 97 98 101 protected JTextField textField = new JTextField(); 102 103 106 private final MyComboListener cmbListener = new MyComboListener(); 107 108 111 private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener(); 112 113 117 protected int[] mappings = null; 118 119 122 protected SSRowSet sSRowSet; 123 124 127 protected String columnName = ""; 128 129 132 protected String [] options; 133 134 138 protected int predefinedOptions = -1; 139 140 144 public static final int YES_NO_OPTION = 0; 145 146 149 public static final int YES = 1; 150 151 154 public static final int NO = 0; 155 156 160 public static final int GENDER_OPTION = 1; 161 162 165 public static final int MALE = 0; 166 167 170 public static final int FEMALE = 1; 171 172 175 public static final int UNISEX = 2; 176 177 181 public static final int INCLUDE_EXCLUDE_OPTION = 2; 182 183 186 public static final int EXCLUDE = 0; 187 188 191 public static final int INCLUDE = 1; 192 193 196 public SSComboBox() { 197 init(); 198 } 199 200 207 public void setSelectedValue(int _value) { 208 textField.setText(String.valueOf(_value)); 209 } 210 211 219 public int getSelectedValue() { 220 if (getSelectedIndex() == -1) { 222 return NON_SELECTED; 223 } 224 225 if (mappings != null) { 226 return mappings[getSelectedIndex()]; 228 } 229 return getSelectedIndex(); 231 232 } 233 234 240 public void setColumnName(String _columnName) { 241 String oldValue = columnName; 242 columnName = _columnName; 243 firePropertyChange("columnName", oldValue, columnName); 244 bind(); 245 } 246 247 252 public String getColumnName() { 253 return columnName; 254 } 255 256 261 public void setSSRowSet(SSRowSet _sSRowSet) { 262 SSRowSet oldValue = sSRowSet; 263 sSRowSet = _sSRowSet; 264 firePropertyChange("sSRowSet", oldValue, sSRowSet); 265 bind(); 266 } 267 268 273 public SSRowSet getSSRowSet() { 274 return sSRowSet; 275 } 276 277 283 public void setMappings(int[] _mappings) { 284 int[] oldValue = (int[])_mappings.clone(); 285 mappings = (int[])_mappings.clone(); 286 firePropertyChange("mappings", oldValue, mappings); 287 } 293 294 300 public int[] getMappings() { 301 return mappings; 302 } 303 304 309 public void setOptions(String [] _options) { 310 String [] oldValue = (String [])_options.clone(); 311 options = (String [])_options.clone(); 312 firePropertyChange("options", oldValue, options); 313 314 if (getItemCount() != 0) { 318 removeAllItems(); 319 } 320 for (int i=0;i<_options.length;i++) { 321 addItem(_options[i]); 322 } 323 } 324 325 330 public String [] getOptions() { 331 return options; 332 } 335 336 346 public boolean setOptions(String [] _options, int[]_mappings) { 347 if (_options.length != _mappings.length) { 348 return false; 349 } 350 351 setOptions(_options); 352 353 setMappings(_mappings); 354 355 return true; 356 357 360 376 } 378 379 385 public boolean setPredefinedOptions(int _predefinedOptions) { 386 int oldValue = predefinedOptions; 387 388 if (_predefinedOptions == YES_NO_OPTION) { 389 setOptions(new String []{"No", "Yes"}); 390 } else if (_predefinedOptions == SEX_OPTION || _predefinedOptions == GENDER_OPTION) { 391 setOptions(new String []{"Male", "Female", "Unisex"}); 392 } else if (_predefinedOptions == INCLUDE_EXCLUDE_OPTION) { 393 setOptions(new String []{"Include", "Exclude"}); 394 } else { 395 return false; 396 } 397 398 predefinedOptions = _predefinedOptions; 399 firePropertyChange("predefinedOptions", oldValue, predefinedOptions); 400 401 return true; 402 403 404 430 } 431 432 438 public int getPredefinedOptions() { 439 return predefinedOptions; 440 } 441 442 448 public void bind(SSRowSet _sSRowSet, String _columnName) { 449 SSRowSet oldValue = sSRowSet; 450 sSRowSet = _sSRowSet; 451 firePropertyChange("sSRowSet", oldValue, sSRowSet); 452 453 String oldValue2 = columnName; 454 columnName = _columnName; 455 firePropertyChange("columnName", oldValue2, columnName); 456 457 bind(); 458 } 459 460 463 protected void init() { 464 addKeyListener(new KeyAdapter() { 467 public void keyReleased(KeyEvent ke) { 468 if (ke.getKeyCode() == KeyEvent.VK_ENTER) { 469 ((Component)ke.getSource()).transferFocus(); 470 } 471 } 472 }); 473 474 setPreferredSize(new Dimension(200,20)); 476 } 477 478 481 protected void bind() { 482 483 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 485 return; 486 } 487 488 removeListeners(); 490 491 textField.setDocument(new SSTextDocument(sSRowSet, columnName)); 493 494 updateDisplay(); 496 497 addListeners(); 499 500 } 501 502 506 protected void updateDisplay() { 507 try { 508 String text = textField.getText().trim(); 509 int intValue = 0; 511 if ( !(text.trim().equals("")) ) { 512 intValue = Integer.parseInt(text); 513 } 514 if ( (mappings==null && intValue != getSelectedIndex()) || 522 (mappings!=null && getSelectedIndex() == -1) || 523 (mappings!=null && mappings[getSelectedIndex()] != intValue) ) { 524 525 if (mappings==null && (intValue <0 || intValue >= getItemCount() )) { 526 System.out.println("Error: value from DB:" + intValue + " items in combo box: " + getItemCount()); 530 setSelectedIndex(-1); 531 } else { 532 if (mappings!=null) { 535 int i=0; 536 for (;i<mappings.length;i++) { 537 if (mappings[i] == intValue) { 538 setSelectedIndex(i); 539 break; 540 } 541 } 542 if (i==mappings.length) { 544 System.out.println("change ERROR: could not find a corresponding item in combo for value " + intValue); 545 setSelectedIndex(-1); 546 } 547 } else { 548 setSelectedIndex(intValue); 551 } 552 } 553 } 554 555 } catch(NumberFormatException nfe) { 556 nfe.printStackTrace(); 557 } 558 559 } 560 561 564 private void addListeners() { 565 textField.getDocument().addDocumentListener(textFieldDocumentListener); 566 addActionListener(cmbListener); 567 } 568 569 572 private void removeListeners() { 573 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 574 removeActionListener(cmbListener); 575 } 576 577 581 private class MyTextFieldDocumentListener implements DocumentListener, Serializable { 582 583 public void changedUpdate(DocumentEvent de) { 584 removeActionListener(cmbListener); 585 updateDisplay(); 586 addActionListener(cmbListener); 587 } 589 public void insertUpdate(DocumentEvent de) { 590 removeActionListener(cmbListener); 591 updateDisplay(); 592 addActionListener(cmbListener); 593 } 595 public void removeUpdate(DocumentEvent de) { 596 597 598 599 } 600 } 601 602 606 private class MyComboListener implements ActionListener, Serializable { 607 608 public void actionPerformed(ActionEvent ae) { 609 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 610 int index = getSelectedIndex(); 611 try { 612 if (index == -1) { 613 textField.setText(""); 614 } else { 615 String strValueInText = textField.getText(); 616 int valueOfText = -1; 617 strValueInText = strValueInText.trim(); 618 if ( !strValueInText.equals("") ) { 619 valueOfText = Integer.parseInt(strValueInText); 620 } 621 622 if ( mappings == null && valueOfText != index ) { 623 textField.setText( String.valueOf(index) ); 624 } 625 else if(mappings != null && mappings.length > index && valueOfText != mappings[index]){ 626 textField.setText(String.valueOf(mappings[index])); 627 } 628 } 629 } catch(NullPointerException npe) { 630 npe.printStackTrace(); 631 } catch(NumberFormatException nfe) { 632 nfe.printStackTrace(); 633 } 634 textField.getDocument().addDocumentListener(textFieldDocumentListener); 635 } 636 } 637 638 639 640 648 public static final int SEX_OPTION = 1; 649 650 656 public static final int UNI_SEX = 2; 657 658 664 public SSComboBox(SSTextDocument document) { 665 666 init(); 668 670 this.setDocument(document); 671 } 672 673 682 public void setDocument(SSTextDocument _document) { 683 textField.setDocument(_document); 684 updateDisplay(); 685 removeListeners(); 689 addListeners(); 690 } 691 692 699 public JComboBox getComboBox() { 700 return this; 701 } 702 703 710 public Component getComponent() { 711 return this; 712 } 713 714 726 public boolean setOption(String [] _options) { 727 setOptions(_options); 728 return true; 729 } 730 731 744 public boolean setOption(String [] _options, int[]_mappings) { 745 return setOptions(_options, _mappings); 746 } 747 748 758 public void setOption(int _options, int[]_mappings) throws PropertyVetoException { 759 setPredefinedOptions(_options); 760 setMappings(_mappings); 761 } 762 763 772 public boolean setOption(int _option) { 773 return setPredefinedOptions(_option); 774 } 775 776 785 public void setMappingValues(int[] _mappings) { 786 setMappings(_mappings); 787 } 788 789 } 791 792 793 | Popular Tags |