1 /* 2 * Copyright (c) 1998, 2014, 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 * @since 1.2 39 */ 40 @SuppressWarnings("serial") // Superclass is not serializable across versions 41 public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable { 42 Vector<E> objects; 43 Object selectedObject; 44 45 /** 46 * Constructs an empty DefaultComboBoxModel object. 47 */ 48 public DefaultComboBoxModel() { 159 public void removeElement(Object anObject) { 160 int index = objects.indexOf(anObject); 161 if ( index != -1 ) { 162 removeElementAt(index); 163 } 164 } 165 166 /** 167 * Empties the list. 168 */ 169 public void removeAllElements() { 170 if ( objects.size() > 0 ) { 171 int firstIndex = 0; 172 int lastIndex = objects.size() - 1; 173 objects.removeAllElements(); 174 selectedObject = null; 175 fireIntervalRemoved(this, firstIndex, lastIndex); 176 } else { 177 selectedObject = null; 178 } 179 } 180 } | 1 /* 2 * Copyright (c) 1998, 2018, 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.Collection; 28 import java.util.Vector; 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 * @since 1.2 39 */ 40 @SuppressWarnings("serial") // Superclass is not serializable across versions 41 public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable { 42 Vector<E> objects; 43 Object selectedObject; 44 45 /** 46 * Constructs an empty DefaultComboBoxModel object. 47 */ 48 public DefaultComboBoxModel() { 159 public void removeElement(Object anObject) { 160 int index = objects.indexOf(anObject); 161 if ( index != -1 ) { 162 removeElementAt(index); 163 } 164 } 165 166 /** 167 * Empties the list. 168 */ 169 public void removeAllElements() { 170 if ( objects.size() > 0 ) { 171 int firstIndex = 0; 172 int lastIndex = objects.size() - 1; 173 objects.removeAllElements(); 174 selectedObject = null; 175 fireIntervalRemoved(this, firstIndex, lastIndex); 176 } else { 177 selectedObject = null; 178 } 179 } 180 181 /** 182 * Adds all of the elements present in the collection. 183 * <p> 184 * 185 * @param c the collection which contains the elements to add. 186 */ 187 public void addElements(Collection<E> c) { 188 if (c.isEmpty()) { 189 return; 190 } 191 192 int startIndex = getSize(); 193 194 objects.addAll(c); 195 fireIntervalAdded(this, startIndex, getSize() - 1); 196 } 197 198 /** 199 * Add all of the elements present in the collection, starting 200 * from the index specified. 201 * <p> 202 * Throws an <code>IllegalArgumentException</code> 203 * if the index was invalid. 204 * 205 * @param index the index from which to start adding elements from. 206 * @param c the collection which contains the elements to add 207 * @exception IllegalArgumentException if <code>c</code> doesn't fall 208 * in the range of the list. 209 */ 210 public void addElementsAt(int index, Collection<E> c) { 211 if (index < 0 || index > getSize()) { 212 throw new IllegalArgumentException( 213 "index must be >=0 and <= number of elements in the combobox."); 214 } 215 216 if (c.isEmpty()) { 217 return; 218 } 219 220 objects.addAll(index, c); 221 fireIntervalAdded(this, index, index + c.size() - 1); 222 } 223 } |