1 /* 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing; 26 27 import java.util.*; 28 29 import java.io.Serializable; 30 31 /** 32 * The default model for combo boxes. 33 * 34 * @param <E> the type of the elements of this model 35 * 36 * @author Arnaud Weber 37 * @author Tom Santos 38 */ 39 40 public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable { 41 Vector<E> objects; 42 Object selectedObject; 43 44 /** 45 * Constructs an empty DefaultComboBoxModel object. 46 */ 47 public DefaultComboBoxModel() { 48 objects = new Vector<E>(); 49 } 50 51 /** 52 * Constructs a DefaultComboBoxModel object initialized with 53 * an array of objects. 54 * 55 * @param items an array of Object objects 56 */ 57 public DefaultComboBoxModel(final E items[]) { 58 objects = new Vector<E>(items.length); 59 60 int i,c; 61 for ( i=0,c=items.length;i<c;i++ ) 62 objects.addElement(items[i]); 63 64 if ( getSize() > 0 ) { 65 selectedObject = getElementAt( 0 ); 66 } 67 } 68 69 /** 70 * Constructs a DefaultComboBoxModel object initialized with 71 * a vector. 72 * 73 * @param v a Vector object ... 74 */ 75 public DefaultComboBoxModel(Vector<E> v) { 76 objects = v; 77 78 if ( getSize() > 0 ) { 79 selectedObject = getElementAt( 0 ); 80 } 81 } 82 83 // implements javax.swing.ComboBoxModel 84 /** 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 */ 89 public void setSelectedItem(Object anObject) { 90 if ((selectedObject != null && !selectedObject.equals( anObject )) || 91 selectedObject == null && anObject != null) { 92 selectedObject = anObject; 93 fireContentsChanged(this, -1, -1); 94 } 95 } 96 97 // implements javax.swing.ComboBoxModel 98 public Object getSelectedItem() { 99 return selectedObject; 100 } 101 102 // implements javax.swing.ListModel 103 public int getSize() { 104 return objects.size(); 105 } 106 107 // implements javax.swing.ListModel 108 public E getElementAt(int index) { 109 if ( index >= 0 && index < objects.size() ) 110 return objects.elementAt(index); 111 else 112 return null; 113 } 114 115 /** 116 * Returns the index-position of the specified object in the list. 117 * 118 * @param anObject the object to return the index of 119 * @return an int representing the index position, where 0 is 120 * the first position 121 */ 122 public int getIndexOf(Object anObject) { 123 return objects.indexOf(anObject); 124 } 125 126 // implements javax.swing.MutableComboBoxModel 127 public void addElement(E anObject) { 128 objects.addElement(anObject); 129 fireIntervalAdded(this,objects.size()-1, objects.size()-1); 130 if ( objects.size() == 1 && selectedObject == null && anObject != null ) { 131 setSelectedItem( anObject ); 132 } 133 } 134 135 // implements javax.swing.MutableComboBoxModel 136 public void insertElementAt(E anObject,int index) { 137 objects.insertElementAt(anObject,index); 138 fireIntervalAdded(this, index, index); 139 } 140 141 // implements javax.swing.MutableComboBoxModel 142 public void removeElementAt(int index) { 143 if ( getElementAt( index ) == selectedObject ) { 144 if ( index == 0 ) { 145 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) ); 146 } 147 else { 148 setSelectedItem( getElementAt( index - 1 ) ); 149 } 150 } 151 152 objects.removeElementAt(index); 153 154 fireIntervalRemoved(this, index, index); 155 } 156 157 // implements javax.swing.MutableComboBoxModel 158 public void removeElement(Object anObject) { 159 int index = objects.indexOf(anObject); 160 if ( index != -1 ) { 161 removeElementAt(index); 162 } 163 } 164 165 /** 166 * Empties the list. 167 */ 168 public void removeAllElements() { 169 if ( objects.size() > 0 ) { 170 int firstIndex = 0; 171 int lastIndex = objects.size() - 1; 172 objects.removeAllElements(); 173 selectedObject = null; 174 fireIntervalRemoved(this, firstIndex, lastIndex); 175 } else { 176 selectedObject = null; 177 } 178 } 179 }