| 1 7 package javax.swing; 8 9 import java.beans.*; 10 import java.util.*; 11 12 import java.awt.*; 13 import java.awt.event.*; 14 15 import java.io.Serializable ; 16 import java.io.ObjectOutputStream ; 17 import java.io.ObjectInputStream ; 18 import java.io.IOException ; 19 20 import javax.swing.event.*; 21 import javax.swing.plaf.*; 22 import javax.swing.border.*; 23 24 import javax.accessibility.*; 25 26 62 public class JComboBox extends JComponent 63 implements ItemSelectable,ListDataListener,ActionListener, Accessible { 64 68 private static final String uiClassID = "ComboBoxUI"; 69 70 77 protected ComboBoxModel dataModel; 78 85 protected ListCellRenderer renderer; 86 93 protected ComboBoxEditor editor; 94 101 protected int maximumRowCount = 8; 102 103 110 protected boolean isEditable = false; 111 118 protected KeySelectionManager keySelectionManager = null; 119 126 protected String actionCommand = "comboBoxChanged"; 127 134 protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled(); 135 136 140 protected Object selectedItemReminder = null; 141 142 private Object prototypeDisplayValue; 143 144 private boolean firingActionEvent = false; 146 147 private boolean selectingItem = false; 149 150 161 public JComboBox(ComboBoxModel aModel) { 162 super(); 163 setModel(aModel); 164 init(); 165 } 166 167 175 public JComboBox(final Object items[]) { 176 super(); 177 setModel(new DefaultComboBoxModel (items)); 178 init(); 179 } 180 181 189 public JComboBox(Vector<?> items) { 190 super(); 191 setModel(new DefaultComboBoxModel (items)); 192 init(); 193 } 194 195 203 public JComboBox() { 204 super(); 205 setModel(new DefaultComboBoxModel ()); 206 init(); 207 } 208 209 private void init() { 210 installAncestorListener(); 211 setOpaque(true); 212 updateUI(); 213 } 214 215 protected void installAncestorListener() { 216 addAncestorListener(new AncestorListener(){ 217 public void ancestorAdded(AncestorEvent event){ hidePopup();} 218 public void ancestorRemoved(AncestorEvent event){ hidePopup();} 219 public void ancestorMoved(AncestorEvent event){ 220 if (event.getSource() != JComboBox.this) 221 hidePopup(); 222 }}); 223 } 224 225 237 public void setUI(ComboBoxUI ui) { 238 super.setUI(ui); 239 } 240 241 246 public void updateUI() { 247 setUI((ComboBoxUI)UIManager.getUI(this)); 248 } 249 250 251 258 public String getUIClassID() { 259 return uiClassID; 260 } 261 262 263 268 public ComboBoxUI getUI() { 269 return(ComboBoxUI)ui; 270 } 271 272 283 public void setModel(ComboBoxModel aModel) { 284 ComboBoxModel oldModel = dataModel; 285 if (oldModel != null) { 286 oldModel.removeListDataListener(this); 287 } 288 dataModel = aModel; 289 dataModel.addListDataListener(this); 290 291 selectedItemReminder = dataModel.getSelectedItem(); 293 294 firePropertyChange( "model", oldModel, dataModel); 295 } 296 297 303 public ComboBoxModel getModel() { 304 return dataModel; 305 } 306 307 310 311 341 public void setLightWeightPopupEnabled(boolean aFlag) { 342 boolean oldFlag = lightWeightPopupEnabled; 343 lightWeightPopupEnabled = aFlag; 344 firePropertyChange("lightWeightPopupEnabled", oldFlag, lightWeightPopupEnabled); 345 } 346 347 355 public boolean isLightWeightPopupEnabled() { 356 return lightWeightPopupEnabled; 357 } 358 359 376 public void setEditable(boolean aFlag) { 377 boolean oldFlag = isEditable; 378 isEditable = aFlag; 379 firePropertyChange( "editable", oldFlag, isEditable ); 380 } 381 382 388 public boolean isEditable() { 389 return isEditable; 390 } 391 392 404 public void setMaximumRowCount(int count) { 405 int oldCount = maximumRowCount; 406 maximumRowCount = count; 407 firePropertyChange( "maximumRowCount", oldCount, maximumRowCount ); 408 } 409 410 417 public int getMaximumRowCount() { 418 return maximumRowCount; 419 } 420 421 442 public void setRenderer(ListCellRenderer aRenderer) { 443 ListCellRenderer oldRenderer = renderer; 444 renderer = aRenderer; 445 firePropertyChange( "renderer", oldRenderer, renderer ); 446 invalidate(); 447 } 448 449 456 public ListCellRenderer getRenderer() { 457 return renderer; 458 } 459 460 474 public void setEditor(ComboBoxEditor anEditor) { 475 ComboBoxEditor oldEditor = editor; 476 477 if ( editor != null ) { 478 editor.removeActionListener(this); 479 } 480 editor = anEditor; 481 if ( editor != null ) { 482 editor.addActionListener(this); 483 } 484 firePropertyChange( "editor", oldEditor, editor ); 485 } 486 487 493 public ComboBoxEditor getEditor() { 494 return editor; 495 } 496 497 501 529 public void setSelectedItem(Object anObject) { 530 Object oldSelection = selectedItemReminder; 531 if (oldSelection == null || !oldSelection.equals(anObject)) { 532 533 if (anObject != null && !isEditable()) { 534 boolean found = false; 537 for (int i = 0; i < dataModel.getSize(); i++) { 538 if (anObject.equals(dataModel.getElementAt(i))) { 539 found = true; 540 break; 541 } 542 } 543 if (!found) { 544 return; 545 } 546 } 547 548 selectingItem = true; 551 dataModel.setSelectedItem(anObject); 552 selectingItem = false; 553 554 if (selectedItemReminder != dataModel.getSelectedItem()) { 555 selectedItemChanged(); 559 } 560 } 561 fireActionEvent(); 562 } 563 564 574 public Object getSelectedItem() { 575 return dataModel.getSelectedItem(); 576 } 577 578 589 public void setSelectedIndex(int anIndex) { 590 int size = dataModel.getSize(); 591 592 if ( anIndex == -1 ) { 593 setSelectedItem( null ); 594 } else if ( anIndex < -1 || anIndex >= size ) { 595 throw new IllegalArgumentException ("setSelectedIndex: " + anIndex + " out of bounds"); 596 } else { 597 setSelectedItem(dataModel.getElementAt(anIndex)); 598 } 599 } 600 601 614 public int getSelectedIndex() { 615 Object sObject = dataModel.getSelectedItem(); 616 int i,c; 617 Object obj; 618 619 for ( i=0,c=dataModel.getSize();i<c;i++ ) { 620 obj = dataModel.getElementAt(i); 621 if ( obj != null && obj.equals(sObject) ) 622 return i; 623 } 624 return -1; 625 } 626 627 635 public Object getPrototypeDisplayValue() { 636 return prototypeDisplayValue; 637 } 638 639 660 public void setPrototypeDisplayValue(Object prototypeDisplayValue) { 661 Object oldValue = this.prototypeDisplayValue; 662 this.prototypeDisplayValue = prototypeDisplayValue; 663 firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue); 664 } 665 666 688 public void addItem(Object anObject) { 689 checkMutableComboBoxModel(); 690 ((MutableComboBoxModel )dataModel).addElement(anObject); 691 } 692 693 703 public void insertItemAt(Object anObject, int index) { 704 checkMutableComboBoxModel(); 705 ((MutableComboBoxModel )dataModel).insertElementAt(anObject,index); 706 } 707 708 716 public void removeItem(Object anObject) { 717 checkMutableComboBoxModel(); 718 ((MutableComboBoxModel )dataModel).removeElement(anObject); 719 } 720 721 731 public void removeItemAt(int anIndex) { 732 checkMutableComboBoxModel(); 733 ((MutableComboBoxModel )dataModel).removeElementAt( anIndex ); 734 } 735 736 739 public void removeAllItems() { 740 checkMutableComboBoxModel(); 741 MutableComboBoxModel model = (MutableComboBoxModel )dataModel; 742 int size = model.getSize(); 743 744 if ( model instanceof DefaultComboBoxModel ) { 745 ((DefaultComboBoxModel )model).removeAllElements(); 746 } 747 else { 748 for ( int i = 0; i < size; ++i ) { 749 Object element = model.getElementAt( 0 ); 750 model.removeElement( element ); 751 } 752 } 753 selectedItemReminder = null; 754 if (isEditable()) { 755 editor.setItem(null); 756 } 757 } 758 759 765 void checkMutableComboBoxModel() { 766 if ( !(dataModel instanceof MutableComboBoxModel ) ) 767 throw new RuntimeException ("Cannot use this method with a non-Mutable data model."); 768 } 769 770 774 public void showPopup() { 775 setPopupVisible(true); 776 } 777 778 782 public void hidePopup() { 783 setPopupVisible(false); 784 } 785 786 789 public void setPopupVisible(boolean v) { 790 getUI().setPopupVisible(this, v); 791 } 792 793 798 public boolean isPopupVisible() { 799 return getUI().isPopupVisible(this); 800 } 801 802 803 804 813 public void addItemListener(ItemListener aListener) { 814 listenerList.add(ItemListener.class,aListener); 815 } 816 817 821 public void removeItemListener(ItemListener aListener) { 822 listenerList.remove(ItemListener.class,aListener); 823 } 824 825 833 public ItemListener[] getItemListeners() { 834 return (ItemListener[])listenerList.getListeners(ItemListener.class); 835 } 836 837 847 public void addActionListener(ActionListener l) { 848 listenerList.add(ActionListener.class,l); 849 } 850 851 855 public void removeActionListener(ActionListener l) { 856 if ((l != null) && (getAction() == l)) { 857 setAction(null); 858 } else { 859 listenerList.remove(ActionListener.class, l); 860 } 861 } 862 863 871 public ActionListener[] getActionListeners() { 872 return (ActionListener[])listenerList.getListeners( 873 ActionListener.class); 874 } 875 876 888 public void addPopupMenuListener(PopupMenuListener l) { 889 listenerList.add(PopupMenuListener.class,l); 890 } 891 892 899 public void removePopupMenuListener(PopupMenuListener l) { 900 listenerList.remove(PopupMenuListener.class,l); 901 } 902 903 911 public PopupMenuListener[] getPopupMenuListeners() { 912 return (PopupMenuListener[])listenerList.getListeners( 913 PopupMenuListener.class); 914 } 915 916 925 public void firePopupMenuWillBecomeVisible() { 926 Object [] listeners = listenerList.getListenerList(); 927 PopupMenuEvent e=null; 928 for (int i = listeners.length-2; i>=0; i-=2) { 929 if (listeners[i]==PopupMenuListener.class) { 930 if (e == null) 931 e = new PopupMenuEvent(this); 932 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e); 933 } 934 } 935 } 936 937 946 public void firePopupMenuWillBecomeInvisible() { 947 Object [] listeners = listenerList.getListenerList(); 948 PopupMenuEvent e=null; 949 for (int i = listeners.length-2; i>=0; i-=2) { 950 if (listeners[i]==PopupMenuListener.class) { 951 if (e == null) 952 e = new PopupMenuEvent(this); 953 ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e); 954 } 955 } 956 } 957 958 967 public void firePopupMenuCanceled() { 968 Object [] listeners = listenerList.getListenerList(); 969 PopupMenuEvent e=null; 970 for (int i = listeners.length-2; i>=0; i-=2) { 971 if (listeners[i]==PopupMenuListener.class) { 972 if (e == null) 973 e = new PopupMenuEvent(this); 974 ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e); 975 } 976 } 977 } 978 979 988 public void setActionCommand(String aCommand) { 989 actionCommand = aCommand; 990 } 991 992 999 public String getActionCommand() { 1000 return actionCommand; 1001 } 1002 1003 private Action action; 1004 private PropertyChangeListener actionPropertyChangeListener; 1005 1006 |