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 1036 public void setAction(Action a) { 1037 Action oldValue = getAction(); 1038 if (action==null || !action.equals(a)) { 1039 action = a; 1040 if (oldValue!=null) { 1041 removeActionListener(oldValue); 1042 oldValue.removePropertyChangeListener(actionPropertyChangeListener); 1043 actionPropertyChangeListener = null; 1044 } 1045 configurePropertiesFromAction(action); 1046 if (action!=null) { 1047 if (!isListener(ActionListener.class, action)) { 1049 addActionListener(action); 1050 } 1051 actionPropertyChangeListener = createActionPropertyChangeListener(action); 1053 action.addPropertyChangeListener(actionPropertyChangeListener); 1054 } 1055 firePropertyChange("action", oldValue, action); 1056 revalidate(); 1057 repaint(); 1058 } 1059 } 1060 1061 private boolean isListener(Class c, ActionListener a) { 1062 boolean isListener = false; 1063 Object [] listeners = listenerList.getListenerList(); 1064 for (int i = listeners.length-2; i>=0; i-=2) { 1065 if (listeners[i]==c && listeners[i+1]==a) { 1066 isListener=true; 1067 } 1068 } 1069 return isListener; 1070 } 1071 1072 1083 public Action getAction() { 1084 return action; 1085 } 1086 1087 1100 protected void configurePropertiesFromAction(Action a) { 1101 setEnabled((a!=null?a.isEnabled():true)); 1102 setToolTipText((a!=null?(String )a.getValue(Action.SHORT_DESCRIPTION):null)); 1103 } 1104 1105 1123 protected PropertyChangeListener createActionPropertyChangeListener(Action a) { 1124 return new AbstractActionPropertyChangeListener (this, a) { 1125 public void propertyChange(PropertyChangeEvent e) { 1126 String propertyName = e.getPropertyName(); 1127 JComboBox comboBox = (JComboBox )getTarget(); 1128 if (comboBox == null) { Action action = (Action )e.getSource(); 1130 action.removePropertyChangeListener(this); 1131 } else { 1132 if (e.getPropertyName().equals(Action.SHORT_DESCRIPTION)) { 1133 String text = (String ) e.getNewValue(); 1134 comboBox.setToolTipText(text); 1135 } else if (propertyName.equals("enabled")) { 1136 Boolean enabledState = (Boolean ) e.getNewValue(); 1137 comboBox.setEnabled(enabledState.booleanValue()); 1138 comboBox.repaint(); 1139 } 1140 } 1141 } 1142 }; 1143 } 1144 1145 1152 protected void fireItemStateChanged(ItemEvent e) { 1153 Object [] listeners = listenerList.getListenerList(); 1155 for ( int i = listeners.length-2; i>=0; i-=2 ) { 1158 if ( listeners[i]==ItemListener.class ) { 1159 ((ItemListener)listeners[i+1]).itemStateChanged(e); 1163 } 1164 } 1165 } 1166 1167 1173 protected void fireActionEvent() { 1174 if (!firingActionEvent) { 1175 firingActionEvent = true; 1177 ActionEvent e = null; 1178 Object [] listeners = listenerList.getListenerList(); 1180 long mostRecentEventTime = EventQueue.getMostRecentEventTime(); 1181 int modifiers = 0; 1182 AWTEvent currentEvent = EventQueue.getCurrentEvent(); 1183 if (currentEvent instanceof InputEvent) { 1184 modifiers = ((InputEvent)currentEvent).getModifiers(); 1185 } else if (currentEvent instanceof ActionEvent) { 1186 modifiers = ((ActionEvent)currentEvent).getModifiers(); 1187 } 1188 for ( int i = listeners.length-2; i>=0; i-=2 ) { 1191 if ( listeners[i]==ActionListener.class ) { 1192 if ( e == null ) 1194 e = new ActionEvent(this,ActionEvent.ACTION_PERFORMED, 1195 getActionCommand(), 1196 mostRecentEventTime, modifiers); 1197 ((ActionListener)listeners[i+1]).actionPerformed(e); 1198 } 1199 } 1200 firingActionEvent = false; 1201 } 1202 } 1203 1204 1208 protected void selectedItemChanged() { 1209 if (selectedItemReminder != null ) { 1210 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED, 1211 selectedItemReminder, 1212 ItemEvent.DESELECTED)); 1213 } 1214 1215 selectedItemReminder = dataModel.getSelectedItem(); 1217 1218 if (selectedItemReminder != null ) { 1219 fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED, 1220 selectedItemReminder, 1221 ItemEvent.SELECTED)); 1222 } 1223 } 1224 1225 1233 public Object [] getSelectedObjects() { 1234 Object selectedObject = getSelectedItem(); 1235 if ( selectedObject == null ) 1236 return new Object [0]; 1237 else { 1238 Object result[] = new Object [1]; 1239 result[0] = selectedObject; 1240 return result; 1241 } 1242 } 1243 1244 1248 public void actionPerformed(ActionEvent e) { 1249 Object newItem = getEditor().getItem(); 1250 setPopupVisible(false); 1251 getModel().setSelectedItem(newItem); 1252 String oldCommand = getActionCommand(); 1253 setActionCommand("comboBoxEdited"); 1254 fireActionEvent(); 1255 setActionCommand(oldCommand); 1256 } 1257 1258 1262 public void contentsChanged(ListDataEvent e) { 1263 Object oldSelection = selectedItemReminder; 1264 Object newSelection = dataModel.getSelectedItem(); 1265 if (oldSelection == null || !oldSelection.equals(newSelection)) { 1266 selectedItemChanged(); 1267 if (!selectingItem) { 1268 fireActionEvent(); 1269 } 1270 } 1271 } 1272 1273 1277 public void intervalAdded(ListDataEvent e) { 1278 if (selectedItemReminder != dataModel.getSelectedItem()) { 1279 selectedItemChanged(); 1280 } 1281 } 1282 1283 1287 public void intervalRemoved(ListDataEvent e) { 1288 contentsChanged(e); 1289 } 1290 1291 1299 public boolean selectWithKeyChar(char keyChar) { 1300 int index; 1301 1302 if ( keySelectionManager == null ) 1303 keySelectionManager = createDefaultKeySelectionManager(); 1304 1305 index = keySelectionManager.selectionForKey(keyChar,getModel()); 1306 if ( index != -1 ) { 1307 setSelectedIndex(index); 1308 return true; 1309 } 1310 else 1311 return false; 1312 } 1313 1314 1326 public void setEnabled(boolean b) { 1327 super.setEnabled(b); 1328 firePropertyChange( "enabled", !isEnabled(), isEnabled() ); 1329 } 1330 1331 1339 public void configureEditor(ComboBoxEditor anEditor, Object anItem) { 1340 anEditor.setItem(anItem); 1341 } 1342 1343 1350 public void processKeyEvent(KeyEvent e) { 1351 if ( e.getKeyCode() == KeyEvent.VK_TAB ) { 1352 hidePopup(); 1353 } 1354 super.processKeyEvent(e); 1355 } 1356 1357 1366 public void setKeySelectionManager(KeySelectionManager aManager) { 1367 keySelectionManager = aManager; 1368 } 1369 1370 1375 public KeySelectionManager getKeySelectionManager() { 1376 return keySelectionManager; 1377 } 1378 1379 1380 1385 public int getItemCount() { 1386 return dataModel.getSize(); 1387 } 1388 1389 1399 public Object getItemAt(int index) { 1400 return dataModel.getElementAt(index); 1401 } 1402 1403 1409 protected KeySelectionManager createDefaultKeySelectionManager() { 1410 return new DefaultKeySelectionManager(); 1411 } 1412 1413 1414 1421 public interface KeySelectionManager { 1422 1433 int selectionForKey(char aKey,ComboBoxModel aModel); 1434 } 1435 1436 class DefaultKeySelectionManager implements KeySelectionManager, Serializable { 1437 public int selectionForKey(char aKey,ComboBoxModel aModel) { 1438 int i,c; 1439 int currentSelection = -1; 1440 Object selectedItem = aModel.getSelectedItem(); 1441 String v; 1442 String pattern; 1443 1444 if ( selectedItem != null ) { 1445 for ( i=0,c=aModel.getSize();i<c;i++ ) { 1446 if ( selectedItem == aModel.getElementAt(i) ) { 1447 currentSelection = i; 1448 break; 1449 } 1450 } 1451 } 1452 1453 pattern = ("" + aKey).toLowerCase(); 1454 aKey = pattern.charAt(0); 1455 1456 for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) { 1457 Object elem = aModel.getElementAt(i); 1458 if (elem != null && elem.toString() != null) { 1459 v = elem.toString().toLowerCase(); 1460 if ( v.length() > 0 && v.charAt(0) == aKey ) 1461 return i; 1462 } 1463 } 1464 1465 for ( i = 0 ; i < currentSelection ; i ++ ) { 1466 Object elem = aModel.getElementAt(i); 1467 if (elem != null && elem.toString() != null) { 1468 v = elem.toString().toLowerCase(); 1469 if ( v.length() > 0 && v.charAt(0) == aKey ) 1470 return i; 1471 } 1472 } 1473 return -1; 1474 } 1475 } 1476 1477 1478 1483 private void writeObject(ObjectOutputStream s) throws IOException { 1484 s.defaultWriteObject(); 1485 if (getUIClassID().equals(uiClassID)) { 1486 byte count = JComponent.getWriteObjCounter(this); 1487 JComponent.setWriteObjCounter(this, --count); 1488 if (count == 0 && ui != null) { 1489 ui.installUI(this); 1490 } 1491 } 1492 } 1493 1494 1495 1504 protected String paramString() { 1505 String selectedItemReminderString = (selectedItemReminder != null ? 1506 selectedItemReminder.toString() : 1507 ""); 1508 String isEditableString = (isEditable ? "true" : "false"); 1509 String lightWeightPopupEnabledString = (lightWeightPopupEnabled ? 1510 "true" : "false"); 1511 1512 return super.paramString() + 1513 ",isEditable=" + isEditableString + 1514 ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString + 1515 ",maximumRowCount=" + maximumRowCount + 1516 ",selectedItemReminder=" + selectedItemReminderString; 1517 } 1518 1519 1520 1524 1533 public AccessibleContext getAccessibleContext() { 1534 if ( accessibleContext == null ) { 1535 accessibleContext = new AccessibleJComboBox(); 1536 } 1537 return accessibleContext; 1538 } 1539 1540 1554 protected class AccessibleJComboBox extends AccessibleJComponent 1555 implements AccessibleAction, AccessibleSelection { 1556 1557 1558 private JList popupList; private Accessible previousSelectedAccessible = null; 1560 1561 1564 public AccessibleJComboBox() { 1565 Accessible a = getUI().getAccessibleChild(JComboBox.this, 0); 1568 if (a instanceof javax.swing.plaf.basic.ComboPopup ) { 1569 popupList = ((javax.swing.plaf.basic.ComboPopup )a).getList(); 1571 popupList.addListSelectionListener( 1572 new AccessibleJComboBoxListSelectionListener()); 1573 } 1574 JComboBox.this.addPopupMenuListener( 1576 new AccessibleJComboBoxPopupMenuListener()); 1577 } 1578 1579 1583 private class AccessibleJComboBoxPopupMenuListener 1584 implements PopupMenuListener { 1585 1586 1589 public void popupMenuWillBecomeVisible(PopupMenuEvent e) { 1590 if (popupList == null) { 1592 return; 1593 } 1594 int selectedIndex = popupList.getSelectedIndex(); 1595 if (selectedIndex < 0) { 1596 return; 1597 } 1598 previousSelectedAccessible = 1599 popupList.getAccessibleContext().getAccessibleChild(selectedIndex); 1600 } 1601 1602 1606 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { 1607 } 1609 1610 1613 public void popupMenuCanceled(PopupMenuEvent e) { 1614 } 1616 } 1617 1618 1622 private class AccessibleJComboBoxListSelectionListener 1623 implements ListSelectionListener { 1624 1625 public void valueChanged(ListSelectionEvent e) { 1626 if (popupList == null) { 1627 return; 1628 } 1629 1630 int selectedIndex = popupList.getSelectedIndex(); 1632 if (selectedIndex < 0) { 1633 return; 1634 } 1635 Accessible selectedAccessible = 1636 popupList.getAccessibleContext().getAccessibleChild(selectedIndex); 1637 if (selectedAccessible == null) { 1638 return; 1639 } 1640 1641 PropertyChangeEvent pce = null; 1644 1645 if (previousSelectedAccessible != null) { 1646 pce = new PropertyChangeEvent(previousSelectedAccessible, 1647 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1648 AccessibleState.FOCUSED, null); 1649 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1650 null, pce); 1651 } 1652 pce = new PropertyChangeEvent(selectedAccessible, 1655 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1656 null, AccessibleState.FOCUSED); 1657 firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 1658 null, pce); 1659 1660 firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, 1663 previousSelectedAccessible, selectedAccessible); 1664 1665 previousSelectedAccessible = selectedAccessible; 1667 } 1668 } 1669 1670 1671 1678 public int getAccessibleChildrenCount() { 1679 if (ui != null) { 1681 return ui.getAccessibleChildrenCount(JComboBox.this); 1682 } else { 1683 return super.getAccessibleChildrenCount(); 1684 } 1685 } 1686 1687 1696 public Accessible getAccessibleChild(int i) { 1697 if (ui != null) { 1699 return ui.getAccessibleChild(JComboBox.this, i); 1700 } else { 1701 return super.getAccessibleChild(i); 1702 } 1703 } 1704 1705 1712 public AccessibleRole getAccessibleRole() { 1713 return AccessibleRole.COMBO_BOX; 1714 } 1715 1716 1730 public AccessibleStateSet getAccessibleStateSet() { 1731 AccessibleStateSet ass = super.getAccessibleStateSet(); 1733 if (ass == null) { 1734 ass = new AccessibleStateSet(); 1735 } 1736 if (JComboBox.this.isPopupVisible()) { 1737 ass.add(AccessibleState.EXPANDED); 1738 } else { 1739 ass.add(AccessibleState.COLLAPSED); 1740 } 1741 return ass; 1742 } 1743 1744 1752 public AccessibleAction getAccessibleAction() { 1753 return this; 1754 } 1755 1756 1761 public String getAccessibleActionDescription(int i) { 1762 if (i == 0) { 1763 return UIManager.getString("ComboBox.togglePopupText"); 1764 } 1765 else { 1766 return null; 1767 } 1768 } 1769 1770 1776 public int getAccessibleActionCount() { 1777 return 1; 1778 } 1779 1780 1786 public boolean doAccessibleAction(int i) { 1787 if (i == 0) { 1788 setPopupVisible(!isPopupVisible()); 1789 return true; 1790 } 1791 else { 1792 return false; 1793 } 1794 } 1795 1796 1797 1805 public AccessibleSelection getAccessibleSelection() { 1806 return this; 1807 } 1808 1809 1815 public int getAccessibleSelectionCount() { 1816 Object o = JComboBox.this.getSelectedItem(); 1817 if (o != null) { 1818 return 1; 1819 } else { 1820 return 0; 1821 } 1822 } 1823 1824 1836 public Accessible getAccessibleSelection(int i) { 1837 Accessible a = 1839 JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0); 1840 if (a != null && 1841 a instanceof javax.swing.plaf.basic.ComboPopup ) { 1842 1843 JList list = ((javax.swing.plaf.basic.ComboPopup )a).getList(); 1845 1846 AccessibleContext ac = list.getAccessibleContext(); 1848 if (ac != null) { 1849 AccessibleSelection as = ac.getAccessibleSelection(); 1850 if (as != null) { 1851 return as.getAccessibleSelection(i); 1852 } 1853 } 1854 } 1855 return null; 1856 } 1857 1858 1867 public boolean isAccessibleChildSelected(int i) { 1868 return JComboBox.this.getSelectedIndex() == i; 1869 } 1870 1871 1881 public void addAccessibleSelection(int i) { 1882 clearAccessibleSelection(); 1884 JComboBox.this.setSelectedIndex(i); 1885 } 1886 1887 1895 public void removeAccessibleSelection(int i) { 1896 if (JComboBox.this.getSelectedIndex() == i) { 1897 clearAccessibleSelection(); 1898 } 1899 } 1900 1901 1905 public void clearAccessibleSelection() { 1906 JComboBox.this.setSelectedIndex(-1); 1907 } 1908 1909 1913 public void selectAllAccessibleSelection() { 1914 } 1916 1917 private EditorAccessibleContext editorAccessibleContext = null; 1927 1928 private class AccessibleEditor implements Accessible { 1929 public AccessibleContext getAccessibleContext() { 1930 if (editorAccessibleContext == null) { 1931 Component c = JComboBox.this.getEditor().getEditorComponent(); 1932 if (c instanceof Accessible) { 1933 editorAccessibleContext = 1934 new EditorAccessibleContext((Accessible)c); 1935 } 1936 } 1937 return editorAccessibleContext; 1938 } 1939 } 1940 1941 1948 private class EditorAccessibleContext extends AccessibleContext { 1949 1950 private AccessibleContext ac; 1951 1952 private EditorAccessibleContext() { 1953 } 1954 1955 1959 EditorAccessibleContext(Accessible a) { 1960 this.ac = a.getAccessibleContext(); 1961 } 1962 1963 1978 public String getAccessibleName() { 1979 return ac.getAccessibleName(); 1980 } 1981 1982 1996 public void setAccessibleName(String s) { 1997 ac.setAccessibleName(s); 1998 } 1999 2000 2012 public String getAccessibleDescription() { 2013 return ac.getAccessibleDescription(); 2014 } 2015 2016 2030 public void setAccessibleDescription(String s) { 2031 ac.setAccessibleDescription(s); 2032 } 2033 2034 2052 public AccessibleRole getAccessibleRole() { 2053 return ac.getAccessibleRole(); 2054 } 2055 2056 2068 public AccessibleStateSet getAccessibleStateSet() { 2069 return ac.getAccessibleStateSet(); 2070 } 2071 2072 2078 public Accessible getAccessibleParent() { 2079 return ac.getAccessibleParent(); 2080 } 2081 2082 2090 public void setAccessibleParent(Accessible a) { 2091 ac.setAccessibleParent(a); 2092 } 2093 2094 2104 public int getAccessibleIndexInParent() { 2105 return JComboBox.this.getSelectedIndex(); 2106 } 2107 2108 2113 public int getAccessibleChildrenCount() { 2114 return ac.getAccessibleChildrenCount(); 2115 } 2116 2117 2127 public Accessible getAccessibleChild(int i) { 2128 return ac.getAccessibleChild(i); 2129 } 2130 2131 2143 public Locale getLocale() throws IllegalComponentStateException { 2144 return ac.getLocale(); 2145 } 2146 2147 2162 public void addPropertyChangeListener(PropertyChangeListener listener) { 2163 ac.addPropertyChangeListener(listener); 2164 } 2165 2166 2173 public void removePropertyChangeListener(PropertyChangeListener listener) { 2174 ac.removePropertyChangeListener(listener); 2175 } 2176 2177 2184 public AccessibleAction getAccessibleAction() { 2185 return ac.getAccessibleAction(); 2186 } 2187 2188 2195 public AccessibleComponent getAccessibleComponent() { 2196 return ac.getAccessibleComponent(); 2197 } 2198 2199 2206 public AccessibleSelection getAccessibleSelection() { 2207 return ac.getAccessibleSelection(); 2208 } 2209 2210 2217 public AccessibleText getAccessibleText() { 2218 return ac.getAccessibleText(); 2219 } 2220 2221 2228 public AccessibleEditableText getAccessibleEditableText() { 2229 return ac.getAccessibleEditableText(); 2230 } 2231 2232 2239 public AccessibleValue getAccessibleValue() { 2240 return ac.getAccessibleValue(); 2241 } 2242 2243 2251 public AccessibleIcon [] getAccessibleIcon() { 2252 return ac.getAccessibleIcon(); 2253 } 2254 2255 2262 public AccessibleRelationSet getAccessibleRelationSet() { 2263 return ac.getAccessibleRelationSet(); 2264 } 2265 2266 2273 public AccessibleTable getAccessibleTable() { 2274 return ac.getAccessibleTable(); 2275 } 2276 2277 2298 public void firePropertyChange(String propertyName, 2299 Object oldValue, 2300 Object newValue) { 2301 ac.firePropertyChange(propertyName, oldValue, newValue); 2302 } 2303 } 2304 2305 } } 2307 | Popular Tags |