Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java
41153 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package javax.swing;2526import java.util.Collection;27import java.util.Vector;28import java.io.Serializable;2930/**31* The default model for combo boxes.32*33* @param <E> the type of the elements of this model34*35* @author Arnaud Weber36* @author Tom Santos37* @since 1.238*/39@SuppressWarnings("serial") // Superclass is not serializable across versions40public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {41Vector<E> objects;42Object selectedObject;4344/**45* Constructs an empty DefaultComboBoxModel object.46*/47public DefaultComboBoxModel() {48objects = new Vector<E>();49}5051/**52* Constructs a DefaultComboBoxModel object initialized with53* an array of objects.54*55* @param items an array of Object objects56*/57public DefaultComboBoxModel(final E[] items) {58objects = new Vector<E>(items.length);5960int i,c;61for ( i=0,c=items.length;i<c;i++ )62objects.addElement(items[i]);6364if ( getSize() > 0 ) {65selectedObject = getElementAt( 0 );66}67}6869/**70* Constructs a DefaultComboBoxModel object initialized with71* a vector.72*73* @param v a Vector object ...74*/75public DefaultComboBoxModel(Vector<E> v) {76objects = v;7778if ( getSize() > 0 ) {79selectedObject = getElementAt( 0 );80}81}8283// implements javax.swing.ComboBoxModel84/**85* Set the value of the selected item. The selected item may be null.86*87* @param anObject The combo box value or null for no selection.88*/89public void setSelectedItem(Object anObject) {90if ((selectedObject != null && !selectedObject.equals( anObject )) ||91selectedObject == null && anObject != null) {92selectedObject = anObject;93fireContentsChanged(this, -1, -1);94}95}9697// implements javax.swing.ComboBoxModel98public Object getSelectedItem() {99return selectedObject;100}101102// implements javax.swing.ListModel103public int getSize() {104return objects.size();105}106107// implements javax.swing.ListModel108public E getElementAt(int index) {109if ( index >= 0 && index < objects.size() )110return objects.elementAt(index);111else112return null;113}114115/**116* Returns the index-position of the specified object in the list.117*118* @param anObject the object to return the index of119* @return an int representing the index position, where 0 is120* the first position121*/122public int getIndexOf(Object anObject) {123return objects.indexOf(anObject);124}125126// implements javax.swing.MutableComboBoxModel127public void addElement(E anObject) {128objects.addElement(anObject);129fireIntervalAdded(this,objects.size()-1, objects.size()-1);130if ( objects.size() == 1 && selectedObject == null && anObject != null ) {131setSelectedItem( anObject );132}133}134135// implements javax.swing.MutableComboBoxModel136public void insertElementAt(E anObject,int index) {137objects.insertElementAt(anObject,index);138fireIntervalAdded(this, index, index);139}140141// implements javax.swing.MutableComboBoxModel142public void removeElementAt(int index) {143if ( getElementAt( index ) == selectedObject ) {144if ( index == 0 ) {145setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );146}147else {148setSelectedItem( getElementAt( index - 1 ) );149}150}151152objects.removeElementAt(index);153154fireIntervalRemoved(this, index, index);155}156157// implements javax.swing.MutableComboBoxModel158public void removeElement(Object anObject) {159int index = objects.indexOf(anObject);160if ( index != -1 ) {161removeElementAt(index);162}163}164165/**166* Empties the list.167*/168public void removeAllElements() {169if ( objects.size() > 0 ) {170int firstIndex = 0;171int lastIndex = objects.size() - 1;172objects.removeAllElements();173selectedObject = null;174fireIntervalRemoved(this, firstIndex, lastIndex);175} else {176selectedObject = null;177}178}179180/**181* Adds all of the elements present in the collection.182*183* @param c the collection which contains the elements to add184* @throws NullPointerException if {@code c} is null185*/186public void addAll(Collection<? extends E> c) {187if (c.isEmpty()) {188return;189}190191int startIndex = getSize();192193objects.addAll(c);194fireIntervalAdded(this, startIndex, getSize() - 1);195}196197/**198* Adds all of the elements present in the collection, starting199* from the specified index.200*201* @param index index at which to insert the first element from the202* specified collection203* @param c the collection which contains the elements to add204* @throws ArrayIndexOutOfBoundsException if {@code index} does not205* fall within the range of number of elements currently held206* @throws NullPointerException if {@code c} is null207*/208public void addAll(int index, Collection<? extends E> c) {209if (index < 0 || index > getSize()) {210throw new ArrayIndexOutOfBoundsException("index out of range: " +211index);212}213214if (c.isEmpty()) {215return;216}217218objects.addAll(index, c);219fireIntervalAdded(this, index, index + c.size() - 1);220}221}222223224