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 * 184 * @param c the collection which contains the elements to add 185 * @throws NullPointerException if {@code c} is null 186 */ 187 public void addAll(Collection<? extends 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 * Adds all of the elements present in the collection, starting 200 * from the specified index. 201 * 202 * @param index index at which to insert the first element from the 203 * specified collection 204 * @param c the collection which contains the elements to add 205 * @throws ArrayIndexOutOfBoundsException if {@code index} does not 206 * fall within the range of number of elements currently held 207 * @throws NullPointerException if {@code c} is null 208 */ 209 public void addAll(int index, Collection<? extends E> c) { 210 if (index < 0 || index > getSize()) { 211 throw new ArrayIndexOutOfBoundsException("index out of range: " + 212 index); 213 } 214 215 if (c.isEmpty()) { 216 return; 217 } 218 219 objects.addAll(index, c); 220 fireIntervalAdded(this, index, index + c.size() - 1); 221 } 222 } |