0N/A/*
2362N/A * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing;
0N/A
243N/Aimport java.beans.PropertyChangeEvent;
243N/Aimport java.beans.PropertyChangeListener;
243N/Aimport java.beans.Transient;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/A
0N/Aimport java.io.Serializable;
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.IOException;
0N/A
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.*;
0N/A
0N/Aimport javax.accessibility.*;
0N/A
0N/A/**
0N/A * A component that combines a button or editable field and a drop-down list.
0N/A * The user can select a value from the drop-down list, which appears at the
0N/A * user's request. If you make the combo box editable, then the combo box
0N/A * includes an editable field into which the user can type a value.
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
0N/A * <p>
0N/A * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/combobox.html">How to Use Combo Boxes</a>
0N/A * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
0N/A * for further information.
0N/A * <p>
0N/A * @see ComboBoxModel
0N/A * @see DefaultComboBoxModel
0N/A *
4202N/A * @param <E> the type of the elements of this combo box
4202N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A combination of a text field and a drop-down list.
0N/A *
0N/A * @author Arnaud Weber
0N/A * @author Mark Davidson
0N/A */
4202N/Apublic class JComboBox<E> extends JComponent
0N/Aimplements ItemSelectable,ListDataListener,ActionListener, Accessible {
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "ComboBoxUI";
0N/A
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #getModel
0N/A * @see #setModel
0N/A */
4202N/A protected ComboBoxModel<E> dataModel;
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #getRenderer
0N/A * @see #setRenderer
0N/A */
4202N/A protected ListCellRenderer<? super E> renderer;
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #getEditor
0N/A * @see #setEditor
0N/A */
0N/A protected ComboBoxEditor editor;
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #getMaximumRowCount
0N/A * @see #setMaximumRowCount
0N/A */
0N/A protected int maximumRowCount = 8;
0N/A
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #isEditable
0N/A * @see #setEditable
0N/A */
0N/A protected boolean isEditable = false;
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #setKeySelectionManager
0N/A * @see #getKeySelectionManager
0N/A */
0N/A protected KeySelectionManager keySelectionManager = null;
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #setActionCommand
0N/A * @see #getActionCommand
0N/A */
0N/A protected String actionCommand = "comboBoxChanged";
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override. Use the accessor methods instead.
0N/A *
0N/A * @see #setLightWeightPopupEnabled
0N/A * @see #isLightWeightPopupEnabled
0N/A */
0N/A protected boolean lightWeightPopupEnabled = JPopupMenu.getDefaultLightWeightPopupEnabled();
0N/A
0N/A /**
0N/A * This protected field is implementation specific. Do not access directly
0N/A * or override.
0N/A */
0N/A protected Object selectedItemReminder = null;
0N/A
4202N/A private E prototypeDisplayValue;
0N/A
0N/A // Flag to ensure that infinite loops do not occur with ActionEvents.
0N/A private boolean firingActionEvent = false;
0N/A
0N/A // Flag to ensure the we don't get multiple ActionEvents on item selection.
0N/A private boolean selectingItem = false;
0N/A
0N/A /**
0N/A * Creates a <code>JComboBox</code> that takes its items from an
0N/A * existing <code>ComboBoxModel</code>. Since the
0N/A * <code>ComboBoxModel</code> is provided, a combo box created using
0N/A * this constructor does not create a default combo box model and
0N/A * may impact how the insert, remove and add methods behave.
0N/A *
0N/A * @param aModel the <code>ComboBoxModel</code> that provides the
0N/A * displayed list of items
0N/A * @see DefaultComboBoxModel
0N/A */
4202N/A public JComboBox(ComboBoxModel<E> aModel) {
0N/A super();
0N/A setModel(aModel);
0N/A init();
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JComboBox</code> that contains the elements
0N/A * in the specified array. By default the first item in the array
0N/A * (and therefore the data model) becomes selected.
0N/A *
0N/A * @param items an array of objects to insert into the combo box
0N/A * @see DefaultComboBoxModel
0N/A */
4202N/A public JComboBox(E[] items) {
0N/A super();
4202N/A setModel(new DefaultComboBoxModel<E>(items));
0N/A init();
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JComboBox</code> that contains the elements
0N/A * in the specified Vector. By default the first item in the vector
0N/A * (and therefore the data model) becomes selected.
0N/A *
0N/A * @param items an array of vectors to insert into the combo box
0N/A * @see DefaultComboBoxModel
0N/A */
4202N/A public JComboBox(Vector<E> items) {
0N/A super();
4202N/A setModel(new DefaultComboBoxModel<E>(items));
0N/A init();
0N/A }
0N/A
0N/A /**
0N/A * Creates a <code>JComboBox</code> with a default data model.
0N/A * The default data model is an empty list of objects.
0N/A * Use <code>addItem</code> to add items. By default the first item
0N/A * in the data model becomes selected.
0N/A *
0N/A * @see DefaultComboBoxModel
0N/A */
0N/A public JComboBox() {
0N/A super();
4202N/A setModel(new DefaultComboBoxModel<E>());
0N/A init();
0N/A }
0N/A
0N/A private void init() {
0N/A installAncestorListener();
1173N/A setUIProperty("opaque",true);
0N/A updateUI();
0N/A }
0N/A
0N/A protected void installAncestorListener() {
0N/A addAncestorListener(new AncestorListener(){
0N/A public void ancestorAdded(AncestorEvent event){ hidePopup();}
0N/A public void ancestorRemoved(AncestorEvent event){ hidePopup();}
0N/A public void ancestorMoved(AncestorEvent event){
0N/A if (event.getSource() != JComboBox.this)
0N/A hidePopup();
0N/A }});
0N/A }
0N/A
0N/A /**
0N/A * Sets the L&F object that renders this component.
0N/A *
0N/A * @param ui the <code>ComboBoxUI</code> L&F object
0N/A * @see UIDefaults#getUI
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(ComboBoxUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A /**
0N/A * Resets the UI property to a value from the current look and feel.
0N/A *
0N/A * @see JComponent#updateUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((ComboBoxUI)UIManager.getUI(this));
0N/A
4202N/A ListCellRenderer<? super E> renderer = getRenderer();
0N/A if (renderer instanceof Component) {
0N/A SwingUtilities.updateComponentTreeUI((Component)renderer);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the name of the L&F class that renders this component.
0N/A *
0N/A * @return the string "ComboBoxUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the L&F object that renders this component.
0N/A *
0N/A * @return the ComboBoxUI object that renders this component
0N/A */
0N/A public ComboBoxUI getUI() {
0N/A return(ComboBoxUI)ui;
0N/A }
0N/A
0N/A /**
0N/A * Sets the data model that the <code>JComboBox</code> uses to obtain
0N/A * the list of items.
0N/A *
0N/A * @param aModel the <code>ComboBoxModel</code> that provides the
0N/A * displayed list of items
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: Model that the combo box uses to get data to display.
0N/A */
4202N/A public void setModel(ComboBoxModel<E> aModel) {
4202N/A ComboBoxModel<E> oldModel = dataModel;
0N/A if (oldModel != null) {
0N/A oldModel.removeListDataListener(this);
0N/A }
0N/A dataModel = aModel;
0N/A dataModel.addListDataListener(this);
0N/A
0N/A // set the current selected item.
0N/A selectedItemReminder = dataModel.getSelectedItem();
0N/A
0N/A firePropertyChange( "model", oldModel, dataModel);
0N/A }
0N/A
0N/A /**
0N/A * Returns the data model currently used by the <code>JComboBox</code>.
0N/A *
0N/A * @return the <code>ComboBoxModel</code> that provides the displayed
0N/A * list of items
0N/A */
4202N/A public ComboBoxModel<E> getModel() {
0N/A return dataModel;
0N/A }
0N/A
0N/A /*
0N/A * Properties
0N/A */
0N/A
0N/A /**
0N/A * Sets the <code>lightWeightPopupEnabled</code> property, which
0N/A * provides a hint as to whether or not a lightweight
0N/A * <code>Component</code> should be used to contain the
0N/A * <code>JComboBox</code>, versus a heavyweight
0N/A * <code>Component</code> such as a <code>Panel</code>
0N/A * or a <code>Window</code>. The decision of lightweight
0N/A * versus heavyweight is ultimately up to the
0N/A * <code>JComboBox</code>. Lightweight windows are more
0N/A * efficient than heavyweight windows, but lightweight
0N/A * and heavyweight components do not mix well in a GUI.
0N/A * If your application mixes lightweight and heavyweight
0N/A * components, you should disable lightweight popups.
0N/A * The default value for the <code>lightWeightPopupEnabled</code>
0N/A * property is <code>true</code>, unless otherwise specified
0N/A * by the look and feel. Some look and feels always use
0N/A * heavyweight popups, no matter what the value of this property.
0N/A * <p>
0N/A * See the article <a href="http://java.sun.com/products/jfc/tsc/articles/mixing/index.html">Mixing Heavy and Light Components</a>
0N/A * on <a href="http://java.sun.com/products/jfc/tsc">
0N/A * <em>The Swing Connection</em></a>
0N/A * This method fires a property changed event.
0N/A *
0N/A * @param aFlag if <code>true</code>, lightweight popups are desired
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * expert: true
0N/A * description: Set to <code>false</code> to require heavyweight popups.
0N/A */
0N/A public void setLightWeightPopupEnabled(boolean aFlag) {
0N/A boolean oldFlag = lightWeightPopupEnabled;
0N/A lightWeightPopupEnabled = aFlag;
0N/A firePropertyChange("lightWeightPopupEnabled", oldFlag, lightWeightPopupEnabled);
0N/A }
0N/A
0N/A /**
0N/A * Gets the value of the <code>lightWeightPopupEnabled</code>
0N/A * property.
0N/A *
0N/A * @return the value of the <code>lightWeightPopupEnabled</code>
0N/A * property
0N/A * @see #setLightWeightPopupEnabled
0N/A */
0N/A public boolean isLightWeightPopupEnabled() {
0N/A return lightWeightPopupEnabled;
0N/A }
0N/A
0N/A /**
0N/A * Determines whether the <code>JComboBox</code> field is editable.
0N/A * An editable <code>JComboBox</code> allows the user to type into the
0N/A * field or selected an item from the list to initialize the field,
0N/A * after which it can be edited. (The editing affects only the field,
0N/A * the list item remains intact.) A non editable <code>JComboBox</code>
0N/A * displays the selected item in the field,
0N/A * but the selection cannot be modified.
0N/A *
0N/A * @param aFlag a boolean value, where true indicates that the
0N/A * field is editable
0N/A *
0N/A * @beaninfo
0N/A * bound: true
0N/A * preferred: true
0N/A * description: If true, the user can type a new value in the combo box.
0N/A */
0N/A public void setEditable(boolean aFlag) {
0N/A boolean oldFlag = isEditable;
0N/A isEditable = aFlag;
0N/A firePropertyChange( "editable", oldFlag, isEditable );
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the <code>JComboBox</code> is editable.
0N/A * By default, a combo box is not editable.
0N/A *
0N/A * @return true if the <code>JComboBox</code> is editable, else false
0N/A */
0N/A public boolean isEditable() {
0N/A return isEditable;
0N/A }
0N/A
0N/A /**
0N/A * Sets the maximum number of rows the <code>JComboBox</code> displays.
0N/A * If the number of objects in the model is greater than count,
0N/A * the combo box uses a scrollbar.
0N/A *
0N/A * @param count an integer specifying the maximum number of items to
0N/A * display in the list before using a scrollbar
0N/A * @beaninfo
0N/A * bound: true
0N/A * preferred: true
0N/A * description: The maximum number of rows the popup should have
0N/A */
0N/A public void setMaximumRowCount(int count) {
0N/A int oldCount = maximumRowCount;
0N/A maximumRowCount = count;
0N/A firePropertyChange( "maximumRowCount", oldCount, maximumRowCount );
0N/A }
0N/A
0N/A /**
0N/A * Returns the maximum number of items the combo box can display
0N/A * without a scrollbar
0N/A *
0N/A * @return an integer specifying the maximum number of items that are
0N/A * displayed in the list before using a scrollbar
0N/A */
0N/A public int getMaximumRowCount() {
0N/A return maximumRowCount;
0N/A }
0N/A
0N/A /**
0N/A * Sets the renderer that paints the list items and the item selected from the list in
0N/A * the JComboBox field. The renderer is used if the JComboBox is not
0N/A * editable. If it is editable, the editor is used to render and edit
0N/A * the selected item.
0N/A * <p>
0N/A * The default renderer displays a string or an icon.
0N/A * Other renderers can handle graphic images and composite items.
0N/A * <p>
0N/A * To display the selected item,
0N/A * <code>aRenderer.getListCellRendererComponent</code>
0N/A * is called, passing the list object and an index of -1.
0N/A *
0N/A * @param aRenderer the <code>ListCellRenderer</code> that
0N/A * displays the selected item
0N/A * @see #setEditor
0N/A * @beaninfo
0N/A * bound: true
0N/A * expert: true
0N/A * description: The renderer that paints the item selected in the list.
0N/A */
4202N/A public void setRenderer(ListCellRenderer<? super E> aRenderer) {
4202N/A ListCellRenderer<? super E> oldRenderer = renderer;
0N/A renderer = aRenderer;
0N/A firePropertyChange( "renderer", oldRenderer, renderer );
0N/A invalidate();
0N/A }
0N/A
0N/A /**
0N/A * Returns the renderer used to display the selected item in the
0N/A * <code>JComboBox</code> field.
0N/A *
0N/A * @return the <code>ListCellRenderer</code> that displays
0N/A * the selected item.
0N/A */
4202N/A public ListCellRenderer<? super E> getRenderer() {
0N/A return renderer;
0N/A }
0N/A
0N/A /**
0N/A * Sets the editor used to paint and edit the selected item in the
0N/A * <code>JComboBox</code> field. The editor is used only if the
0N/A * receiving <code>JComboBox</code> is editable. If not editable,
0N/A * the combo box uses the renderer to paint the selected item.
0N/A *
0N/A * @param anEditor the <code>ComboBoxEditor</code> that
0N/A * displays the selected item
0N/A * @see #setRenderer
0N/A * @beaninfo
0N/A * bound: true
0N/A * expert: true
0N/A * description: The editor that combo box uses to edit the current value
0N/A */
0N/A public void setEditor(ComboBoxEditor anEditor) {
0N/A ComboBoxEditor oldEditor = editor;
0N/A
0N/A if ( editor != null ) {
0N/A editor.removeActionListener(this);
0N/A }
0N/A editor = anEditor;
0N/A if ( editor != null ) {
0N/A editor.addActionListener(this);
0N/A }
0N/A firePropertyChange( "editor", oldEditor, editor );
0N/A }
0N/A
0N/A /**
0N/A * Returns the editor used to paint and edit the selected item in the
0N/A * <code>JComboBox</code> field.
0N/A *
0N/A * @return the <code>ComboBoxEditor</code> that displays the selected item
0N/A */
0N/A public ComboBoxEditor getEditor() {
0N/A return editor;
0N/A }
0N/A
0N/A //
0N/A // Selection
0N/A //
0N/A
0N/A /**
0N/A * Sets the selected item in the combo box display area to the object in
0N/A * the argument.
0N/A * If <code>anObject</code> is in the list, the display area shows
0N/A * <code>anObject</code> selected.
0N/A * <p>
0N/A * If <code>anObject</code> is <i>not</i> in the list and the combo box is
0N/A * uneditable, it will not change the current selection. For editable
0N/A * combo boxes, the selection will change to <code>anObject</code>.
0N/A * <p>
0N/A * If this constitutes a change in the selected item,
0N/A * <code>ItemListener</code>s added to the combo box will be notified with
0N/A * one or two <code>ItemEvent</code>s.
0N/A * If there is a current selected item, an <code>ItemEvent</code> will be
0N/A * fired and the state change will be <code>ItemEvent.DESELECTED</code>.
0N/A * If <code>anObject</code> is in the list and is not currently selected
0N/A * then an <code>ItemEvent</code> will be fired and the state change will
0N/A * be <code>ItemEvent.SELECTED</code>.
0N/A * <p>
0N/A * <code>ActionListener</code>s added to the combo box will be notified
0N/A * with an <code>ActionEvent</code> when this method is called.
0N/A *
0N/A * @param anObject the list object to select; use <code>null</code> to
0N/A clear the selection
0N/A * @beaninfo
0N/A * preferred: true
0N/A * description: Sets the selected item in the JComboBox.
0N/A */
0N/A public void setSelectedItem(Object anObject) {
0N/A Object oldSelection = selectedItemReminder;
0N/A Object objectToSelect = anObject;
0N/A if (oldSelection == null || !oldSelection.equals(anObject)) {
0N/A
0N/A if (anObject != null && !isEditable()) {
0N/A // For non editable combo boxes, an invalid selection
0N/A // will be rejected.
0N/A boolean found = false;
0N/A for (int i = 0; i < dataModel.getSize(); i++) {
4202N/A E element = dataModel.getElementAt(i);
0N/A if (anObject.equals(element)) {
0N/A found = true;
0N/A objectToSelect = element;
0N/A break;
0N/A }
0N/A }
0N/A if (!found) {
0N/A return;
0N/A }
0N/A }
0N/A
0N/A // Must toggle the state of this flag since this method
0N/A // call may result in ListDataEvents being fired.
0N/A selectingItem = true;
0N/A dataModel.setSelectedItem(objectToSelect);
0N/A selectingItem = false;
0N/A
0N/A if (selectedItemReminder != dataModel.getSelectedItem()) {
0N/A // in case a users implementation of ComboBoxModel
0N/A // doesn't fire a ListDataEvent when the selection
0N/A // changes.
0N/A selectedItemChanged();
0N/A }
0N/A }
0N/A fireActionEvent();
0N/A }
0N/A
0N/A /**
0N/A * Returns the current selected item.
0N/A * <p>
0N/A * If the combo box is editable, then this value may not have been added
0N/A * to the combo box with <code>addItem</code>, <code>insertItemAt</code>
0N/A * or the data constructors.
0N/A *
0N/A * @return the current selected Object
0N/A * @see #setSelectedItem
0N/A */
0N/A public Object getSelectedItem() {
0N/A return dataModel.getSelectedItem();
0N/A }
0N/A
0N/A /**
0N/A * Selects the item at index <code>anIndex</code>.
0N/A *
0N/A * @param anIndex an integer specifying the list item to select,
0N/A * where 0 specifies the first item in the list and -1 indicates no selection
0N/A * @exception IllegalArgumentException if <code>anIndex</code> < -1 or
0N/A * <code>anIndex</code> is greater than or equal to size
0N/A * @beaninfo
0N/A * preferred: true
0N/A * description: The item at index is selected.
0N/A */
0N/A public void setSelectedIndex(int anIndex) {
0N/A int size = dataModel.getSize();
0N/A
0N/A if ( anIndex == -1 ) {
0N/A setSelectedItem( null );
0N/A } else if ( anIndex < -1 || anIndex >= size ) {
0N/A throw new IllegalArgumentException("setSelectedIndex: " + anIndex + " out of bounds");
0N/A } else {
0N/A setSelectedItem(dataModel.getElementAt(anIndex));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the first item in the list that matches the given item.
0N/A * The result is not always defined if the <code>JComboBox</code>
0N/A * allows selected items that are not in the list.
0N/A * Returns -1 if there is no selected item or if the user specified
0N/A * an item which is not in the list.
0N/A
0N/A * @return an integer specifying the currently selected list item,
0N/A * where 0 specifies
0N/A * the first item in the list;
0N/A * or -1 if no item is selected or if
0N/A * the currently selected item is not in the list
0N/A */
243N/A @Transient
0N/A public int getSelectedIndex() {
0N/A Object sObject = dataModel.getSelectedItem();
0N/A int i,c;
4202N/A E obj;
0N/A
0N/A for ( i=0,c=dataModel.getSize();i<c;i++ ) {
0N/A obj = dataModel.getElementAt(i);
0N/A if ( obj != null && obj.equals(sObject) )
0N/A return i;
0N/A }
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the "prototypical display" value - an Object used
0N/A * for the calculation of the display height and width.
0N/A *
0N/A * @return the value of the <code>prototypeDisplayValue</code> property
0N/A * @see #setPrototypeDisplayValue
0N/A * @since 1.4
0N/A */
4202N/A public E getPrototypeDisplayValue() {
0N/A return prototypeDisplayValue;
0N/A }
0N/A
0N/A /**
0N/A * Sets the prototype display value used to calculate the size of the display
0N/A * for the UI portion.
0N/A * <p>
0N/A * If a prototype display value is specified, the preferred size of
0N/A * the combo box is calculated by configuring the renderer with the
0N/A * prototype display value and obtaining its preferred size. Specifying
0N/A * the preferred display value is often useful when the combo box will be
0N/A * displaying large amounts of data. If no prototype display value has
0N/A * been specified, the renderer must be configured for each value from
0N/A * the model and its preferred size obtained, which can be
0N/A * relatively expensive.
0N/A *
0N/A * @param prototypeDisplayValue
0N/A * @see #getPrototypeDisplayValue
0N/A * @since 1.4
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The display prototype value, used to compute display width and height.
0N/A */
4202N/A public void setPrototypeDisplayValue(E prototypeDisplayValue) {
0N/A Object oldValue = this.prototypeDisplayValue;
0N/A this.prototypeDisplayValue = prototypeDisplayValue;
0N/A firePropertyChange("prototypeDisplayValue", oldValue, prototypeDisplayValue);
0N/A }
0N/A
0N/A /**
0N/A * Adds an item to the item list.
0N/A * This method works only if the <code>JComboBox</code> uses a
0N/A * mutable data model.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Focus and keyboard navigation problems may arise if you add duplicate
0N/A * String objects. A workaround is to add new objects instead of String
0N/A * objects and make sure that the toString() method is defined.
0N/A * For example:
0N/A * <pre>
0N/A * comboBox.addItem(makeObj("Item 1"));
0N/A * comboBox.addItem(makeObj("Item 1"));
0N/A * ...
0N/A * private Object makeObj(final String item) {
0N/A * return new Object() { public String toString() { return item; } };
0N/A * }
0N/A * </pre>
0N/A *
4202N/A * @param item the item to add to the list
0N/A * @see MutableComboBoxModel
0N/A */
4202N/A public void addItem(E item) {
0N/A checkMutableComboBoxModel();
4202N/A ((MutableComboBoxModel<E>)dataModel).addElement(item);
0N/A }
0N/A
0N/A /**
0N/A * Inserts an item into the item list at a given index.
0N/A * This method works only if the <code>JComboBox</code> uses a
0N/A * mutable data model.
0N/A *
4202N/A * @param item the item to add to the list
0N/A * @param index an integer specifying the position at which
0N/A * to add the item
0N/A * @see MutableComboBoxModel
0N/A */
4202N/A public void insertItemAt(E item, int index) {
0N/A checkMutableComboBoxModel();
4202N/A ((MutableComboBoxModel<E>)dataModel).insertElementAt(item,index);
0N/A }
0N/A
0N/A /**
0N/A * Removes an item from the item list.
0N/A * This method works only if the <code>JComboBox</code> uses a
0N/A * mutable data model.
0N/A *
0N/A * @param anObject the object to remove from the item list
0N/A * @see MutableComboBoxModel
0N/A */
0N/A public void removeItem(Object anObject) {
0N/A checkMutableComboBoxModel();
0N/A ((MutableComboBoxModel)dataModel).removeElement(anObject);
0N/A }
0N/A
0N/A /**
0N/A * Removes the item at <code>anIndex</code>
0N/A * This method works only if the <code>JComboBox</code> uses a
0N/A * mutable data model.
0N/A *
0N/A * @param anIndex an int specifying the index of the item to remove,
0N/A * where 0
0N/A * indicates the first item in the list
0N/A * @see MutableComboBoxModel
0N/A */
0N/A public void removeItemAt(int anIndex) {
0N/A checkMutableComboBoxModel();
4202N/A ((MutableComboBoxModel<E>)dataModel).removeElementAt( anIndex );
0N/A }
0N/A
0N/A /**
0N/A * Removes all items from the item list.
0N/A */
0N/A public void removeAllItems() {
0N/A checkMutableComboBoxModel();
4202N/A MutableComboBoxModel<E> model = (MutableComboBoxModel<E>)dataModel;
0N/A int size = model.getSize();
0N/A
0N/A if ( model instanceof DefaultComboBoxModel ) {
0N/A ((DefaultComboBoxModel)model).removeAllElements();
0N/A }
0N/A else {
0N/A for ( int i = 0; i < size; ++i ) {
4202N/A E element = model.getElementAt( 0 );
0N/A model.removeElement( element );
0N/A }
0N/A }
0N/A selectedItemReminder = null;
0N/A if (isEditable()) {
0N/A editor.setItem(null);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks that the <code>dataModel</code> is an instance of
0N/A * <code>MutableComboBoxModel</code>. If not, it throws an exception.
0N/A * @exception RuntimeException if <code>dataModel</code> is not an
0N/A * instance of <code>MutableComboBoxModel</code>.
0N/A */
0N/A void checkMutableComboBoxModel() {
0N/A if ( !(dataModel instanceof MutableComboBoxModel) )
0N/A throw new RuntimeException("Cannot use this method with a non-Mutable data model.");
0N/A }
0N/A
0N/A /**
0N/A * Causes the combo box to display its popup window.
0N/A * @see #setPopupVisible
0N/A */
0N/A public void showPopup() {
0N/A setPopupVisible(true);
0N/A }
0N/A
0N/A /**
0N/A * Causes the combo box to close its popup window.
0N/A * @see #setPopupVisible
0N/A */
0N/A public void hidePopup() {
0N/A setPopupVisible(false);
0N/A }
0N/A
0N/A /**
0N/A * Sets the visibility of the popup.
0N/A */
0N/A public void setPopupVisible(boolean v) {
0N/A getUI().setPopupVisible(this, v);
0N/A }
0N/A
0N/A /**
0N/A * Determines the visibility of the popup.
0N/A *
0N/A * @return true if the popup is visible, otherwise returns false
0N/A */
0N/A public boolean isPopupVisible() {
0N/A return getUI().isPopupVisible(this);
0N/A }
0N/A
0N/A /** Selection **/
0N/A
0N/A /**
0N/A * Adds an <code>ItemListener</code>.
0N/A * <p>
0N/A * <code>aListener</code> will receive one or two <code>ItemEvent</code>s when
0N/A * the selected item changes.
0N/A *
0N/A * @param aListener the <code>ItemListener</code> that is to be notified
0N/A * @see #setSelectedItem
0N/A */
0N/A public void addItemListener(ItemListener aListener) {
0N/A listenerList.add(ItemListener.class,aListener);
0N/A }
0N/A
0N/A /** Removes an <code>ItemListener</code>.
0N/A *
0N/A * @param aListener the <code>ItemListener</code> to remove
0N/A */
0N/A public void removeItemListener(ItemListener aListener) {
0N/A listenerList.remove(ItemListener.class,aListener);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>ItemListener</code>s added
0N/A * to this JComboBox with addItemListener().
0N/A *
0N/A * @return all of the <code>ItemListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public ItemListener[] getItemListeners() {
625N/A return listenerList.getListeners(ItemListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Adds an <code>ActionListener</code>.
0N/A * <p>
0N/A * The <code>ActionListener</code> will receive an <code>ActionEvent</code>
0N/A * when a selection has been made. If the combo box is editable, then
0N/A * an <code>ActionEvent</code> will be fired when editing has stopped.
0N/A *
0N/A * @param l the <code>ActionListener</code> that is to be notified
0N/A * @see #setSelectedItem
0N/A */
0N/A public void addActionListener(ActionListener l) {
0N/A listenerList.add(ActionListener.class,l);
0N/A }
0N/A
0N/A /** Removes an <code>ActionListener</code>.
0N/A *
0N/A * @param l the <code>ActionListener</code> to remove
0N/A */
0N/A public void removeActionListener(ActionListener l) {
0N/A if ((l != null) && (getAction() == l)) {
0N/A setAction(null);
0N/A } else {
0N/A listenerList.remove(ActionListener.class, l);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>ActionListener</code>s added
0N/A * to this JComboBox with addActionListener().
0N/A *
0N/A * @return all of the <code>ActionListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public ActionListener[] getActionListeners() {
625N/A return listenerList.getListeners(ActionListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Adds a <code>PopupMenu</code> listener which will listen to notification
0N/A * messages from the popup portion of the combo box.
0N/A * <p>
0N/A * For all standard look and feels shipped with Java, the popup list
0N/A * portion of combo box is implemented as a <code>JPopupMenu</code>.
0N/A * A custom look and feel may not implement it this way and will
0N/A * therefore not receive the notification.
0N/A *
0N/A * @param l the <code>PopupMenuListener</code> to add
0N/A * @since 1.4
0N/A */
0N/A public void addPopupMenuListener(PopupMenuListener l) {
0N/A listenerList.add(PopupMenuListener.class,l);
0N/A }
0N/A
0N/A /**
0N/A * Removes a <code>PopupMenuListener</code>.
0N/A *
0N/A * @param l the <code>PopupMenuListener</code> to remove
0N/A * @see #addPopupMenuListener
0N/A * @since 1.4
0N/A */
0N/A public void removePopupMenuListener(PopupMenuListener l) {
0N/A listenerList.remove(PopupMenuListener.class,l);
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of all the <code>PopupMenuListener</code>s added
0N/A * to this JComboBox with addPopupMenuListener().
0N/A *
0N/A * @return all of the <code>PopupMenuListener</code>s added or an empty
0N/A * array if no listeners have been added
0N/A * @since 1.4
0N/A */
0N/A public PopupMenuListener[] getPopupMenuListeners() {
625N/A return listenerList.getListeners(PopupMenuListener.class);
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListener</code>s that the popup portion of the
0N/A * combo box will become visible.
0N/A * <p>
0N/A * This method is public but should not be called by anything other than
0N/A * the UI delegate.
0N/A * @see #addPopupMenuListener
0N/A * @since 1.4
0N/A */
0N/A public void firePopupMenuWillBecomeVisible() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeVisible(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListener</code>s that the popup portion of the
0N/A * combo box has become invisible.
0N/A * <p>
0N/A * This method is public but should not be called by anything other than
0N/A * the UI delegate.
0N/A * @see #addPopupMenuListener
0N/A * @since 1.4
0N/A */
0N/A public void firePopupMenuWillBecomeInvisible() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuWillBecomeInvisible(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies <code>PopupMenuListener</code>s that the popup portion of the
0N/A * combo box has been canceled.
0N/A * <p>
0N/A * This method is public but should not be called by anything other than
0N/A * the UI delegate.
0N/A * @see #addPopupMenuListener
0N/A * @since 1.4
0N/A */
0N/A public void firePopupMenuCanceled() {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A PopupMenuEvent e=null;
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==PopupMenuListener.class) {
0N/A if (e == null)
0N/A e = new PopupMenuEvent(this);
0N/A ((PopupMenuListener)listeners[i+1]).popupMenuCanceled(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Sets the action command that should be included in the event
0N/A * sent to action listeners.
0N/A *
0N/A * @param aCommand a string containing the "command" that is sent
0N/A * to action listeners; the same listener can then
0N/A * do different things depending on the command it
0N/A * receives
0N/A */
0N/A public void setActionCommand(String aCommand) {
0N/A actionCommand = aCommand;
0N/A }
0N/A
0N/A /**
0N/A * Returns the action command that is included in the event sent to
0N/A * action listeners.
0N/A *
0N/A * @return the string containing the "command" that is sent
0N/A * to action listeners.
0N/A */
0N/A public String getActionCommand() {
0N/A return actionCommand;
0N/A }
0N/A
0N/A private Action action;
0N/A private PropertyChangeListener actionPropertyChangeListener;
0N/A
0N/A /**
0N/A * Sets the <code>Action</code> for the <code>ActionEvent</code> source.
0N/A * The new <code>Action</code> replaces any previously set
0N/A * <code>Action</code> but does not affect <code>ActionListeners</code>
0N/A * independently added with <code>addActionListener</code>.
0N/A * If the <code>Action</code> is already a registered
0N/A * <code>ActionListener</code> for the <code>ActionEvent</code> source,
0N/A * it is not re-registered.
0N/A * <p>
0N/A * Setting the <code>Action</code> results in immediately changing
0N/A * all the properties described in <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a>.
0N/A * Subsequently, the combobox's properties are automatically updated
0N/A * as the <code>Action</code>'s properties change.
0N/A * <p>
0N/A * This method uses three other methods to set
0N/A * and help track the <code>Action</code>'s property values.
0N/A * It uses the <code>configurePropertiesFromAction</code> method
0N/A * to immediately change the combobox's properties.
0N/A * To track changes in the <code>Action</code>'s property values,
0N/A * this method registers the <code>PropertyChangeListener</code>
0N/A * returned by <code>createActionPropertyChangeListener</code>. The
0N/A * default {@code PropertyChangeListener} invokes the
0N/A * {@code actionPropertyChanged} method when a property in the
0N/A * {@code Action} changes.
0N/A *
0N/A * @param a the <code>Action</code> for the <code>JComboBox</code>,
0N/A * or <code>null</code>.
0N/A * @since 1.3
0N/A * @see Action
0N/A * @see #getAction
0N/A * @see #configurePropertiesFromAction
0N/A * @see #createActionPropertyChangeListener
0N/A * @see #actionPropertyChanged
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: the Action instance connected with this ActionEvent source
0N/A */
0N/A public void setAction(Action a) {
0N/A Action oldValue = getAction();
0N/A if (action==null || !action.equals(a)) {
0N/A action = a;
0N/A if (oldValue!=null) {
0N/A removeActionListener(oldValue);
0N/A oldValue.removePropertyChangeListener(actionPropertyChangeListener);
0N/A actionPropertyChangeListener = null;
0N/A }
0N/A configurePropertiesFromAction(action);
0N/A if (action!=null) {
0N/A // Don't add if it is already a listener
0N/A if (!isListener(ActionListener.class, action)) {
0N/A addActionListener(action);
0N/A }
0N/A // Reverse linkage:
0N/A actionPropertyChangeListener = createActionPropertyChangeListener(action);
0N/A action.addPropertyChangeListener(actionPropertyChangeListener);
0N/A }
0N/A firePropertyChange("action", oldValue, action);
0N/A }
0N/A }
0N/A
0N/A private boolean isListener(Class c, ActionListener a) {
0N/A boolean isListener = false;
0N/A Object[] listeners = listenerList.getListenerList();
0N/A for (int i = listeners.length-2; i>=0; i-=2) {
0N/A if (listeners[i]==c && listeners[i+1]==a) {
0N/A isListener=true;
0N/A }
0N/A }
0N/A return isListener;
0N/A }
0N/A
0N/A /**
0N/A * Returns the currently set <code>Action</code> for this
0N/A * <code>ActionEvent</code> source, or <code>null</code> if no
0N/A * <code>Action</code> is set.
0N/A *
0N/A * @return the <code>Action</code> for this <code>ActionEvent</code>
0N/A * source; or <code>null</code>
0N/A * @since 1.3
0N/A * @see Action
0N/A * @see #setAction
0N/A */
0N/A public Action getAction() {
0N/A return action;
0N/A }
0N/A
0N/A /**
0N/A * Sets the properties on this combobox to match those in the specified
0N/A * <code>Action</code>. Refer to <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for more
0N/A * details as to which properties this sets.
0N/A *
0N/A * @param a the <code>Action</code> from which to get the properties,
0N/A * or <code>null</code>
0N/A * @since 1.3
0N/A * @see Action
0N/A * @see #setAction
0N/A */
0N/A protected void configurePropertiesFromAction(Action a) {
0N/A AbstractAction.setEnabledFromAction(this, a);
0N/A AbstractAction.setToolTipTextFromAction(this, a);
0N/A setActionCommandFromAction(a);
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a <code>PropertyChangeListener</code> that is
0N/A * responsible for listening for changes from the specified
0N/A * <code>Action</code> and updating the appropriate properties.
0N/A * <p>
0N/A * <b>Warning:</b> If you subclass this do not create an anonymous
0N/A * inner class. If you do the lifetime of the combobox will be tied to
0N/A * that of the <code>Action</code>.
0N/A *
0N/A * @param a the combobox's action
0N/A * @since 1.3
0N/A * @see Action
0N/A * @see #setAction
0N/A */
0N/A protected PropertyChangeListener createActionPropertyChangeListener(Action a) {
0N/A return new ComboBoxActionPropertyChangeListener(this, a);
0N/A }
0N/A
0N/A /**
0N/A * Updates the combobox's state in response to property changes in
0N/A * associated action. This method is invoked from the
0N/A * {@code PropertyChangeListener} returned from
0N/A * {@code createActionPropertyChangeListener}. Subclasses do not normally
0N/A * need to invoke this. Subclasses that support additional {@code Action}
0N/A * properties should override this and
0N/A * {@code configurePropertiesFromAction}.
0N/A * <p>
0N/A * Refer to the table at <a href="Action.html#buttonActions">
0N/A * Swing Components Supporting <code>Action</code></a> for a list of
0N/A * the properties this method sets.
0N/A *
0N/A * @param action the <code>Action</code> associated with this combobox
0N/A * @param propertyName the name of the property that changed
0N/A * @since 1.6
0N/A * @see Action
0N/A * @see #configurePropertiesFromAction
0N/A */
0N/A protected void actionPropertyChanged(Action action, String propertyName) {
0N/A if (propertyName == Action.ACTION_COMMAND_KEY) {
0N/A setActionCommandFromAction(action);
0N/A } else if (propertyName == "enabled") {
0N/A AbstractAction.setEnabledFromAction(this, action);
0N/A } else if (Action.SHORT_DESCRIPTION == propertyName) {
0N/A AbstractAction.setToolTipTextFromAction(this, action);
0N/A }
0N/A }
0N/A
0N/A private void setActionCommandFromAction(Action a) {
0N/A setActionCommand((a != null) ?
0N/A (String)a.getValue(Action.ACTION_COMMAND_KEY) :
0N/A null);
0N/A }
0N/A
0N/A
0N/A private static class ComboBoxActionPropertyChangeListener
4202N/A extends ActionPropertyChangeListener<JComboBox<?>> {
4202N/A ComboBoxActionPropertyChangeListener(JComboBox<?> b, Action a) {
0N/A super(b, a);
0N/A }
4202N/A protected void actionPropertyChanged(JComboBox<?> cb,
0N/A Action action,
0N/A PropertyChangeEvent e) {
0N/A if (AbstractAction.shouldReconfigure(e)) {
0N/A cb.configurePropertiesFromAction(action);
0N/A } else {
0N/A cb.actionPropertyChanged(action, e.getPropertyName());
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A * @param e the event of interest
0N/A *
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireItemStateChanged(ItemEvent e) {
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for ( int i = listeners.length-2; i>=0; i-=2 ) {
0N/A if ( listeners[i]==ItemListener.class ) {
0N/A // Lazily create the event:
0N/A // if (changeEvent == null)
0N/A // changeEvent = new ChangeEvent(this);
0N/A ((ItemListener)listeners[i+1]).itemStateChanged(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Notifies all listeners that have registered interest for
0N/A * notification on this event type.
0N/A *
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireActionEvent() {
0N/A if (!firingActionEvent) {
0N/A // Set flag to ensure that an infinite loop is not created
0N/A firingActionEvent = true;
0N/A ActionEvent e = null;
0N/A // Guaranteed to return a non-null array
0N/A Object[] listeners = listenerList.getListenerList();
0N/A long mostRecentEventTime = EventQueue.getMostRecentEventTime();
0N/A int modifiers = 0;
0N/A AWTEvent currentEvent = EventQueue.getCurrentEvent();
0N/A if (currentEvent instanceof InputEvent) {
0N/A modifiers = ((InputEvent)currentEvent).getModifiers();
0N/A } else if (currentEvent instanceof ActionEvent) {
0N/A modifiers = ((ActionEvent)currentEvent).getModifiers();
0N/A }
0N/A // Process the listeners last to first, notifying
0N/A // those that are interested in this event
0N/A for ( int i = listeners.length-2; i>=0; i-=2 ) {
0N/A if ( listeners[i]==ActionListener.class ) {
0N/A // Lazily create the event:
0N/A if ( e == null )
0N/A e = new ActionEvent(this,ActionEvent.ACTION_PERFORMED,
0N/A getActionCommand(),
0N/A mostRecentEventTime, modifiers);
0N/A ((ActionListener)listeners[i+1]).actionPerformed(e);
0N/A }
0N/A }
0N/A firingActionEvent = false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This protected method is implementation specific. Do not access directly
0N/A * or override.
0N/A */
0N/A protected void selectedItemChanged() {
0N/A if (selectedItemReminder != null ) {
0N/A fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,
0N/A selectedItemReminder,
0N/A ItemEvent.DESELECTED));
0N/A }
0N/A
0N/A // set the new selected item.
0N/A selectedItemReminder = dataModel.getSelectedItem();
0N/A
0N/A if (selectedItemReminder != null ) {
0N/A fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,
0N/A selectedItemReminder,
0N/A ItemEvent.SELECTED));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an array containing the selected item.
0N/A * This method is implemented for compatibility with
0N/A * <code>ItemSelectable</code>.
0N/A *
0N/A * @return an array of <code>Objects</code> containing one
0N/A * element -- the selected item
0N/A */
0N/A public Object[] getSelectedObjects() {
0N/A Object selectedObject = getSelectedItem();
0N/A if ( selectedObject == null )
0N/A return new Object[0];
0N/A else {
0N/A Object result[] = new Object[1];
0N/A result[0] = selectedObject;
0N/A return result;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method is public as an implementation side effect.
0N/A * do not call or override.
0N/A */
0N/A public void actionPerformed(ActionEvent e) {
0N/A Object newItem = getEditor().getItem();
0N/A setPopupVisible(false);
0N/A getModel().setSelectedItem(newItem);
0N/A String oldCommand = getActionCommand();
0N/A setActionCommand("comboBoxEdited");
0N/A fireActionEvent();
0N/A setActionCommand(oldCommand);
0N/A }
0N/A
0N/A /**
0N/A * This method is public as an implementation side effect.
0N/A * do not call or override.
0N/A */
0N/A public void contentsChanged(ListDataEvent e) {
0N/A Object oldSelection = selectedItemReminder;
0N/A Object newSelection = dataModel.getSelectedItem();
0N/A if (oldSelection == null || !oldSelection.equals(newSelection)) {
0N/A selectedItemChanged();
0N/A if (!selectingItem) {
0N/A fireActionEvent();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method is public as an implementation side effect.
0N/A * do not call or override.
0N/A */
0N/A public void intervalAdded(ListDataEvent e) {
0N/A if (selectedItemReminder != dataModel.getSelectedItem()) {
0N/A selectedItemChanged();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method is public as an implementation side effect.
0N/A * do not call or override.
0N/A */
0N/A public void intervalRemoved(ListDataEvent e) {
0N/A contentsChanged(e);
0N/A }
0N/A
0N/A /**
0N/A * Selects the list item that corresponds to the specified keyboard
0N/A * character and returns true, if there is an item corresponding
0N/A * to that character. Otherwise, returns false.
0N/A *
0N/A * @param keyChar a char, typically this is a keyboard key
0N/A * typed by the user
0N/A */
0N/A public boolean selectWithKeyChar(char keyChar) {
0N/A int index;
0N/A
0N/A if ( keySelectionManager == null )
0N/A keySelectionManager = createDefaultKeySelectionManager();
0N/A
0N/A index = keySelectionManager.selectionForKey(keyChar,getModel());
0N/A if ( index != -1 ) {
0N/A setSelectedIndex(index);
0N/A return true;
0N/A }
0N/A else
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Enables the combo box so that items can be selected. When the
0N/A * combo box is disabled, items cannot be selected and values
0N/A * cannot be typed into its field (if it is editable).
0N/A *
0N/A * @param b a boolean value, where true enables the component and
0N/A * false disables it
0N/A * @beaninfo
0N/A * bound: true
0N/A * preferred: true
0N/A * description: Whether the combo box is enabled.
0N/A */
0N/A public void setEnabled(boolean b) {
0N/A super.setEnabled(b);
0N/A firePropertyChange( "enabled", !isEnabled(), isEnabled() );
0N/A }
0N/A
0N/A /**
0N/A * Initializes the editor with the specified item.
0N/A *
0N/A * @param anEditor the <code>ComboBoxEditor</code> that displays
0N/A * the list item in the
0N/A * combo box field and allows it to be edited
0N/A * @param anItem the object to display and edit in the field
0N/A */
0N/A public void configureEditor(ComboBoxEditor anEditor, Object anItem) {
0N/A anEditor.setItem(anItem);
0N/A }
0N/A
0N/A /**
0N/A * Handles <code>KeyEvent</code>s, looking for the Tab key.
0N/A * If the Tab key is found, the popup window is closed.
0N/A *
0N/A * @param e the <code>KeyEvent</code> containing the keyboard
0N/A * key that was pressed
0N/A */
0N/A public void processKeyEvent(KeyEvent e) {
0N/A if ( e.getKeyCode() == KeyEvent.VK_TAB ) {
0N/A hidePopup();
0N/A }
0N/A super.processKeyEvent(e);
0N/A }
0N/A
0N/A /**
0N/A * Sets the object that translates a keyboard character into a list
0N/A * selection. Typically, the first selection with a matching first
0N/A * character becomes the selected item.
0N/A *
0N/A * @beaninfo
0N/A * expert: true
0N/A * description: The objects that changes the selection when a key is pressed.
0N/A */
0N/A public void setKeySelectionManager(KeySelectionManager aManager) {
0N/A keySelectionManager = aManager;
0N/A }
0N/A
0N/A /**
0N/A * Returns the list's key-selection manager.
0N/A *
0N/A * @return the <code>KeySelectionManager</code> currently in use
0N/A */
0N/A public KeySelectionManager getKeySelectionManager() {
0N/A return keySelectionManager;
0N/A }
0N/A
0N/A /* Accessing the model */
0N/A /**
0N/A * Returns the number of items in the list.
0N/A *
0N/A * @return an integer equal to the number of items in the list
0N/A */
0N/A public int getItemCount() {
0N/A return dataModel.getSize();
0N/A }
0N/A
0N/A /**
0N/A * Returns the list item at the specified index. If <code>index</code>
0N/A * is out of range (less than zero or greater than or equal to size)
0N/A * it will return <code>null</code>.
0N/A *
0N/A * @param index an integer indicating the list position, where the first
0N/A * item starts at zero
4202N/A * @return the item at that list position; or
0N/A * <code>null</code> if out of range
0N/A */
4202N/A public E getItemAt(int index) {
0N/A return dataModel.getElementAt(index);
0N/A }
0N/A
0N/A /**
0N/A * Returns an instance of the default key-selection manager.
0N/A *
0N/A * @return the <code>KeySelectionManager</code> currently used by the list
0N/A * @see #setKeySelectionManager
0N/A */
0N/A protected KeySelectionManager createDefaultKeySelectionManager() {
0N/A return new DefaultKeySelectionManager();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * The interface that defines a <code>KeySelectionManager</code>.
0N/A * To qualify as a <code>KeySelectionManager</code>,
0N/A * the class needs to implement the method
0N/A * that identifies the list index given a character and the
0N/A * combo box data model.
0N/A */
0N/A public interface KeySelectionManager {
0N/A /** Given <code>aKey</code> and the model, returns the row
0N/A * that should become selected. Return -1 if no match was
0N/A * found.
0N/A *
0N/A * @param aKey a char value, usually indicating a keyboard key that
0N/A * was pressed
0N/A * @param aModel a ComboBoxModel -- the component's data model, containing
0N/A * the list of selectable items
0N/A * @return an int equal to the selected row, where 0 is the
0N/A * first item and -1 is none.
0N/A */
0N/A int selectionForKey(char aKey,ComboBoxModel aModel);
0N/A }
0N/A
0N/A class DefaultKeySelectionManager implements KeySelectionManager, Serializable {
0N/A public int selectionForKey(char aKey,ComboBoxModel aModel) {
0N/A int i,c;
0N/A int currentSelection = -1;
0N/A Object selectedItem = aModel.getSelectedItem();
0N/A String v;
0N/A String pattern;
0N/A
0N/A if ( selectedItem != null ) {
0N/A for ( i=0,c=aModel.getSize();i<c;i++ ) {
0N/A if ( selectedItem == aModel.getElementAt(i) ) {
0N/A currentSelection = i;
0N/A break;
0N/A }
0N/A }
0N/A }
0N/A
0N/A pattern = ("" + aKey).toLowerCase();
0N/A aKey = pattern.charAt(0);
0N/A
0N/A for ( i = ++currentSelection, c = aModel.getSize() ; i < c ; i++ ) {
0N/A Object elem = aModel.getElementAt(i);
0N/A if (elem != null && elem.toString() != null) {
0N/A v = elem.toString().toLowerCase();
0N/A if ( v.length() > 0 && v.charAt(0) == aKey )
0N/A return i;
0N/A }
0N/A }
0N/A
0N/A for ( i = 0 ; i < currentSelection ; i ++ ) {
0N/A Object elem = aModel.getElementAt(i);
0N/A if (elem != null && elem.toString() != null) {
0N/A v = elem.toString().toLowerCase();
0N/A if ( v.length() > 0 && v.charAt(0) == aKey )
0N/A return i;
0N/A }
0N/A }
0N/A return -1;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * See <code>readObject</code> and <code>writeObject</code> in
0N/A * <code>JComponent</code> for more
0N/A * information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string representation of this <code>JComboBox</code>.
0N/A * This method is intended to be used only for debugging purposes,
0N/A * and the content and format of the returned string may vary between
0N/A * implementations. The returned string may be empty but may not
0N/A * be <code>null</code>.
0N/A *
0N/A * @return a string representation of this <code>JComboBox</code>
0N/A */
0N/A protected String paramString() {
0N/A String selectedItemReminderString = (selectedItemReminder != null ?
0N/A selectedItemReminder.toString() :
0N/A "");
0N/A String isEditableString = (isEditable ? "true" : "false");
0N/A String lightWeightPopupEnabledString = (lightWeightPopupEnabled ?
0N/A "true" : "false");
0N/A
0N/A return super.paramString() +
0N/A ",isEditable=" + isEditableString +
0N/A ",lightWeightPopupEnabled=" + lightWeightPopupEnabledString +
0N/A ",maximumRowCount=" + maximumRowCount +
0N/A ",selectedItemReminder=" + selectedItemReminderString;
0N/A }
0N/A
0N/A
0N/A///////////////////
0N/A// Accessibility support
0N/A///////////////////
0N/A
0N/A /**
0N/A * Gets the AccessibleContext associated with this JComboBox.
0N/A * For combo boxes, the AccessibleContext takes the form of an
0N/A * AccessibleJComboBox.
0N/A * A new AccessibleJComboBox instance is created if necessary.
0N/A *
0N/A * @return an AccessibleJComboBox that serves as the
0N/A * AccessibleContext of this JComboBox
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if ( accessibleContext == null ) {
0N/A accessibleContext = new AccessibleJComboBox();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * <code>JComboBox</code> class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to Combo Box user-interface elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJComboBox extends AccessibleJComponent
0N/A implements AccessibleAction, AccessibleSelection {
0N/A
0N/A
0N/A private JList popupList; // combo box popup list
0N/A private Accessible previousSelectedAccessible = null;
0N/A
0N/A /**
0N/A * Returns an AccessibleJComboBox instance
0N/A * @since 1.4
0N/A */
0N/A public AccessibleJComboBox() {
0N/A // set the combo box editor's accessible name and description
0N/A JComboBox.this.addPropertyChangeListener(new AccessibleJComboBoxPropertyChangeListener());
0N/A setEditorNameAndDescription();
0N/A
0N/A // Get the popup list
0N/A Accessible a = getUI().getAccessibleChild(JComboBox.this, 0);
0N/A if (a instanceof javax.swing.plaf.basic.ComboPopup) {
0N/A // Listen for changes to the popup menu selection.
0N/A popupList = ((javax.swing.plaf.basic.ComboPopup)a).getList();
0N/A popupList.addListSelectionListener(
0N/A new AccessibleJComboBoxListSelectionListener());
0N/A }
0N/A // Listen for popup menu show/hide events
0N/A JComboBox.this.addPopupMenuListener(
0N/A new AccessibleJComboBoxPopupMenuListener());
0N/A }
0N/A
0N/A /*
0N/A * JComboBox PropertyChangeListener
0N/A */
0N/A private class AccessibleJComboBoxPropertyChangeListener
0N/A implements PropertyChangeListener {
0N/A
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A if (e.getPropertyName() == "editor") {
0N/A // set the combo box editor's accessible name
0N/A // and description
0N/A setEditorNameAndDescription();
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Sets the combo box editor's accessible name and descripton
0N/A */
0N/A private void setEditorNameAndDescription() {
0N/A ComboBoxEditor editor = JComboBox.this.getEditor();
0N/A if (editor != null) {
0N/A Component comp = editor.getEditorComponent();
0N/A if (comp instanceof Accessible) {
625N/A AccessibleContext ac = comp.getAccessibleContext();
0N/A if (ac != null) { // may be null
0N/A ac.setAccessibleName(getAccessibleName());
0N/A ac.setAccessibleDescription(getAccessibleDescription());
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Listener for combo box popup menu
0N/A * TIGER - 4669379 4894434
0N/A */
0N/A private class AccessibleJComboBoxPopupMenuListener
0N/A implements PopupMenuListener {
0N/A
0N/A /**
0N/A * This method is called before the popup menu becomes visible
0N/A */
0N/A public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
0N/A // save the initial selection
0N/A if (popupList == null) {
0N/A return;
0N/A }
0N/A int selectedIndex = popupList.getSelectedIndex();
0N/A if (selectedIndex < 0) {
0N/A return;
0N/A }
0N/A previousSelectedAccessible =
0N/A popupList.getAccessibleContext().getAccessibleChild(selectedIndex);
0N/A }
0N/A
0N/A /**
0N/A * This method is called before the popup menu becomes invisible
0N/A * Note that a JPopupMenu can become invisible any time
0N/A */
0N/A public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
0N/A // ignore
0N/A }
0N/A
0N/A /**
0N/A * This method is called when the popup menu is canceled
0N/A */
0N/A public void popupMenuCanceled(PopupMenuEvent e) {
0N/A // ignore
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Handles changes to the popup list selection.
0N/A * TIGER - 4669379 4894434 4933143
0N/A */
0N/A private class AccessibleJComboBoxListSelectionListener
0N/A implements ListSelectionListener {
0N/A
0N/A public void valueChanged(ListSelectionEvent e) {
0N/A if (popupList == null) {
0N/A return;
0N/A }
0N/A
0N/A // Get the selected popup list item.
0N/A int selectedIndex = popupList.getSelectedIndex();
0N/A if (selectedIndex < 0) {
0N/A return;
0N/A }
0N/A Accessible selectedAccessible =
0N/A popupList.getAccessibleContext().getAccessibleChild(selectedIndex);
0N/A if (selectedAccessible == null) {
0N/A return;
0N/A }
0N/A
0N/A // Fire a FOCUSED lost PropertyChangeEvent for the
0N/A // previously selected list item.
625N/A PropertyChangeEvent pce;
0N/A
0N/A if (previousSelectedAccessible != null) {
0N/A pce = new PropertyChangeEvent(previousSelectedAccessible,
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.FOCUSED, null);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, pce);
0N/A }
0N/A // Fire a FOCUSED gained PropertyChangeEvent for the
0N/A // currently selected list item.
0N/A pce = new PropertyChangeEvent(selectedAccessible,
0N/A AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.FOCUSED);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, pce);
0N/A
0N/A // Fire the ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY event
0N/A // for the combo box.
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
0N/A previousSelectedAccessible, selectedAccessible);
0N/A
0N/A // Save the previous selection.
0N/A previousSelectedAccessible = selectedAccessible;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object. If all
0N/A * of the children of this object implement Accessible, than this
0N/A * method should return the number of children of this object.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A // Always delegate to the UI if it exists
0N/A if (ui != null) {
0N/A return ui.getAccessibleChildrenCount(JComboBox.this);
0N/A } else {
0N/A return super.getAccessibleChildrenCount();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the nth Accessible child of the object.
0N/A * The child at index zero represents the popup.
0N/A * If the combo box is editable, the child at index one
0N/A * represents the editor.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A // Always delegate to the UI if it exists
0N/A if (ui != null) {
0N/A return ui.getAccessibleChild(JComboBox.this, i);
0N/A } else {
0N/A return super.getAccessibleChild(i);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.COMBO_BOX;
0N/A }
0N/A
0N/A /**
0N/A * Gets the state set of this object. The AccessibleStateSet of
0N/A * an object is composed of a set of unique AccessibleStates.
0N/A * A change in the AccessibleStateSet of an object will cause a
0N/A * PropertyChangeEvent to be fired for the ACCESSIBLE_STATE_PROPERTY
0N/A * property.
0N/A *
0N/A * @return an instance of AccessibleStateSet containing the
0N/A * current state set of the object
0N/A * @see AccessibleStateSet
0N/A * @see AccessibleState
0N/A * @see #addPropertyChangeListener
0N/A *
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A // TIGER - 4489748
0N/A AccessibleStateSet ass = super.getAccessibleStateSet();
0N/A if (ass == null) {
0N/A ass = new AccessibleStateSet();
0N/A }
0N/A if (JComboBox.this.isPopupVisible()) {
0N/A ass.add(AccessibleState.EXPANDED);
0N/A } else {
0N/A ass.add(AccessibleState.COLLAPSED);
0N/A }
0N/A return ass;
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleAction associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleAction interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleAction getAccessibleAction() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Return a description of the specified action of the object.
0N/A *
0N/A * @param i zero-based index of the actions
0N/A */
0N/A public String getAccessibleActionDescription(int i) {
0N/A if (i == 0) {
0N/A return UIManager.getString("ComboBox.togglePopupText");
0N/A }
0N/A else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of Actions available in this object. The
0N/A * default behavior of a combo box is to have one action.
0N/A *
0N/A * @return 1, the number of Actions in this object
0N/A */
0N/A public int getAccessibleActionCount() {
0N/A return 1;
0N/A }
0N/A
0N/A /**
0N/A * Perform the specified Action on the object
0N/A *
0N/A * @param i zero-based index of actions
0N/A * @return true if the the action was performed; else false.
0N/A */
0N/A public boolean doAccessibleAction(int i) {
0N/A if (i == 0) {
0N/A setPopupVisible(!isPopupVisible());
0N/A return true;
0N/A }
0N/A else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Get the AccessibleSelection associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleSelection interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return this;
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of Accessible children currently selected.
0N/A * If no children are selected, the return value will be 0.
0N/A *
0N/A * @return the number of items currently selected.
0N/A * @since 1.3
0N/A */
0N/A public int getAccessibleSelectionCount() {
0N/A Object o = JComboBox.this.getSelectedItem();
0N/A if (o != null) {
0N/A return 1;
0N/A } else {
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns an Accessible representing the specified selected child
0N/A * in the popup. If there isn't a selection, or there are
0N/A * fewer children selected than the integer passed in, the return
0N/A * value will be null.
0N/A * <p>Note that the index represents the i-th selected child, which
0N/A * is different from the i-th child.
0N/A *
0N/A * @param i the zero-based index of selected children
0N/A * @return the i-th selected child
0N/A * @see #getAccessibleSelectionCount
0N/A * @since 1.3
0N/A */
0N/A public Accessible getAccessibleSelection(int i) {
0N/A // Get the popup
0N/A Accessible a =
0N/A JComboBox.this.getUI().getAccessibleChild(JComboBox.this, 0);
0N/A if (a != null &&
0N/A a instanceof javax.swing.plaf.basic.ComboPopup) {
0N/A
0N/A // get the popup list
0N/A JList list = ((javax.swing.plaf.basic.ComboPopup)a).getList();
0N/A
0N/A // return the i-th selection in the popup list
0N/A AccessibleContext ac = list.getAccessibleContext();
0N/A if (ac != null) {
0N/A AccessibleSelection as = ac.getAccessibleSelection();
0N/A if (as != null) {
0N/A return as.getAccessibleSelection(i);
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Determines if the current child of this object is selected.
0N/A *
0N/A * @return true if the current child of this object is selected;
0N/A * else false
0N/A * @param i the zero-based index of the child in this Accessible
0N/A * object.
0N/A * @see AccessibleContext#getAccessibleChild
0N/A * @since 1.3
0N/A */
0N/A public boolean isAccessibleChildSelected(int i) {
0N/A return JComboBox.this.getSelectedIndex() == i;
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified Accessible child of the object to the object's
0N/A * selection. If the object supports multiple selections,
0N/A * the specified child is added to any existing selection, otherwise
0N/A * it replaces any existing selection in the object. If the
0N/A * specified child is already selected, this method has no effect.
0N/A *
0N/A * @param i the zero-based index of the child
0N/A * @see AccessibleContext#getAccessibleChild
0N/A * @since 1.3
0N/A */
0N/A public void addAccessibleSelection(int i) {
0N/A // TIGER - 4856195
0N/A clearAccessibleSelection();
0N/A JComboBox.this.setSelectedIndex(i);
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified child of the object from the object's
0N/A * selection. If the specified item isn't currently selected, this
0N/A * method has no effect.
0N/A *
0N/A * @param i the zero-based index of the child
0N/A * @see AccessibleContext#getAccessibleChild
0N/A * @since 1.3
0N/A */
0N/A public void removeAccessibleSelection(int i) {
0N/A if (JComboBox.this.getSelectedIndex() == i) {
0N/A clearAccessibleSelection();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection in the object, so that no children in the
0N/A * object are selected.
0N/A * @since 1.3
0N/A */
0N/A public void clearAccessibleSelection() {
0N/A JComboBox.this.setSelectedIndex(-1);
0N/A }
0N/A
0N/A /**
0N/A * Causes every child of the object to be selected
0N/A * if the object supports multiple selections.
0N/A * @since 1.3
0N/A */
0N/A public void selectAllAccessibleSelection() {
0N/A // do nothing since multiple selection is not supported
0N/A }
0N/A
0N/A// public Accessible getAccessibleAt(Point p) {
0N/A// Accessible a = getAccessibleChild(1);
0N/A// if ( a != null ) {
0N/A// return a; // the editor
0N/A// }
0N/A// else {
0N/A// return getAccessibleChild(0); // the list
0N/A// }
0N/A// }
0N/A private EditorAccessibleContext editorAccessibleContext = null;
0N/A
0N/A private class AccessibleEditor implements Accessible {
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (editorAccessibleContext == null) {
0N/A Component c = JComboBox.this.getEditor().getEditorComponent();
0N/A if (c instanceof Accessible) {
0N/A editorAccessibleContext =
0N/A new EditorAccessibleContext((Accessible)c);
0N/A }
0N/A }
0N/A return editorAccessibleContext;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Wrapper class for the AccessibleContext implemented by the
0N/A * combo box editor. Delegates all method calls except
0N/A * getAccessibleIndexInParent to the editor. The
0N/A * getAccessibleIndexInParent method returns the selected
0N/A * index in the combo box.
0N/A */
0N/A private class EditorAccessibleContext extends AccessibleContext {
0N/A
0N/A private AccessibleContext ac;
0N/A
0N/A private EditorAccessibleContext() {
0N/A }
0N/A
0N/A /*
0N/A * @param a the AccessibleContext implemented by the
0N/A * combo box editor
0N/A */
0N/A EditorAccessibleContext(Accessible a) {
0N/A this.ac = a.getAccessibleContext();
0N/A }
0N/A
0N/A /**
0N/A * Gets the accessibleName property of this object. The accessibleName
0N/A * property of an object is a localized String that designates the purpose
0N/A * of the object. For example, the accessibleName property of a label
0N/A * or button might be the text of the label or button itself. In the
0N/A * case of an object that doesn't display its name, the accessibleName
0N/A * should still be set. For example, in the case of a text field used
0N/A * to enter the name of a city, the accessibleName for the en_US locale
0N/A * could be 'city.'
0N/A *
0N/A * @return the localized name of the object; null if this
0N/A * object does not have a name
0N/A *
0N/A * @see #setAccessibleName
0N/A */
0N/A public String getAccessibleName() {
0N/A return ac.getAccessibleName();
0N/A }
0N/A
0N/A /**
0N/A * Sets the localized accessible name of this object. Changing the
0N/A * name will cause a PropertyChangeEvent to be fired for the
0N/A * ACCESSIBLE_NAME_PROPERTY property.
0N/A *
0N/A * @param s the new localized name of the object.
0N/A *
0N/A * @see #getAccessibleName
0N/A * @see #addPropertyChangeListener
0N/A *
0N/A * @beaninfo
0N/A * preferred: true
0N/A * description: Sets the accessible name for the component.
0N/A */
0N/A public void setAccessibleName(String s) {
0N/A ac.setAccessibleName(s);
0N/A }
0N/A
0N/A /**
0N/A * Gets the accessibleDescription property of this object. The
0N/A * accessibleDescription property of this object is a short localized
0N/A * phrase describing the purpose of the object. For example, in the
0N/A * case of a 'Cancel' button, the accessibleDescription could be
0N/A * 'Ignore changes and close dialog box.'
0N/A *
0N/A * @return the localized description of the object; null if
0N/A * this object does not have a description
0N/A *
0N/A * @see #setAccessibleDescription
0N/A */
0N/A public String getAccessibleDescription() {
0N/A return ac.getAccessibleDescription();
0N/A }
0N/A
0N/A /**
0N/A * Sets the accessible description of this object. Changing the
0N/A * name will cause a PropertyChangeEvent to be fired for the
0N/A * ACCESSIBLE_DESCRIPTION_PROPERTY property.
0N/A *
0N/A * @param s the new localized description of the object
0N/A *
0N/A * @see #setAccessibleName
0N/A * @see #addPropertyChangeListener
0N/A *
0N/A * @beaninfo
0N/A * preferred: true
0N/A * description: Sets the accessible description for the component.
0N/A */
0N/A public void setAccessibleDescription(String s) {
0N/A ac.setAccessibleDescription(s);
0N/A }
0N/A
0N/A /**
0N/A * Gets the role of this object. The role of the object is the generic
0N/A * purpose or use of the class of this object. For example, the role
0N/A * of a push button is AccessibleRole.PUSH_BUTTON. The roles in
0N/A * AccessibleRole are provided so component developers can pick from
0N/A * a set of predefined roles. This enables assistive technologies to
0N/A * provide a consistent interface to various tweaked subclasses of
0N/A * components (e.g., use AccessibleRole.PUSH_BUTTON for all components
0N/A * that act like a push button) as well as distinguish between sublasses
0N/A * that behave differently (e.g., AccessibleRole.CHECK_BOX for check boxes
0N/A * and AccessibleRole.RADIO_BUTTON for radio buttons).
0N/A * <p>Note that the AccessibleRole class is also extensible, so
0N/A * custom component developers can define their own AccessibleRole's
0N/A * if the set of predefined roles is inadequate.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return ac.getAccessibleRole();
0N/A }
0N/A
0N/A /**
0N/A * Gets the state set of this object. The AccessibleStateSet of an object
0N/A * is composed of a set of unique AccessibleStates. A change in the
0N/A * AccessibleStateSet of an object will cause a PropertyChangeEvent to
0N/A * be fired for the ACCESSIBLE_STATE_PROPERTY property.
0N/A *
0N/A * @return an instance of AccessibleStateSet containing the
0N/A * current state set of the object
0N/A * @see AccessibleStateSet
0N/A * @see AccessibleState
0N/A * @see #addPropertyChangeListener
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A return ac.getAccessibleStateSet();
0N/A }
0N/A
0N/A /**
0N/A * Gets the Accessible parent of this object.
0N/A *
0N/A * @return the Accessible parent of this object; null if this
0N/A * object does not have an Accessible parent
0N/A */
0N/A public Accessible getAccessibleParent() {
0N/A return ac.getAccessibleParent();
0N/A }
0N/A
0N/A /**
0N/A * Sets the Accessible parent of this object. This is meant to be used
0N/A * only in the situations where the actual component's parent should
0N/A * not be treated as the component's accessible parent and is a method
0N/A * that should only be called by the parent of the accessible child.
0N/A *
0N/A * @param a - Accessible to be set as the parent
0N/A */
0N/A public void setAccessibleParent(Accessible a) {
0N/A ac.setAccessibleParent(a);
0N/A }
0N/A
0N/A /**
0N/A * Gets the 0-based index of this object in its accessible parent.
0N/A *
0N/A * @return the 0-based index of this object in its parent; -1 if this
0N/A * object does not have an accessible parent.
0N/A *
0N/A * @see #getAccessibleParent
0N/A * @see #getAccessibleChildrenCount
0N/A * @see #getAccessibleChild
0N/A */
0N/A public int getAccessibleIndexInParent() {
0N/A return JComboBox.this.getSelectedIndex();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of accessible children of the object.
0N/A *
0N/A * @return the number of accessible children of the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A return ac.getAccessibleChildrenCount();
0N/A }
0N/A
0N/A /**
0N/A * Returns the specified Accessible child of the object. The Accessible
0N/A * children of an Accessible object are zero-based, so the first child
0N/A * of an Accessible child is at index 0, the second child is at index 1,
0N/A * and so on.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the Accessible child of the object
0N/A * @see #getAccessibleChildrenCount
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A return ac.getAccessibleChild(i);
0N/A }
0N/A
0N/A /**
0N/A * Gets the locale of the component. If the component does not have a
0N/A * locale, then the locale of its parent is returned.
0N/A *
0N/A * @return this component's locale. If this component does not have
0N/A * a locale, the locale of its parent is returned.
0N/A *
0N/A * @exception IllegalComponentStateException
0N/A * If the Component does not have its own locale and has not yet been
0N/A * added to a containment hierarchy such that the locale can be
0N/A * determined from the containing parent.
0N/A */
0N/A public Locale getLocale() throws IllegalComponentStateException {
0N/A return ac.getLocale();
0N/A }
0N/A
0N/A /**
0N/A * Adds a PropertyChangeListener to the listener list.
0N/A * The listener is registered for all Accessible properties and will
0N/A * be called when those properties change.
0N/A *
0N/A * @see #ACCESSIBLE_NAME_PROPERTY
0N/A * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
0N/A * @see #ACCESSIBLE_STATE_PROPERTY
0N/A * @see #ACCESSIBLE_VALUE_PROPERTY
0N/A * @see #ACCESSIBLE_SELECTION_PROPERTY
0N/A * @see #ACCESSIBLE_TEXT_PROPERTY
0N/A * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
0N/A *
0N/A * @param listener The PropertyChangeListener to be added
0N/A */
0N/A public void addPropertyChangeListener(PropertyChangeListener listener) {
0N/A ac.addPropertyChangeListener(listener);
0N/A }
0N/A
0N/A /**
0N/A * Removes a PropertyChangeListener from the listener list.
0N/A * This removes a PropertyChangeListener that was registered
0N/A * for all properties.
0N/A *
0N/A * @param listener The PropertyChangeListener to be removed
0N/A */
0N/A public void removePropertyChangeListener(PropertyChangeListener listener) {
0N/A ac.removePropertyChangeListener(listener);
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleAction associated with this object that supports
0N/A * one or more actions.
0N/A *
0N/A * @return AccessibleAction if supported by object; else return null
0N/A * @see AccessibleAction
0N/A */
0N/A public AccessibleAction getAccessibleAction() {
0N/A return ac.getAccessibleAction();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleComponent associated with this object that has a
0N/A * graphical representation.
0N/A *
0N/A * @return AccessibleComponent if supported by object; else return null
0N/A * @see AccessibleComponent
0N/A */
0N/A public AccessibleComponent getAccessibleComponent() {
0N/A return ac.getAccessibleComponent();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleSelection associated with this object which allows its
0N/A * Accessible children to be selected.
0N/A *
0N/A * @return AccessibleSelection if supported by object; else return null
0N/A * @see AccessibleSelection
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return ac.getAccessibleSelection();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleText associated with this object presenting
0N/A * text on the display.
0N/A *
0N/A * @return AccessibleText if supported by object; else return null
0N/A * @see AccessibleText
0N/A */
0N/A public AccessibleText getAccessibleText() {
0N/A return ac.getAccessibleText();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleEditableText associated with this object
0N/A * presenting editable text on the display.
0N/A *
0N/A * @return AccessibleEditableText if supported by object; else return null
0N/A * @see AccessibleEditableText
0N/A */
0N/A public AccessibleEditableText getAccessibleEditableText() {
0N/A return ac.getAccessibleEditableText();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleValue associated with this object that supports a
0N/A * Numerical value.
0N/A *
0N/A * @return AccessibleValue if supported by object; else return null
0N/A * @see AccessibleValue
0N/A */
0N/A public AccessibleValue getAccessibleValue() {
0N/A return ac.getAccessibleValue();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleIcons associated with an object that has
0N/A * one or more associated icons
0N/A *
0N/A * @return an array of AccessibleIcon if supported by object;
0N/A * otherwise return null
0N/A * @see AccessibleIcon
0N/A */
0N/A public AccessibleIcon [] getAccessibleIcon() {
0N/A return ac.getAccessibleIcon();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleRelationSet associated with an object
0N/A *
0N/A * @return an AccessibleRelationSet if supported by object;
0N/A * otherwise return null
0N/A * @see AccessibleRelationSet
0N/A */
0N/A public AccessibleRelationSet getAccessibleRelationSet() {
0N/A return ac.getAccessibleRelationSet();
0N/A }
0N/A
0N/A /**
0N/A * Gets the AccessibleTable associated with an object
0N/A *
0N/A * @return an AccessibleTable if supported by object;
0N/A * otherwise return null
0N/A * @see AccessibleTable
0N/A */
0N/A public AccessibleTable getAccessibleTable() {
0N/A return ac.getAccessibleTable();
0N/A }
0N/A
0N/A /**
0N/A * Support for reporting bound property changes. If oldValue and
0N/A * newValue are not equal and the PropertyChangeEvent listener list
0N/A * is not empty, then fire a PropertyChange event to each listener.
0N/A * In general, this is for use by the Accessible objects themselves
0N/A * and should not be called by an application program.
0N/A * @param propertyName The programmatic name of the property that
0N/A * was changed.
0N/A * @param oldValue The old value of the property.
0N/A * @param newValue The new value of the property.
0N/A * @see java.beans.PropertyChangeSupport
0N/A * @see #addPropertyChangeListener
0N/A * @see #removePropertyChangeListener
0N/A * @see #ACCESSIBLE_NAME_PROPERTY
0N/A * @see #ACCESSIBLE_DESCRIPTION_PROPERTY
0N/A * @see #ACCESSIBLE_STATE_PROPERTY
0N/A * @see #ACCESSIBLE_VALUE_PROPERTY
0N/A * @see #ACCESSIBLE_SELECTION_PROPERTY
0N/A * @see #ACCESSIBLE_TEXT_PROPERTY
0N/A * @see #ACCESSIBLE_VISIBLE_DATA_PROPERTY
0N/A */
0N/A public void firePropertyChange(String propertyName,
0N/A Object oldValue,
0N/A Object newValue) {
0N/A ac.firePropertyChange(propertyName, oldValue, newValue);
0N/A }
0N/A }
0N/A
0N/A } // innerclass AccessibleJComboBox
0N/A}