Path: blob/master/src/java.desktop/share/classes/javax/swing/DefaultListModel.java
41153 views
/*1* Copyright (c) 1997, 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*/2425package javax.swing;2627import java.util.Vector;28import java.util.Collection;29import java.util.Enumeration;303132/**33* This class loosely implements the {@code java.util.Vector}34* API, in that it implements the 1.1.x version of35* {@code java.util.Vector}, has no collection class support,36* and notifies the {@code ListDataListener}s when changes occur.37* Presently it delegates to a {@code Vector},38* in a future release it will be a real Collection implementation.39* <p>40* <strong>Warning:</strong>41* Serialized objects of this class will not be compatible with42* future Swing releases. The current serialization support is43* appropriate for short term storage or RMI between applications running44* the same version of Swing. As of 1.4, support for long term storage45* of all JavaBeans46* has been added to the {@code java.beans} package.47* Please see {@link java.beans.XMLEncoder}.48*49* @param <E> the type of the elements of this model50*51* @author Hans Muller52* @since 1.253*/54@SuppressWarnings("serial") // Same-version serialization only55public class DefaultListModel<E> extends AbstractListModel<E>56{57private Vector<E> delegate = new Vector<E>();5859/**60* Constructs a {@code DefaultListModel}.61*/62public DefaultListModel() {}6364/**65* Returns the number of components in this list.66* <p>67* This method is identical to {@code size}, which implements the68* {@code List} interface defined in the 1.2 Collections framework.69* This method exists in conjunction with {@code setSize} so that70* {@code size} is identifiable as a JavaBean property.71*72* @return the number of components in this list73* @see #size()74*/75public int getSize() {76return delegate.size();77}7879/**80* Returns the component at the specified index.81* <blockquote>82* <b>Note:</b> Although this method is not deprecated, the preferred83* method to use is {@code get(int)}, which implements the {@code List}84* interface defined in the 1.2 Collections framework.85* </blockquote>86* @param index an index into this list87* @return the component at the specified index88* @throws ArrayIndexOutOfBoundsException if the {@code index}89* is negative or greater than the current size of this90* list91* @see #get(int)92*/93public E getElementAt(int index) {94return delegate.elementAt(index);95}9697/**98* Copies the components of this list into the specified array.99* The array must be big enough to hold all the objects in this list,100* else an {@code IndexOutOfBoundsException} is thrown.101*102* @param anArray the array into which the components get copied103* @see Vector#copyInto(Object[])104*/105public void copyInto(Object[] anArray) {106delegate.copyInto(anArray);107}108109/**110* Trims the capacity of this list to be the list's current size.111*112* @see Vector#trimToSize()113*/114public void trimToSize() {115delegate.trimToSize();116}117118/**119* Increases the capacity of this list, if necessary, to ensure120* that it can hold at least the number of components specified by121* the minimum capacity argument.122*123* @param minCapacity the desired minimum capacity124* @see Vector#ensureCapacity(int)125*/126public void ensureCapacity(int minCapacity) {127delegate.ensureCapacity(minCapacity);128}129130/**131* Sets the size of this list.132*133* @param newSize the new size of this list134* @see Vector#setSize(int)135*/136public void setSize(int newSize) {137int oldSize = delegate.size();138delegate.setSize(newSize);139if (oldSize > newSize) {140fireIntervalRemoved(this, newSize, oldSize-1);141}142else if (oldSize < newSize) {143fireIntervalAdded(this, oldSize, newSize-1);144}145}146147/**148* Returns the current capacity of this list.149*150* @return the current capacity151* @see Vector#capacity()152*/153public int capacity() {154return delegate.capacity();155}156157/**158* Returns the number of components in this list.159*160* @return the number of components in this list161* @see Vector#size()162*/163public int size() {164return delegate.size();165}166167/**168* Tests whether this list has any components.169*170* @return {@code true} if and only if this list has171* no components, that is, its size is zero;172* {@code false} otherwise173* @see Vector#isEmpty()174*/175public boolean isEmpty() {176return delegate.isEmpty();177}178179/**180* Returns an enumeration of the components of this list.181*182* @return an enumeration of the components of this list183* @see Vector#elements()184*/185public Enumeration<E> elements() {186return delegate.elements();187}188189/**190* Tests whether the specified object is a component in this list.191*192* @param elem an object193* @return {@code true} if the specified object194* is the same as a component in this list195* @see Vector#contains(Object)196*/197public boolean contains(Object elem) {198return delegate.contains(elem);199}200201/**202* Searches for the first occurrence of {@code elem}.203*204* @param elem an object205* @return the index of the first occurrence of the argument in this206* list; returns {@code -1} if the object is not found207* @see Vector#indexOf(Object)208*/209public int indexOf(Object elem) {210return delegate.indexOf(elem);211}212213/**214* Searches for the first occurrence of {@code elem}, beginning215* the search at {@code index}.216*217* @param elem the desired component218* @param index the index from which to begin searching219* @return the index where the first occurrence of {@code elem}220* is found after {@code index}; returns {@code -1}221* if the {@code elem} is not found in the list222* @see Vector#indexOf(Object,int)223*/224public int indexOf(Object elem, int index) {225return delegate.indexOf(elem, index);226}227228/**229* Returns the index of the last occurrence of {@code elem}.230*231* @param elem the desired component232* @return the index of the last occurrence of {@code elem}233* in the list; returns {@code elem} if the object is not found234* @see Vector#lastIndexOf(Object)235*/236public int lastIndexOf(Object elem) {237return delegate.lastIndexOf(elem);238}239240/**241* Searches backwards for {@code elem}, starting from the242* specified index, and returns an index to it.243*244* @param elem the desired component245* @param index the index to start searching from246* @return the index of the last occurrence of the {@code elem}247* in this list at position less than {@code index};248* returns {@code -1} if the object is not found249* @see Vector#lastIndexOf(Object,int)250*/251public int lastIndexOf(Object elem, int index) {252return delegate.lastIndexOf(elem, index);253}254255/**256* Returns the component at the specified index.257* <blockquote>258* <b>Note:</b> Although this method is not deprecated, the preferred259* method to use is {@code get(int)}, which implements the260* {@code List} interface defined in the 1.2 Collections framework.261* </blockquote>262*263* @param index an index into this list264* @return the component at the specified index265* @throws ArrayIndexOutOfBoundsException if the index266* is negative or not less than the size of the list267* @see #get(int)268* @see Vector#elementAt(int)269*/270public E elementAt(int index) {271return delegate.elementAt(index);272}273274/**275* Returns the first component of this list.276* @return the first component of this list277* @see Vector#firstElement()278* @throws java.util.NoSuchElementException if this279* vector has no components280*/281public E firstElement() {282return delegate.firstElement();283}284285/**286* Returns the last component of the list.287*288* @return the last component of the list289* @see Vector#lastElement()290* @throws java.util.NoSuchElementException if this vector291* has no components292*/293public E lastElement() {294return delegate.lastElement();295}296297/**298* Sets the component at the specified {@code index} of this299* list to be the specified element. The previous component at that300* position is discarded.301* <blockquote>302* <b>Note:</b> Although this method is not deprecated, the preferred303* method to use is {@code set(int,Object)}, which implements the304* {@code List} interface defined in the 1.2 Collections framework.305* </blockquote>306*307* @param element what the component is to be set to308* @param index the specified index309* @throws ArrayIndexOutOfBoundsException if the index is invalid310* @see #set(int,Object)311* @see Vector#setElementAt(Object,int)312*/313public void setElementAt(E element, int index) {314delegate.setElementAt(element, index);315fireContentsChanged(this, index, index);316}317318/**319* Deletes the component at the specified index.320* <blockquote>321* <b>Note:</b> Although this method is not deprecated, the preferred322* method to use is {@code remove(int)}, which implements the323* {@code List} interface defined in the 1.2 Collections framework.324* </blockquote>325*326* @param index the index of the object to remove327* @see #remove(int)328* @see Vector#removeElementAt(int)329* @throws ArrayIndexOutOfBoundsException if the index is invalid330*/331public void removeElementAt(int index) {332delegate.removeElementAt(index);333fireIntervalRemoved(this, index, index);334}335336/**337* Inserts the specified element as a component in this list at the338* specified <code>index</code>.339* <blockquote>340* <b>Note:</b> Although this method is not deprecated, the preferred341* method to use is {@code add(int,Object)}, which implements the342* {@code List} interface defined in the 1.2 Collections framework.343* </blockquote>344*345* @param element the component to insert346* @param index where to insert the new component347* @exception ArrayIndexOutOfBoundsException if the index was invalid348* @see #add(int,Object)349* @see Vector#insertElementAt(Object,int)350*/351public void insertElementAt(E element, int index) {352delegate.insertElementAt(element, index);353fireIntervalAdded(this, index, index);354}355356/**357* Adds the specified component to the end of this list.358*359* @param element the component to be added360* @see Vector#addElement(Object)361*/362public void addElement(E element) {363int index = delegate.size();364delegate.addElement(element);365fireIntervalAdded(this, index, index);366}367368/**369* Removes the first (lowest-indexed) occurrence of the argument370* from this list.371*372* @param obj the component to be removed373* @return {@code true} if the argument was a component of this374* list; {@code false} otherwise375* @see Vector#removeElement(Object)376*/377public boolean removeElement(Object obj) {378int index = indexOf(obj);379boolean rv = delegate.removeElement(obj);380if (index >= 0) {381fireIntervalRemoved(this, index, index);382}383return rv;384}385386387/**388* Removes all components from this list and sets its size to zero.389* <blockquote>390* <b>Note:</b> Although this method is not deprecated, the preferred391* method to use is {@code clear}, which implements the392* {@code List} interface defined in the 1.2 Collections framework.393* </blockquote>394*395* @see #clear()396* @see Vector#removeAllElements()397*/398public void removeAllElements() {399int index1 = delegate.size()-1;400delegate.removeAllElements();401if (index1 >= 0) {402fireIntervalRemoved(this, 0, index1);403}404}405406407/**408* Returns a string that displays and identifies this409* object's properties.410*411* @return a String representation of this object412*/413public String toString() {414return delegate.toString();415}416417418/* The remaining methods are included for compatibility with the419* Java 2 platform Vector class.420*/421422/**423* Returns an array containing all of the elements in this list in the424* correct order.425*426* @return an array containing the elements of the list427* @see Vector#toArray()428*/429public Object[] toArray() {430Object[] rv = new Object[delegate.size()];431delegate.copyInto(rv);432return rv;433}434435/**436* Returns the element at the specified position in this list.437*438* @param index index of element to return439* @return the element at the specified position in this list440* @throws ArrayIndexOutOfBoundsException if the index is out of range441* ({@code index < 0 || index >= size()})442*/443public E get(int index) {444return delegate.elementAt(index);445}446447/**448* Replaces the element at the specified position in this list with the449* specified element.450*451* @param index index of element to replace452* @param element element to be stored at the specified position453* @return the element previously at the specified position454* @throws ArrayIndexOutOfBoundsException if the index is out of range455* ({@code index < 0 || index >= size()})456*/457public E set(int index, E element) {458E rv = delegate.elementAt(index);459delegate.setElementAt(element, index);460fireContentsChanged(this, index, index);461return rv;462}463464/**465* Inserts the specified element at the specified position in this list.466*467* @param index index at which the specified element is to be inserted468* @param element element to be inserted469* @throws ArrayIndexOutOfBoundsException if the index is out of range470* ({@code index < 0 || index > size()})471*/472public void add(int index, E element) {473delegate.insertElementAt(element, index);474fireIntervalAdded(this, index, index);475}476477/**478* Removes the element at the specified position in this list.479* Returns the element that was removed from the list480*481* @param index the index of the element to removed482* @return the element previously at the specified position483* @throws ArrayIndexOutOfBoundsException if the index is out of range484* ({@code index < 0 || index >= size()})485*/486public E remove(int index) {487E rv = delegate.elementAt(index);488delegate.removeElementAt(index);489fireIntervalRemoved(this, index, index);490return rv;491}492493/**494* Removes all of the elements from this list. The list will495* be empty after this call returns (unless it throws an exception).496*/497public void clear() {498int index1 = delegate.size()-1;499delegate.removeAllElements();500if (index1 >= 0) {501fireIntervalRemoved(this, 0, index1);502}503}504505/**506* Deletes the components at the specified range of indexes.507* The removal is inclusive, so specifying a range of (1,5)508* removes the component at index 1 and the component at index 5,509* as well as all components in between.510*511* @param fromIndex the index of the lower end of the range512* @param toIndex the index of the upper end of the range513* @throws ArrayIndexOutOfBoundsException if the index was invalid514* @throws IllegalArgumentException if {@code fromIndex > toIndex}515* @see #remove(int)516*/517public void removeRange(int fromIndex, int toIndex) {518if (fromIndex > toIndex) {519throw new IllegalArgumentException("fromIndex must be <= toIndex");520}521for(int i = toIndex; i >= fromIndex; i--) {522delegate.removeElementAt(i);523}524fireIntervalRemoved(this, fromIndex, toIndex);525}526527/**528* Adds all of the elements present in the collection to the list.529*530* @param c the collection which contains the elements to add531* @throws NullPointerException if {@code c} is null532*/533public void addAll(Collection<? extends E> c) {534if (c.isEmpty()) {535return;536}537538int startIndex = getSize();539540delegate.addAll(c);541fireIntervalAdded(this, startIndex, getSize() - 1);542}543544/**545* Adds all of the elements present in the collection, starting546* from the specified index.547*548* @param index index at which to insert the first element from the549* specified collection550* @param c the collection which contains the elements to add551* @throws ArrayIndexOutOfBoundsException if {@code index} does not552* fall within the range of number of elements currently held553* @throws NullPointerException if {@code c} is null554*/555public void addAll(int index, Collection<? extends E> c) {556if (index < 0 || index > getSize()) {557throw new ArrayIndexOutOfBoundsException("index out of range: " +558index);559}560561if (c.isEmpty()) {562return;563}564565delegate.addAll(index, c);566fireIntervalAdded(this, index, index + c.size() - 1);567}568}569570571