src/share/classes/javax/swing/JComboBox.java

Print this page




1083             action = a;
1084             if (oldValue!=null) {
1085                 removeActionListener(oldValue);
1086                 oldValue.removePropertyChangeListener(actionPropertyChangeListener);
1087                 actionPropertyChangeListener = null;
1088             }
1089             configurePropertiesFromAction(action);
1090             if (action!=null) {
1091                 // Don't add if it is already a listener
1092                 if (!isListener(ActionListener.class, action)) {
1093                     addActionListener(action);
1094                 }
1095                 // Reverse linkage:
1096                 actionPropertyChangeListener = createActionPropertyChangeListener(action);
1097                 action.addPropertyChangeListener(actionPropertyChangeListener);
1098             }
1099             firePropertyChange("action", oldValue, action);
1100         }
1101     }
1102 
1103     private boolean isListener(Class c, ActionListener a) {
1104         boolean isListener = false;
1105         Object[] listeners = listenerList.getListenerList();
1106         for (int i = listeners.length-2; i>=0; i-=2) {
1107             if (listeners[i]==c && listeners[i+1]==a) {
1108                     isListener=true;
1109             }
1110         }
1111         return isListener;
1112     }
1113 
1114     /**
1115      * Returns the currently set <code>Action</code> for this
1116      * <code>ActionEvent</code> source, or <code>null</code> if no
1117      * <code>Action</code> is set.
1118      *
1119      * @return the <code>Action</code> for this <code>ActionEvent</code>
1120      *          source; or <code>null</code>
1121      * @since 1.3
1122      * @see Action
1123      * @see #setAction


1513 
1514     /**
1515      * The interface that defines a <code>KeySelectionManager</code>.
1516      * To qualify as a <code>KeySelectionManager</code>,
1517      * the class needs to implement the method
1518      * that identifies the list index given a character and the
1519      * combo box data model.
1520      */
1521     public interface KeySelectionManager {
1522         /** Given <code>aKey</code> and the model, returns the row
1523          *  that should become selected. Return -1 if no match was
1524          *  found.
1525          *
1526          * @param  aKey  a char value, usually indicating a keyboard key that
1527          *               was pressed
1528          * @param aModel a ComboBoxModel -- the component's data model, containing
1529          *               the list of selectable items
1530          * @return an int equal to the selected row, where 0 is the
1531          *         first item and -1 is none.
1532          */
1533         int selectionForKey(char aKey,ComboBoxModel aModel);
1534     }
1535 
1536     class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
1537         public int selectionForKey(char aKey,ComboBoxModel aModel) {
1538             int i,c;
1539             int currentSelection = -1;
1540             Object selectedItem = aModel.getSelectedItem();
1541             String v;
1542             String pattern;
1543 
1544             if ( selectedItem != null ) {
1545                 for ( i=0,c=aModel.getSize();i<c;i++ ) {
1546                     if ( selectedItem == aModel.getElementAt(i) ) {
1547                         currentSelection  =  i;
1548                         break;
1549                     }
1550                 }
1551             }
1552 
1553             pattern = ("" + aKey).toLowerCase();
1554             aKey = pattern.charAt(0);
1555 
1556             for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
1557                 Object elem = aModel.getElementAt(i);


1639 
1640     /**
1641      * This class implements accessibility support for the
1642      * <code>JComboBox</code> class.  It provides an implementation of the
1643      * Java Accessibility API appropriate to Combo Box user-interface elements.
1644      * <p>
1645      * <strong>Warning:</strong>
1646      * Serialized objects of this class will not be compatible with
1647      * future Swing releases. The current serialization support is
1648      * appropriate for short term storage or RMI between applications running
1649      * the same version of Swing.  As of 1.4, support for long term storage
1650      * of all JavaBeans&trade;
1651      * has been added to the <code>java.beans</code> package.
1652      * Please see {@link java.beans.XMLEncoder}.
1653      */
1654     @SuppressWarnings("serial") // Same-version serialization only
1655     protected class AccessibleJComboBox extends AccessibleJComponent
1656     implements AccessibleAction, AccessibleSelection {
1657 
1658 
1659         private JList popupList; // combo box popup list
1660         private Accessible previousSelectedAccessible = null;
1661 
1662         /**
1663          * Returns an AccessibleJComboBox instance
1664          * @since 1.4
1665          */
1666         public AccessibleJComboBox() {
1667             // set the combo box editor's accessible name and description
1668             JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener());
1669             setEditorNameAndDescription();
1670 
1671             // Get the popup list
1672             Accessible a = getUI().getAccessibleChild(JComboBox.this, 0);
1673             if (a instanceof javax.swing.plaf.basic.ComboPopup) {
1674                 // Listen for changes to the popup menu selection.
1675                 popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList();
1676                 popupList.addListSelectionListener(
1677                     new AccessibleJComboBoxListSelectionListener());
1678             }
1679             // Listen for popup menu show/hide events


1963          * Returns an Accessible representing the specified selected child
1964          * in the popup.  If there isn't a selection, or there are
1965          * fewer children selected than the integer passed in, the return
1966          * value will be null.
1967          * <p>Note that the index represents the i-th selected child, which
1968          * is different from the i-th child.
1969          *
1970          * @param i the zero-based index of selected children
1971          * @return the i-th selected child
1972          * @see #getAccessibleSelectionCount
1973          * @since 1.3
1974          */
1975         public Accessible getAccessibleSelection(int i) {
1976             // Get the popup
1977             Accessible a =
1978                 JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0);
1979             if (a != null &&
1980                 a instanceof javax.swing.plaf.basic.ComboPopup) {
1981 
1982                 // get the popup list
1983                 JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList();

1984 
1985                 // return the i-th selection in the popup list
1986                 AccessibleContext ac = list.getAccessibleContext();
1987                 if (ac != null) {
1988                     AccessibleSelection as = ac.getAccessibleSelection();
1989                     if (as != null) {
1990                         return as.getAccessibleSelection(i);
1991                     }
1992                 }
1993             }
1994             return null;
1995         }
1996 
1997         /**
1998          * Determines if the current child of this object is selected.
1999          *
2000          * @return true if the current child of this object is selected;
2001          *              else false
2002          * @param i the zero-based index of the child in this Accessible
2003          * object.




1083             action = a;
1084             if (oldValue!=null) {
1085                 removeActionListener(oldValue);
1086                 oldValue.removePropertyChangeListener(actionPropertyChangeListener);
1087                 actionPropertyChangeListener = null;
1088             }
1089             configurePropertiesFromAction(action);
1090             if (action!=null) {
1091                 // Don't add if it is already a listener
1092                 if (!isListener(ActionListener.class, action)) {
1093                     addActionListener(action);
1094                 }
1095                 // Reverse linkage:
1096                 actionPropertyChangeListener = createActionPropertyChangeListener(action);
1097                 action.addPropertyChangeListener(actionPropertyChangeListener);
1098             }
1099             firePropertyChange("action", oldValue, action);
1100         }
1101     }
1102 
1103     private boolean isListener(Class<?> c, ActionListener a) {
1104         boolean isListener = false;
1105         Object[] listeners = listenerList.getListenerList();
1106         for (int i = listeners.length-2; i>=0; i-=2) {
1107             if (listeners[i]==c && listeners[i+1]==a) {
1108                     isListener=true;
1109             }
1110         }
1111         return isListener;
1112     }
1113 
1114     /**
1115      * Returns the currently set <code>Action</code> for this
1116      * <code>ActionEvent</code> source, or <code>null</code> if no
1117      * <code>Action</code> is set.
1118      *
1119      * @return the <code>Action</code> for this <code>ActionEvent</code>
1120      *          source; or <code>null</code>
1121      * @since 1.3
1122      * @see Action
1123      * @see #setAction


1513 
1514     /**
1515      * The interface that defines a <code>KeySelectionManager</code>.
1516      * To qualify as a <code>KeySelectionManager</code>,
1517      * the class needs to implement the method
1518      * that identifies the list index given a character and the
1519      * combo box data model.
1520      */
1521     public interface KeySelectionManager {
1522         /** Given <code>aKey</code> and the model, returns the row
1523          *  that should become selected. Return -1 if no match was
1524          *  found.
1525          *
1526          * @param  aKey  a char value, usually indicating a keyboard key that
1527          *               was pressed
1528          * @param aModel a ComboBoxModel -- the component's data model, containing
1529          *               the list of selectable items
1530          * @return an int equal to the selected row, where 0 is the
1531          *         first item and -1 is none.
1532          */
1533         int selectionForKey(char aKey,ComboBoxModel<?> aModel);
1534     }
1535 
1536     class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
1537         public int selectionForKey(char aKey,ComboBoxModel<?> aModel) {
1538             int i,c;
1539             int currentSelection = -1;
1540             Object selectedItem = aModel.getSelectedItem();
1541             String v;
1542             String pattern;
1543 
1544             if ( selectedItem != null ) {
1545                 for ( i=0,c=aModel.getSize();i<c;i++ ) {
1546                     if ( selectedItem == aModel.getElementAt(i) ) {
1547                         currentSelection  =  i;
1548                         break;
1549                     }
1550                 }
1551             }
1552 
1553             pattern = ("" + aKey).toLowerCase();
1554             aKey = pattern.charAt(0);
1555 
1556             for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
1557                 Object elem = aModel.getElementAt(i);


1639 
1640     /**
1641      * This class implements accessibility support for the
1642      * <code>JComboBox</code> class.  It provides an implementation of the
1643      * Java Accessibility API appropriate to Combo Box user-interface elements.
1644      * <p>
1645      * <strong>Warning:</strong>
1646      * Serialized objects of this class will not be compatible with
1647      * future Swing releases. The current serialization support is
1648      * appropriate for short term storage or RMI between applications running
1649      * the same version of Swing.  As of 1.4, support for long term storage
1650      * of all JavaBeans&trade;
1651      * has been added to the <code>java.beans</code> package.
1652      * Please see {@link java.beans.XMLEncoder}.
1653      */
1654     @SuppressWarnings("serial") // Same-version serialization only
1655     protected class AccessibleJComboBox extends AccessibleJComponent
1656     implements AccessibleAction, AccessibleSelection {
1657 
1658 
1659         private JList<?> popupList; // combo box popup list
1660         private Accessible previousSelectedAccessible = null;
1661 
1662         /**
1663          * Returns an AccessibleJComboBox instance
1664          * @since 1.4
1665          */
1666         public AccessibleJComboBox() {
1667             // set the combo box editor's accessible name and description
1668             JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener());
1669             setEditorNameAndDescription();
1670 
1671             // Get the popup list
1672             Accessible a = getUI().getAccessibleChild(JComboBox.this, 0);
1673             if (a instanceof javax.swing.plaf.basic.ComboPopup) {
1674                 // Listen for changes to the popup menu selection.
1675                 popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList();
1676                 popupList.addListSelectionListener(
1677                     new AccessibleJComboBoxListSelectionListener());
1678             }
1679             // Listen for popup menu show/hide events


1963          * Returns an Accessible representing the specified selected child
1964          * in the popup.  If there isn't a selection, or there are
1965          * fewer children selected than the integer passed in, the return
1966          * value will be null.
1967          * <p>Note that the index represents the i-th selected child, which
1968          * is different from the i-th child.
1969          *
1970          * @param i the zero-based index of selected children
1971          * @return the i-th selected child
1972          * @see #getAccessibleSelectionCount
1973          * @since 1.3
1974          */
1975         public Accessible getAccessibleSelection(int i) {
1976             // Get the popup
1977             Accessible a =
1978                 JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0);
1979             if (a != null &&
1980                 a instanceof javax.swing.plaf.basic.ComboPopup) {
1981 
1982                 // get the popup list
1983                 @SuppressWarnings("unchecked")
1984                 JList<?> list = ((javax.swing.plaf.basic.ComboPopup)a).getList();
1985 
1986                 // return the i-th selection in the popup list
1987                 AccessibleContext ac = list.getAccessibleContext();
1988                 if (ac != null) {
1989                     AccessibleSelection as = ac.getAccessibleSelection();
1990                     if (as != null) {
1991                         return as.getAccessibleSelection(i);
1992                     }
1993                 }
1994             }
1995             return null;
1996         }
1997 
1998         /**
1999          * Determines if the current child of this object is selected.
2000          *
2001          * @return true if the current child of this object is selected;
2002          *              else false
2003          * @param i the zero-based index of the child in this Accessible
2004          * object.