int i,c; int currentSelection = -1; Object selectedItem = aModel.getSelectedItem(); String v; String pattern; if ( selectedItem != null ) { for ( i=0,c=aModel.getSize();i<c;i++ ) { if ( selectedItem == aModel.getElementAt(i) ) { currentSelection = i; break; } } } pattern = ("" + aKey).toLowerCase(); aKey = pattern.charAt(0); for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) { Object elem = aModel.getElementAt(i); if (elem != null && elem.toString() != null) { v = elem.toString().toLowerCase(); if ( v.length() > 0 && v.charAt(0) == aKey ) return i; } } for ( i = 0 ; i < currentSelection ; i ++ ) { Object elem = aModel.getElementAt(i); if (elem != null && elem.toString() != null) { v = elem.toString().toLowerCase(); if ( v.length() > 0 && v.charAt(0) == aKey ) return i; } } return -1; } } /** * See <code>readObject</code> and <code>writeObject</code> in * <code>JComponent</code> for more * information about serialization in Swing. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if (getUIClassID().equals(uiClassID)) { byte count = JComponent.getWriteObjCounter(this); JComponent.setWriteObjCounter(this, --count); if (count == 0 && ui != null) { ui.installUI(this); } } } /** * Returns a string representation of this <code>JComboBox</code>. * This method is intended to be used only for debugging purposes, * and the content and format of the returned string may vary between * implementations. The returned string may be empty but may not * be <code>null</code>. * * @return a string representation of this <code>JComboBox</code> */ protected String paramString() { String selectedItemReminderString = (selectedItemReminder != null ? selectedItemReminder.toString() : ""); String isEditableString = (isEditable ? "true" : "false"); String lightWeightPopupEnabledString = (lightWeightPopupEnabled ? "true" : "false"); return super.paramString() + ",isEditable=" + isEditableString + ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString + ",maximumRowCount=" + maximumRowCount + ",selectedItemReminder=" + selectedItemReminderString; } ///////////////////// Accessibility support/////////////////// /** * Gets the AccessibleContext associated with this JComboBox. * For combo boxes, the AccessibleContext takes the form of an * AccessibleJComboBox. * A new AccessibleJComboBox instance is created if necessary. * * @return an AccessibleJComboBox that serves as the * AccessibleContext of this JComboBox */ public AccessibleContext getAccessibleContext() { if ( accessibleContext == null ) { accessibleContext = new AccessibleJComboBox(); } return accessibleContext; } /** * This class implements accessibility support for the * <code>JComboBox</code> class. It provides an implementation of the * Java Accessibility API appropriate to Combo Box user-interface elements. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ protected class AccessibleJComboBox extends AccessibleJComponent implements AccessibleAction, AccessibleSelection { private JList popupList; // combo box popup list private Accessible previousSelectedAccessible = null; /** * Returns an AccessibleJComboBox instance * @since 1.4 */ public AccessibleJComboBox() { // set the combo box editor's accessible name and description JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener()); setEditorNameAndDescription(); // Get the popup list Accessible a = getUI().getAccessibleChild(JComboBox.this, 0); if (a instanceof javax.swing.plaf.basic.ComboPopup) { // Listen for changes to the popup menu selection. popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList(); popupList.addListSelectionListener( new AccessibleJComboBoxListSelectionListener()); } // Listen for popup menu show/hide events JComboBox.this.addPopupMenuListener( new AccessibleJComboBoxPopupMenuListener()); } /* * JComboBox PropertyChangeListener */ private class AccessibleJComboBoxPropertyChangeListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName() == "editor") { // set the combo box editor's accessible name // and description setEditorNameAndDescription(); } } } /* * Sets the combo box editor's accessible name and descripton */ private void setEditorNameAndDescription() { ComboBoxEditor editor = JComboBox.this.getEditor(); if (editor != null) { Component comp = editor.getEditorComponent(); if (comp instanceof Accessible) { AccessibleContext ac = comp.getAccessibleContext(); if (ac != null) { // may be null ac.setAccessibleName(getAccessibleName()); ac.setAccessibleDescription(getAccessibleDescription()); } } } } /* * Listener for combo box popup menu * TIGER - 4669379 4894434 */ private class AccessibleJComboBoxPopupMenuListener implements PopupMenuListener { /** * This method is called before the popup menu becomes visible */ public void popupMenuWillBecomeVisible(PopupMenuEvent e) { // save the initial selection if (popupList == null) { return; } int selectedIndex = popupList.getSelectedIndex(); if (selectedIndex < 0) { return; } previousSelectedAccessible = popupList.getAccessibleContext().getAccessibleChild(selectedIndex); } /** * This method is called before the popup menu becomes invisible * Note that a JPopupMenu can become invisible any time */ public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { // ignore } /** * This method is called when the popup menu is canceled */ public void popupMenuCanceled(PopupMenuEvent e) { // ignore } } /* * Handles changes to the popup list selection. * TIGER - 4669379 4894434 4933143 */ private class AccessibleJComboBoxListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { if (popupList == null) { return; } // Get the selected popup list item. int selectedIndex = popupList.getSelectedIndex(); if (selectedIndex < 0) { return; } Accessible selectedAccessible = popupList.getAccessibleContext().getAccessibleChild(selectedIndex); if (selectedAccessible == null) { return; } // Fire a FOCUSED lost PropertyChangeEvent for the // previously selected list item. PropertyChangeEvent pce; if (previousSelectedAccessible != null) { pce = new PropertyChangeEvent(previousSelectedAccessible, AccessibleContext.ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null); firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, pce); } // Fire a FOCUSED gained PropertyChangeEvent for the // currently selected list item. pce = new PropertyChangeEvent(selectedAccessible, AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED); firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, pce); // Fire the ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY event // for the combo box. firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, previousSelectedAccessible, selectedAccessible); // Save the previous selection. previousSelectedAccessible = selectedAccessible; } } /** * Returns the number of accessible children in the object. If all * of the children of this object implement Accessible, than this * method should return the number of children of this object. * * @return the number of accessible children in the object. */ public int getAccessibleChildrenCount() { // Always delegate to the UI if it exists if (ui != null) { return ui.getAccessibleChildrenCount(JComboBox.this); } else { return super.getAccessibleChildrenCount(); } } /** * Returns the nth Accessible child of the object. * The child at index zero represents the popup. * If the combo box is editable, the child at index one * represents the editor. * * @param i zero-based index of child * @return the nth Accessible child of the object */ public Accessible getAccessibleChild(int i) { // Always delegate to the UI if it exists if (ui != null) { return ui.getAccessibleChild(JComboBox.this, i); } else { return super.getAccessibleChild(i); } } /** * Get the role of this object. * * @return an instance of AccessibleRole describing the role of the * object * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.COMBO_BOX; } /** * Gets the state set of this object. The AccessibleStateSet of * an object is composed of a set of unique AccessibleStates. * A change in the AccessibleStateSet of an object will cause a * PropertyChangeEvent to be fired for the ACCESSIBLE_STATE_PROPERTY * property. * * @return an instance of AccessibleStateSet containing the * current state set of the object * @see AccessibleStateSet * @see AccessibleState * @see #addPropertyChangeListener * */ public AccessibleStateSet getAccessibleStateSet() { // TIGER - 4489748 AccessibleStateSet ass = super.getAccessibleStateSet(); if (ass == null) { ass = new AccessibleStateSet(); } if (JComboBox.this.isPopupVisible()) { ass.add(AccessibleState.EXPANDED); } else { ass.add(AccessibleState.COLLAPSED); } return ass; } /** * Get the AccessibleAction associated with this object. In the * implementation of the Java Accessibility API for this class, * return this object, which is responsible for implementing the * AccessibleAction interface on behalf of itself. * * @return this object */ public AccessibleAction getAccessibleAction() { return this; } /** * Return a description of the specified action of the object. * * @param i zero-based index of the actions */ public String getAccessibleActionDescription(int i) { if (i == 0) { return UIManager.getString("ComboBox.togglePopupText"); } else { return null; } } /** * Returns the number of Actions available in this object. The * default behavior of a combo box is to have one action. * * @return 1, the number of Actions in this object */ public int getAccessibleActionCount() { return 1; } /** * Perform the specified Action on the object * * @param i zero-based index of actions * @return true if the the action was performed; else false. */ public boolean doAccessibleAction(int i) { if (i == 0) { setPopupVisible(!isPopupVisible()); return true; } else { return false; } } /** * Get the AccessibleSelection associated with this object. In the * implementation of the Java Accessibility API for this class, * return this object, which is responsible for implementing the * AccessibleSelection interface on behalf of itself. * * @return this object */ public AccessibleSelection getAccessibleSelection() { return this; } /** * Returns the number of Accessible children currently selected. * If no children are selected, the return value will be 0. * * @return the number of items currently selected. * @since 1.3 */ public int getAccessibleSelectionCount() { Object o = JComboBox.this.getSelectedItem(); if (o != null) { return 1; } else { return 0; } } /** * Returns an Accessible representing the specified selected child * in the popup. If there isn't a selection, or there are * fewer children selected than the integer passed in, the return * value will be null. * <p>Note that the index represents the i-th selected child, which * is different from the i-th child. * * @param i the zero-based index of selected children * @return the i-th selected child * @see #getAccessibleSelectionCount * @since 1.3 */ public Accessible getAccessibleSelection(int i) { // Get the popup Accessible a = JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0); if (a != null && a instanceof javax.swing.plaf.basic.ComboPopup) { // get the popup list JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList(); // return the i-th selection in the popup list AccessibleContext ac = list.getAccessibleContext(); if (ac != null) { AccessibleSelection as = ac.getAccessibleSelection(); if (as != null) { return as.getAccessibleSelection(i); } } } return null; } /** * Determines if the current child of this object is selected. * * @return true if the current child of this object is selected; * else false * @param i the zero-based index of the child in this Accessible * object. * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public boolean isAccessibleChildSelected(int i) { return JComboBox.this.getSelectedIndex() == i; } /** * Adds the specified Accessible child of the object to the object's * selection. If the object supports multiple selections, * the specified child is added to any existing selection, otherwise * it replaces any existing selection in the object. If the * specified child is already selected, this method has no effect. * * @param i the zero-based index of the child * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public void addAccessibleSelection(int i) { // TIGER - 4856195 clearAccessibleSelection(); JComboBox.this.setSelectedIndex(i); } /** * Removes the specified child of the object from the object's * selection. If the specified item isn't currently selected, this * method has no effect. * * @param i the zero-based index of the child * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public void removeAccessibleSelection(int i) { if (JComboBox.this.getSelectedIndex() == i) {Lines 1501–2000 of 2412. Previous lines Load more
int i,c; int currentSelection = -1; Object selectedItem = aModel.getSelectedItem(); String v; String pattern; if ( selectedItem != null ) { for ( i=0,c=aModel.getSize();i<c;i++ ) { if ( selectedItem == aModel.getElementAt(i) ) { currentSelection = i; break; } } } pattern = ("" + aKey).toLowerCase(); aKey = pattern.charAt(0); for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) { Object elem = aModel.getElementAt(i); if (elem != null && elem.toString() != null) { v = elem.toString().toLowerCase(); if ( v.length() > 0 && v.charAt(0) == aKey ) return i; } } for ( i = 0 ; i < currentSelection ; i ++ ) { Object elem = aModel.getElementAt(i); if (elem != null && elem.toString() != null) { v = elem.toString().toLowerCase(); if ( v.length() > 0 && v.charAt(0) == aKey ) return i; } } return -1; } } /** * See <code>readObject</code> and <code>writeObject</code> in * <code>JComponent</code> for more * information about serialization in Swing. */ private void writeObject(ObjectOutputStream s) throws IOException { s.defaultWriteObject(); if (getUIClassID().equals(uiClassID)) { byte count = JComponent.getWriteObjCounter(this); JComponent.setWriteObjCounter(this, --count); if (count == 0 && ui != null) { ui.installUI(this); } } } /** * Returns a string representation of this <code>JComboBox</code>. * This method is intended to be used only for debugging purposes, * and the content and format of the returned string may vary between * implementations. The returned string may be empty but may not * be <code>null</code>. * * @return a string representation of this <code>JComboBox</code> */ protected String paramString() { String selectedItemReminderString = (selectedItemReminder != null ? selectedItemReminder.toString() : ""); String isEditableString = (isEditable ? "true" : "false"); String lightWeightPopupEnabledString = (lightWeightPopupEnabled ? "true" : "false"); return super.paramString() + ",isEditable=" + isEditableString + ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString + ",maximumRowCount=" + maximumRowCount + ",selectedItemReminder=" + selectedItemReminderString; } /////////////////// // Accessibility support /////////////////// /** * Gets the AccessibleContext associated with this JComboBox. * For combo boxes, the AccessibleContext takes the form of an * AccessibleJComboBox. * A new AccessibleJComboBox instance is created if necessary. * * @return an AccessibleJComboBox that serves as the * AccessibleContext of this JComboBox */ public AccessibleContext getAccessibleContext() { if ( accessibleContext == null ) { accessibleContext = new AccessibleJComboBox(); } return accessibleContext; } /** * This class implements accessibility support for the * <code>JComboBox</code> class. It provides an implementation of the * Java Accessibility API appropriate to Combo Box user-interface elements. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is * appropriate for short term storage or RMI between applications running * the same version of Swing. As of 1.4, support for long term storage * of all JavaBeans<sup><font size="-2">TM</font></sup> * has been added to the <code>java.beans</code> package. * Please see {@link java.beans.XMLEncoder}. */ protected class AccessibleJComboBox extends AccessibleJComponent implements AccessibleAction, AccessibleSelection { private JList popupList; // combo box popup list private Accessible previousSelectedAccessible = null; /** * Returns an AccessibleJComboBox instance * @since 1.4 */ public AccessibleJComboBox() { // set the combo box editor's accessible name and description JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener()); setEditorNameAndDescription(); // Get the popup list Accessible a = getUI().getAccessibleChild(JComboBox.this, 0); if (a instanceof javax.swing.plaf.basic.ComboPopup) { // Listen for changes to the popup menu selection. popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList(); popupList.addListSelectionListener( new AccessibleJComboBoxListSelectionListener()); } // Listen for popup menu show/hide events JComboBox.this.addPopupMenuListener( new AccessibleJComboBoxPopupMenuListener()); } /* * JComboBox PropertyChangeListener */ private class AccessibleJComboBoxPropertyChangeListener implements PropertyChangeListener { public void propertyChange(PropertyChangeEvent e) { if (e.getPropertyName() == "editor") { // set the combo box editor's accessible name // and description setEditorNameAndDescription(); } } } /* * Sets the combo box editor's accessible name and descripton */ private void setEditorNameAndDescription() { ComboBoxEditor editor = JComboBox.this.getEditor(); if (editor != null) { Component comp = editor.getEditorComponent(); if (comp instanceof Accessible) { AccessibleContext ac = comp.getAccessibleContext(); if (ac != null) { // may be null ac.setAccessibleName(getAccessibleName()); ac.setAccessibleDescription(getAccessibleDescription()); } } } } /* * Listener for combo box popup menu * TIGER - 4669379 4894434 */ private class AccessibleJComboBoxPopupMenuListener implements PopupMenuListener { /** * This method is called before the popup menu becomes visible */ public void popupMenuWillBecomeVisible(PopupMenuEvent e) { // save the initial selection if (popupList == null) { return; } int selectedIndex = popupList.getSelectedIndex(); if (selectedIndex < 0) { return; } previousSelectedAccessible = popupList.getAccessibleContext().getAccessibleChild(selectedIndex); } /** * This method is called before the popup menu becomes invisible * Note that a JPopupMenu can become invisible any time */ public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { // ignore } /** * This method is called when the popup menu is canceled */ public void popupMenuCanceled(PopupMenuEvent e) { // ignore } } /* * Handles changes to the popup list selection. * TIGER - 4669379 4894434 4933143 */ private class AccessibleJComboBoxListSelectionListener implements ListSelectionListener { public void valueChanged(ListSelectionEvent e) { if (popupList == null) { return; } // Get the selected popup list item. int selectedIndex = popupList.getSelectedIndex(); if (selectedIndex < 0) { return; } Accessible selectedAccessible = popupList.getAccessibleContext().getAccessibleChild(selectedIndex); if (selectedAccessible == null) { return; } // Fire a FOCUSED lost PropertyChangeEvent for the // previously selected list item. PropertyChangeEvent pce; if (previousSelectedAccessible != null) { pce = new PropertyChangeEvent(previousSelectedAccessible, AccessibleContext.ACCESSIBLE_STATE_PROPERTY, AccessibleState.FOCUSED, null); firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, pce); } // Fire a FOCUSED gained PropertyChangeEvent for the // currently selected list item. pce = new PropertyChangeEvent(selectedAccessible, AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, AccessibleState.FOCUSED); firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY, null, pce); // Fire the ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY event // for the combo box. firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY, previousSelectedAccessible, selectedAccessible); // Save the previous selection. previousSelectedAccessible = selectedAccessible; } } /** * Returns the number of accessible children in the object. If all * of the children of this object implement Accessible, than this * method should return the number of children of this object. * * @return the number of accessible children in the object. */ public int getAccessibleChildrenCount() { // Always delegate to the UI if it exists if (ui != null) { return ui.getAccessibleChildrenCount(JComboBox.this); } else { return super.getAccessibleChildrenCount(); } } /** * Returns the nth Accessible child of the object. * The child at index zero represents the popup. * If the combo box is editable, the child at index one * represents the editor. * * @param i zero-based index of child * @return the nth Accessible child of the object */ public Accessible getAccessibleChild(int i) { // Always delegate to the UI if it exists if (ui != null) { return ui.getAccessibleChild(JComboBox.this, i); } else { return super.getAccessibleChild(i); } } /** * Get the role of this object. * * @return an instance of AccessibleRole describing the role of the * object * @see AccessibleRole */ public AccessibleRole getAccessibleRole() { return AccessibleRole.COMBO_BOX; } /** * Gets the state set of this object. The AccessibleStateSet of * an object is composed of a set of unique AccessibleStates. * A change in the AccessibleStateSet of an object will cause a * PropertyChangeEvent to be fired for the ACCESSIBLE_STATE_PROPERTY * property. * * @return an instance of AccessibleStateSet containing the * current state set of the object * @see AccessibleStateSet * @see AccessibleState * @see #addPropertyChangeListener * */ public AccessibleStateSet getAccessibleStateSet() { // TIGER - 4489748 AccessibleStateSet ass = super.getAccessibleStateSet(); if (ass == null) { ass = new AccessibleStateSet(); } if (JComboBox.this.isPopupVisible()) { ass.add(AccessibleState.EXPANDED); } else { ass.add(AccessibleState.COLLAPSED); } return ass; } /** * Get the AccessibleAction associated with this object. In the * implementation of the Java Accessibility API for this class, * return this object, which is responsible for implementing the * AccessibleAction interface on behalf of itself. * * @return this object */ public AccessibleAction getAccessibleAction() { return this; } /** * Return a description of the specified action of the object. * * @param i zero-based index of the actions */ public String getAccessibleActionDescription(int i) { if (i == 0) { return UIManager.getString("ComboBox.togglePopupText"); } else { return null; } } /** * Returns the number of Actions available in this object. The * default behavior of a combo box is to have one action. * * @return 1, the number of Actions in this object */ public int getAccessibleActionCount() { return 1; } /** * Perform the specified Action on the object * * @param i zero-based index of actions * @return true if the the action was performed; else false. */ public boolean doAccessibleAction(int i) { if (i == 0) { setPopupVisible(!isPopupVisible()); return true; } else { return false; } } /** * Get the AccessibleSelection associated with this object. In the * implementation of the Java Accessibility API for this class, * return this object, which is responsible for implementing the * AccessibleSelection interface on behalf of itself. * * @return this object */ public AccessibleSelection getAccessibleSelection() { return this; } /** * Returns the number of Accessible children currently selected. * If no children are selected, the return value will be 0. * * @return the number of items currently selected. * @since 1.3 */ public int getAccessibleSelectionCount() { Object o = JComboBox.this.getSelectedItem(); if (o != null) { return 1; } else { return 0; } } /** * Returns an Accessible representing the specified selected child * in the popup. If there isn't a selection, or there are * fewer children selected than the integer passed in, the return * value will be null. * <p>Note that the index represents the i-th selected child, which * is different from the i-th child. * * @param i the zero-based index of selected children * @return the i-th selected child * @see #getAccessibleSelectionCount * @since 1.3 */ public Accessible getAccessibleSelection(int i) { // Get the popup Accessible a = JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0); if (a != null && a instanceof javax.swing.plaf.basic.ComboPopup) { // get the popup list JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList(); // return the i-th selection in the popup list AccessibleContext ac = list.getAccessibleContext(); if (ac != null) { AccessibleSelection as = ac.getAccessibleSelection(); if (as != null) { return as.getAccessibleSelection(i); } } } return null; } /** * Determines if the current child of this object is selected. * * @return true if the current child of this object is selected; * else false * @param i the zero-based index of the child in this Accessible * object. * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public boolean isAccessibleChildSelected(int i) { return JComboBox.this.getSelectedIndex() == i; } /** * Adds the specified Accessible child of the object to the object's * selection. If the object supports multiple selections, * the specified child is added to any existing selection, otherwise * it replaces any existing selection in the object. If the * specified child is already selected, this method has no effect. * * @param i the zero-based index of the child * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public void addAccessibleSelection(int i) { // TIGER - 4856195 clearAccessibleSelection(); JComboBox.this.setSelectedIndex(i); } /** * Removes the specified child of the object from the object's * selection. If the specified item isn't currently selected, this * method has no effect. * * @param i the zero-based index of the child * @see AccessibleContext#getAccessibleChild * @since 1.3 */ public void removeAccessibleSelection(int i) { if (JComboBox.this.getSelectedIndex() == i) {