1 32 33 package com.nqadmin.swingSet; 34 35 import java.io.*; 36 import java.util.Vector ; 37 import java.util.Stack ; 38 import java.text.SimpleDateFormat ; 39 import java.sql.*; 40 import java.awt.*; 41 import java.awt.event.*; 42 import javax.swing.*; 43 import com.nqadmin.swingSet.datasources.SSRowSet; 44 import com.nqadmin.swingSet.datasources.SSConnection; 45 import javax.swing.border.*; 46 import javax.swing.text.*; 47 import javax.swing.event.*; 48 49 127 public class SSDBComboBox extends JComboBox { 129 130 133 protected JTextField textField = new JTextField(); 134 135 138 protected SSConnection sSConnection = null; 139 140 143 protected String query = ""; 144 145 150 protected String primaryKeyColumnName = ""; 151 152 156 protected String displayColumnName = ""; 157 158 162 protected String secondDisplayColumnName = ""; 163 164 168 protected Vector columnVector = new Vector (); 169 170 173 protected int numberOfItems = 0; 174 175 178 protected SSRowSet sSRowSet; 179 180 183 protected String columnName = ""; 184 185 188 private final MyComboListener cmbListener = new MyComboListener(); 189 190 193 private final MyTextFieldDocumentListener textFieldDocumentListener = new MyTextFieldDocumentListener(); 194 195 198 private final MyKeyListener myKeyListener = new MyKeyListener(); 199 200 203 protected String seperator = " - "; 204 205 208 protected String dateFormat = "MM/dd/yyyy"; 209 210 213 public SSDBComboBox() { 214 init(); 215 } 216 217 225 public SSDBComboBox(SSConnection _sSConnection, String _query, String _primaryKeyColumnName, String _displayColumnName) { 226 sSConnection = _sSConnection; 227 query = _query; 228 primaryKeyColumnName = _primaryKeyColumnName; 229 displayColumnName = _displayColumnName; 230 init(); 231 } 232 233 238 public void setSSRowSet(SSRowSet _sSRowSet) { 239 SSRowSet oldValue = sSRowSet; 240 sSRowSet = _sSRowSet; 241 firePropertyChange("sSRowSet", oldValue, sSRowSet); 242 bind(); 243 } 244 245 250 public SSRowSet getSSRowSet() { 251 return sSRowSet; 252 } 253 254 259 public void setSSConnection(SSConnection _sSConnection) { 260 SSConnection oldValue = sSConnection; 261 sSConnection = _sSConnection; 262 firePropertyChange("sSConnection", oldValue, sSConnection); 263 bind(); 264 } 265 266 271 public SSConnection getSSConnection() { 272 return sSConnection; 273 } 274 275 280 public void setQuery(String _query) { 281 String oldValue = query; 282 query = _query; 283 firePropertyChange("query", oldValue, query); 284 } 285 286 291 public String getQuery() { 292 return query; 293 } 294 295 300 public void setColumnName(String _columnName) { 301 String oldValue = columnName; 302 columnName = _columnName; 303 firePropertyChange("columnName", oldValue, columnName); 304 bind(); 305 } 306 307 312 public String getColumnName() { 313 return columnName; 314 } 315 316 321 public void setDisplayColumnName(String _displayColumnName) { 322 String oldValue = displayColumnName; 323 displayColumnName = _displayColumnName; 324 firePropertyChange("displayColumnName", oldValue, displayColumnName); 325 } 326 327 332 public String getDisplayColumnName() { 333 return displayColumnName; 334 } 335 336 342 public void setDateFormat(String _dateFormat) { 343 String oldValue = dateFormat; 344 dateFormat = _dateFormat; 345 firePropertyChange("dateFormat", oldValue, dateFormat); 346 } 347 348 353 public String getDateFormat() { 354 return dateFormat; 355 } 356 357 366 public void setSecondDisplayColumnName(String _secondDisplayColumnName) { 367 String oldValue = secondDisplayColumnName; 368 secondDisplayColumnName = _secondDisplayColumnName; 369 firePropertyChange("secondDisplayColumnName", oldValue, secondDisplayColumnName); 370 } 371 372 378 public String getSecondDisplayColumnName() { 379 return secondDisplayColumnName; 380 } 381 382 387 public void setSeperator(String _seperator) { 388 String oldValue = seperator; 389 seperator = _seperator; 390 firePropertyChange("seperator", oldValue, seperator); 391 } 392 393 398 public String getSeperator() { 399 return seperator; 400 } 401 402 409 public int getNumberOfItems() { 410 return numberOfItems; 411 } 412 413 420 public void setSelectedValue(long _value) { 421 textField.setText(String.valueOf(_value)); 422 } 423 424 432 public long getSelectedValue() { 433 434 int index = getSelectedIndex(); 435 436 if (index == -1) { 437 return -1; 438 } 439 440 return Long.valueOf((String )columnVector.get(index)).longValue(); 441 442 } 443 444 451 public void setSelectedStringValue(String _value) { 452 textField.setText(_value); 453 } 454 455 463 public String getSelectedStringValue() { 464 465 int index = getSelectedIndex(); 466 467 if (index == -1) { 468 return null; 469 } 470 471 return (String )columnVector.get(index); 472 473 } 474 475 479 public void execute() throws SQLException, Exception { 480 481 removeListeners(); 483 484 Statement statement = sSConnection.getConnection().createStatement(); 486 487 if (query.equals("")) { 488 throw new Exception ("Query is empty"); 489 } 490 491 ResultSet rs = statement.executeQuery(query); 492 493 removeAllItems(); 495 columnVector.clear(); 496 497 int i = 0; 499 while (rs.next()) { 500 if ( secondDisplayColumnName != null && !secondDisplayColumnName.trim().equals("")) { 502 addItem(getStringValue(rs,displayColumnName) + seperator + rs.getString(secondDisplayColumnName)); 503 } else { 504 addItem(getStringValue(rs,displayColumnName)); 505 } 506 columnVector.add(i,rs.getString(primaryKeyColumnName)); 508 i++; 509 } 510 511 numberOfItems = i; 513 514 updateDisplay(); 516 addListeners(); 517 518 } 520 526 public void bind(SSRowSet _sSRowSet, String _columnName) { 527 SSRowSet oldValue = sSRowSet; 528 sSRowSet = _sSRowSet; 529 firePropertyChange("sSRowSet", oldValue, sSRowSet); 530 531 String oldValue2 = columnName; 532 columnName = _columnName; 533 firePropertyChange("columnName", oldValue2, columnName); 534 535 bind(); 536 } 537 538 544 public void addItem(String _name, long _value) { 545 columnVector.add(Long.toString(_value)); 546 addItem(_name); 547 numberOfItems++; 548 } 549 550 556 public void addStringItem(String _name, String _value) { 557 columnVector.add(_value); 558 addItem(_name); 559 numberOfItems++; 560 } 561 562 570 public boolean deleteItem(long _value) { 571 int index = columnVector.indexOf(Long.toString(_value)); 572 if (index == -1) { 573 return false; 574 } 575 columnVector.removeElementAt(index); 576 removeItemAt(index); 577 numberOfItems--; 578 return true; 579 } 580 581 589 public boolean deleteStringItem(String _value) { 590 int index = columnVector.indexOf(_value); 591 if (index == -1) { 592 return false; 593 } 594 columnVector.removeElementAt(index); 595 removeItemAt(index); 596 numberOfItems--; 597 return true; 598 } 599 600 615 public boolean updateItem(long _value, String _name) { 616 int index = columnVector.indexOf(Long.toString(_value)); 617 if (index == -1) { 618 return false; 619 } 620 removeActionListener(cmbListener); 621 insertItemAt(_name,index+1); 622 removeItemAt(index); 623 setSelectedIndex(index); 624 addActionListener(cmbListener); 625 return true; 626 } 627 628 643 public boolean updateStringItem(String _value, String _name) { 644 int index = columnVector.indexOf(_value); 645 if (index == -1) { 646 return false; 647 } 648 removeActionListener(cmbListener); 649 insertItemAt(_name,index+1); 650 removeItemAt(index); 651 setSelectedIndex(index); 652 addActionListener(cmbListener); 653 return true; 654 } 655 656 659 protected void init() { 660 663 setPreferredSize(new Dimension(200,20)); 665 } 666 667 670 protected void bind() { 671 672 if (columnName==null || columnName.trim().equals("") || sSRowSet==null) { 674 return; 675 } 676 677 removeListeners(); 679 680 textField.setDocument(new SSTextDocument(sSRowSet, columnName)); 682 683 updateDisplay(); 685 686 addListeners(); 688 689 } 690 691 695 protected void updateDisplay() { 696 697 String text = textField.getText().trim(); 700 if (!text.equals("")) { 701 int columnVectorIndex = columnVector.indexOf(text); 705 if (columnVectorIndex != getSelectedIndex()) { 709 setSelectedIndex(columnVectorIndex); 711 } 712 } 713 else{ 714 setSelectedIndex(-1); 715 } 716 721 } 722 723 731 protected String getStringValue(ResultSet _rs, String _columnName) { 732 String strValue = ""; 733 try { 734 int type = _rs.getMetaData().getColumnType(_rs.findColumn(_columnName)); 735 switch(type){ 736 case Types.DATE: 737 SimpleDateFormat myDateFormat = new SimpleDateFormat (dateFormat); 738 strValue = myDateFormat.format(_rs.getDate(_columnName)); 739 break; 740 default: 741 strValue = _rs.getString(_columnName); 742 break; 743 } 744 } catch(SQLException se) { 745 se.printStackTrace(); 746 } 747 return strValue; 748 749 } 750 751 754 private void addListeners() { 755 addActionListener(cmbListener); 756 addFocusListener(cmbListener); 757 addKeyListener(myKeyListener); 758 textField.getDocument().addDocumentListener(textFieldDocumentListener); 759 } 760 761 764 private void removeListeners() { 765 removeActionListener(cmbListener); 766 removeFocusListener(cmbListener); 767 removeKeyListener(myKeyListener); 768 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 769 770 } 771 772 776 private class MyTextFieldDocumentListener implements DocumentListener { 777 778 public void changedUpdate(DocumentEvent de) { 779 removeActionListener(cmbListener); 780 updateDisplay(); 781 addActionListener(cmbListener); 782 } 783 784 public void insertUpdate(DocumentEvent de) { 785 removeActionListener(cmbListener); 786 updateDisplay(); 787 addActionListener(cmbListener); 788 } 789 790 public void removeUpdate(DocumentEvent de) { 791 removeActionListener(cmbListener); 792 updateDisplay(); 793 addActionListener(cmbListener); 794 } 795 796 } 798 801 private class MyKeyListener extends KeyAdapter { 802 803 String searchString = null; 804 Stack searchStack = new Stack (); 805 int previousIndex = 0; 806 807 public void resetSearchString() { 808 searchString = null; 809 } 810 811 public void keyReleased(KeyEvent ke) { 812 int i; 813 if (ke.getKeyCode() == KeyEvent.VK_ESCAPE || 814 ke.getKeyCode() == KeyEvent.VK_PAGE_UP || 815 ke.getKeyCode() == KeyEvent.VK_PAGE_DOWN || 816 ke.getKeyCode() == KeyEvent.VK_UP || 817 ke.getKeyCode() == KeyEvent.VK_DOWN || 818 ke.getKeyCode() == KeyEvent.VK_ENTER ) { 819 searchString = null; 820 searchStack.removeAllElements(); 821 if (ke.getKeyCode() == KeyEvent.VK_ENTER) { 822 ((Component)ke.getSource()).transferFocus(); 823 } 824 return; 825 826 } 827 828 if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE) { 829 if (searchString == null ) { 830 setSelectedIndex(0); 831 searchStack.removeAllElements(); 832 previousIndex = 0; 833 return; 834 } 835 if (searchString.length() > 0) { 836 searchString = searchString.substring(0,searchString.length()-1); 837 if (searchString.length() == 0) { 838 searchString = null; 839 searchStack.removeAllElements(); 840 previousIndex = 0; 841 } 842 } 843 844 } else if(searchString == null) { 845 searchString = new String (new char[]{ke.getKeyChar()}); 846 } else { 847 searchString = searchString + new String (new char[]{ke.getKeyChar()}); 848 } 849 850 if (searchString == null) { 851 setSelectedIndex(0); 852 return; 853 } 854 855 if (ke.getKeyCode() == KeyEvent.VK_BACK_SPACE) { 856 857 if (searchStack.empty()) { 858 setSelectedIndex(0); 859 return; 860 } else { 861 searchStack.pop(); 863 if(!searchStack.empty()) { 865 previousIndex = ((Integer )searchStack.peek()).intValue(); 866 setSelectedIndex(previousIndex); 867 return; 868 } else { 869 previousIndex = 0; 870 setSelectedIndex(0); 871 return; 872 } 873 } 874 875 } else { 876 877 for (i=previousIndex;i<getItemCount();i++) { 878 if (searchString.length() == 0) { 879 setSelectedIndex(0); 880 } else { 881 if (((String )getItemAt(i)).length() >= searchString.length()) { 882 if (searchString.equalsIgnoreCase( ((String )getItemAt(i)).substring(0,searchString.length())) ) { 883 setSelectedIndex(i); 884 searchStack.push(new Integer (i)); 885 return; 886 } 887 } 888 } 889 890 } 891 892 if (i == getItemCount()) { 893 for (i=0;i<previousIndex;i++) { 894 if (searchString.length() == 0) { 895 setSelectedIndex(0); 896 } else { 897 if (((String )getItemAt(i)).length() >= searchString.length()) { 898 if (searchString.equalsIgnoreCase( ((String )getItemAt(i)).substring(0,searchString.length())) ){ 899 setSelectedIndex(i); 900 searchStack.push(new Integer (i)); 901 return; 902 } 903 } 904 } 905 } 906 } 907 } 908 909 if (searchStack.empty()) { 910 setSelectedIndex(0); 911 } else { 912 setSelectedIndex(((Integer )searchStack.peek()).intValue()); 913 } 914 915 } 916 } 918 922 private class MyComboListener extends FocusAdapter implements ActionListener { 923 924 public void actionPerformed(ActionEvent ae) { 925 926 textField.getDocument().removeDocumentListener(textFieldDocumentListener); 927 928 int index = getSelectedIndex(); 930 931 if (index != -1) { 934 try { 935 String textFieldText = textField.getText(); 946 String columnVectorText = (String )columnVector.get(index); 947 948 if (!textFieldText.equals(columnVectorText)) { 949 textField.setText(columnVectorText); 950 } 951 952 953 960 } catch(NullPointerException npe) { 961 } catch(NumberFormatException nfe) { 962 } 963 } 964 else{ 965 textField.setText(""); 966 } 967 968 textField.getDocument().addDocumentListener(textFieldDocumentListener); 969 970 } 971 972 public void focusLost(FocusEvent fe){ 973 myKeyListener.resetSearchString(); 974 } 975 976 } 978 979 981 992 public SSDBComboBox(SSConnection _sSConnection, String _query, String _primaryKeyColumnName, String _displayColumnName, JTextField _textField) { 993 994 sSConnection = _sSConnection; 995 query = _query; 996 primaryKeyColumnName = _primaryKeyColumnName; 997 displayColumnName = _displayColumnName; 998 textField = _textField; 999 1000 } 1001 1002 1010 public void setConnection(SSConnection _sSConnection) { 1011 sSConnection = _sSConnection; 1012 } 1013 1014 1015 1023 public void setRowSet(SSRowSet _sSRowSet) { 1024 sSRowSet = _sSRowSet; 1025 bind(); 1026 } 1027 1028 1036 public void setTextField(JTextField _textField) { 1037 textField = _textField; 1038 } 1039 1040 1048 public SSConnection getConnection() { 1049 return sSConnection; 1050 } 1051 1052 1059 public JTextField getTextField() { 1060 return textField; 1061 } 1062 1063 1070 public JComboBox getComboBox() { 1071 return this; 1072 } 1073 1074 1081 public Component getComponent() { 1082 return this; 1083 } 1084 1085 1086 1096 public boolean deleteItem(String _name) { 1097 1098 for (int i=0; i<getItemCount();i++) { 1099 if ( ((String )getItemAt(i)).equals(_name) ) { 1100 removeItemAt(i); 1101 columnVector.removeElementAt(i); 1102 numberOfItems--; 1103 return true; 1104 } 1105 } 1106 return false; 1107 } 1108 1109 1110 1122 public boolean deleteItem(String _name, long _value) { 1123 for (int i=0; i<getItemCount();i++) { 1124 if ( ((String )getItemAt(i)).equals(_name) ) { 1125 if (((String )columnVector.elementAt(i)).equals(Long.toString(_value))) { 1126 removeItemAt(i); 1127 columnVector.removeElementAt(i); 1128 numberOfItems--; 1129 return true; 1130 } 1131 } 1132 } 1133 return false; 1134 } 1135 1136 1137} 1139 1140 1141 | Popular Tags |