Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/swing/DefaultListModel.java
38829 views
/*1* Copyright (c) 1997, 2013, 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.Enumeration;2930import javax.swing.event.*;313233/**34* This class loosely implements the <code>java.util.Vector</code>35* API, in that it implements the 1.1.x version of36* <code>java.util.Vector</code>, has no collection class support,37* and notifies the <code>ListDataListener</code>s when changes occur.38* Presently it delegates to a <code>Vector</code>,39* in a future release it will be a real Collection implementation.40* <p>41* <strong>Warning:</strong>42* Serialized objects of this class will not be compatible with43* future Swing releases. The current serialization support is44* appropriate for short term storage or RMI between applications running45* the same version of Swing. As of 1.4, support for long term storage46* of all JavaBeans™47* has been added to the <code>java.beans</code> package.48* Please see {@link java.beans.XMLEncoder}.49*50* @param <E> the type of the elements of this model51*52* @author Hans Muller53*/54public class DefaultListModel<E> extends AbstractListModel<E>55{56private Vector<E> delegate = new Vector<E>();5758/**59* Returns the number of components in this list.60* <p>61* This method is identical to <code>size</code>, which implements the62* <code>List</code> interface defined in the 1.2 Collections framework.63* This method exists in conjunction with <code>setSize</code> so that64* <code>size</code> is identifiable as a JavaBean property.65*66* @return the number of components in this list67* @see #size()68*/69public int getSize() {70return delegate.size();71}7273/**74* Returns the component at the specified index.75* <blockquote>76* <b>Note:</b> Although this method is not deprecated, the preferred77* method to use is <code>get(int)</code>, which implements the78* <code>List</code> interface defined in the 1.2 Collections framework.79* </blockquote>80* @param index an index into this list81* @return the component at the specified index82* @exception ArrayIndexOutOfBoundsException if the <code>index</code>83* is negative or greater than the current size of this84* list85* @see #get(int)86*/87public E getElementAt(int index) {88return delegate.elementAt(index);89}9091/**92* Copies the components of this list into the specified array.93* The array must be big enough to hold all the objects in this list,94* else an <code>IndexOutOfBoundsException</code> is thrown.95*96* @param anArray the array into which the components get copied97* @see Vector#copyInto(Object[])98*/99public void copyInto(Object anArray[]) {100delegate.copyInto(anArray);101}102103/**104* Trims the capacity of this list to be the list's current size.105*106* @see Vector#trimToSize()107*/108public void trimToSize() {109delegate.trimToSize();110}111112/**113* Increases the capacity of this list, if necessary, to ensure114* that it can hold at least the number of components specified by115* the minimum capacity argument.116*117* @param minCapacity the desired minimum capacity118* @see Vector#ensureCapacity(int)119*/120public void ensureCapacity(int minCapacity) {121delegate.ensureCapacity(minCapacity);122}123124/**125* Sets the size of this list.126*127* @param newSize the new size of this list128* @see Vector#setSize(int)129*/130public void setSize(int newSize) {131int oldSize = delegate.size();132delegate.setSize(newSize);133if (oldSize > newSize) {134fireIntervalRemoved(this, newSize, oldSize-1);135}136else if (oldSize < newSize) {137fireIntervalAdded(this, oldSize, newSize-1);138}139}140141/**142* Returns the current capacity of this list.143*144* @return the current capacity145* @see Vector#capacity()146*/147public int capacity() {148return delegate.capacity();149}150151/**152* Returns the number of components in this list.153*154* @return the number of components in this list155* @see Vector#size()156*/157public int size() {158return delegate.size();159}160161/**162* Tests whether this list has any components.163*164* @return <code>true</code> if and only if this list has165* no components, that is, its size is zero;166* <code>false</code> otherwise167* @see Vector#isEmpty()168*/169public boolean isEmpty() {170return delegate.isEmpty();171}172173/**174* Returns an enumeration of the components of this list.175*176* @return an enumeration of the components of this list177* @see Vector#elements()178*/179public Enumeration<E> elements() {180return delegate.elements();181}182183/**184* Tests whether the specified object is a component in this list.185*186* @param elem an object187* @return <code>true</code> if the specified object188* is the same as a component in this list189* @see Vector#contains(Object)190*/191public boolean contains(Object elem) {192return delegate.contains(elem);193}194195/**196* Searches for the first occurrence of <code>elem</code>.197*198* @param elem an object199* @return the index of the first occurrence of the argument in this200* list; returns <code>-1</code> if the object is not found201* @see Vector#indexOf(Object)202*/203public int indexOf(Object elem) {204return delegate.indexOf(elem);205}206207/**208* Searches for the first occurrence of <code>elem</code>, beginning209* the search at <code>index</code>.210*211* @param elem an desired component212* @param index the index from which to begin searching213* @return the index where the first occurrence of <code>elem</code>214* is found after <code>index</code>; returns <code>-1</code>215* if the <code>elem</code> is not found in the list216* @see Vector#indexOf(Object,int)217*/218public int indexOf(Object elem, int index) {219return delegate.indexOf(elem, index);220}221222/**223* Returns the index of the last occurrence of <code>elem</code>.224*225* @param elem the desired component226* @return the index of the last occurrence of <code>elem</code>227* in the list; returns <code>-1</code> if the object is not found228* @see Vector#lastIndexOf(Object)229*/230public int lastIndexOf(Object elem) {231return delegate.lastIndexOf(elem);232}233234/**235* Searches backwards for <code>elem</code>, starting from the236* specified index, and returns an index to it.237*238* @param elem the desired component239* @param index the index to start searching from240* @return the index of the last occurrence of the <code>elem</code>241* in this list at position less than <code>index</code>;242* returns <code>-1</code> if the object is not found243* @see Vector#lastIndexOf(Object,int)244*/245public int lastIndexOf(Object elem, int index) {246return delegate.lastIndexOf(elem, index);247}248249/**250* Returns the component at the specified index.251* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index252* is negative or not less than the size of the list.253* <blockquote>254* <b>Note:</b> Although this method is not deprecated, the preferred255* method to use is <code>get(int)</code>, which implements the256* <code>List</code> interface defined in the 1.2 Collections framework.257* </blockquote>258*259* @param index an index into this list260* @return the component at the specified index261* @see #get(int)262* @see Vector#elementAt(int)263*/264public E elementAt(int index) {265return delegate.elementAt(index);266}267268/**269* Returns the first component of this list.270* Throws a <code>NoSuchElementException</code> if this271* vector has no components.272* @return the first component of this list273* @see Vector#firstElement()274*/275public E firstElement() {276return delegate.firstElement();277}278279/**280* Returns the last component of the list.281* Throws a <code>NoSuchElementException</code> if this vector282* has no components.283*284* @return the last component of the list285* @see Vector#lastElement()286*/287public E lastElement() {288return delegate.lastElement();289}290291/**292* Sets the component at the specified <code>index</code> of this293* list to be the specified element. The previous component at that294* position is discarded.295* <p>296* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index297* is invalid.298* <blockquote>299* <b>Note:</b> Although this method is not deprecated, the preferred300* method to use is <code>set(int,Object)</code>, which implements the301* <code>List</code> interface defined in the 1.2 Collections framework.302* </blockquote>303*304* @param element what the component is to be set to305* @param index the specified index306* @see #set(int,Object)307* @see Vector#setElementAt(Object,int)308*/309public void setElementAt(E element, int index) {310delegate.setElementAt(element, index);311fireContentsChanged(this, index, index);312}313314/**315* Deletes the component at the specified index.316* <p>317* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index318* is invalid.319* <blockquote>320* <b>Note:</b> Although this method is not deprecated, the preferred321* method to use is <code>remove(int)</code>, which implements the322* <code>List</code> interface defined in the 1.2 Collections framework.323* </blockquote>324*325* @param index the index of the object to remove326* @see #remove(int)327* @see Vector#removeElementAt(int)328*/329public void removeElementAt(int index) {330delegate.removeElementAt(index);331fireIntervalRemoved(this, index, index);332}333334/**335* Inserts the specified element as a component in this list at the336* specified <code>index</code>.337* <p>338* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index339* is invalid.340* <blockquote>341* <b>Note:</b> Although this method is not deprecated, the preferred342* method to use is <code>add(int,Object)</code>, which implements the343* <code>List</code> interface defined in the 1.2 Collections framework.344* </blockquote>345*346* @param element the component to insert347* @param index where to insert the new component348* @exception ArrayIndexOutOfBoundsException if the index was invalid349* @see #add(int,Object)350* @see Vector#insertElementAt(Object,int)351*/352public void insertElementAt(E element, int index) {353delegate.insertElementAt(element, index);354fireIntervalAdded(this, index, index);355}356357/**358* Adds the specified component to the end of this list.359*360* @param element the component to be added361* @see Vector#addElement(Object)362*/363public void addElement(E element) {364int index = delegate.size();365delegate.addElement(element);366fireIntervalAdded(this, index, index);367}368369/**370* Removes the first (lowest-indexed) occurrence of the argument371* from this list.372*373* @param obj the component to be removed374* @return <code>true</code> if the argument was a component of this375* list; <code>false</code> otherwise376* @see Vector#removeElement(Object)377*/378public boolean removeElement(Object obj) {379int index = indexOf(obj);380boolean rv = delegate.removeElement(obj);381if (index >= 0) {382fireIntervalRemoved(this, index, index);383}384return rv;385}386387388/**389* Removes all components from this list and sets its size to zero.390* <blockquote>391* <b>Note:</b> Although this method is not deprecated, the preferred392* method to use is <code>clear</code>, which implements the393* <code>List</code> interface defined in the 1.2 Collections framework.394* </blockquote>395*396* @see #clear()397* @see Vector#removeAllElements()398*/399public void removeAllElements() {400int index1 = delegate.size()-1;401delegate.removeAllElements();402if (index1 >= 0) {403fireIntervalRemoved(this, 0, index1);404}405}406407408/**409* Returns a string that displays and identifies this410* object's properties.411*412* @return a String representation of this object413*/414public String toString() {415return delegate.toString();416}417418419/* The remaining methods are included for compatibility with the420* Java 2 platform Vector class.421*/422423/**424* Returns an array containing all of the elements in this list in the425* correct order.426*427* @return an array containing the elements of the list428* @see Vector#toArray()429*/430public Object[] toArray() {431Object[] rv = new Object[delegate.size()];432delegate.copyInto(rv);433return rv;434}435436/**437* Returns the element at the specified position in this list.438* <p>439* Throws an <code>ArrayIndexOutOfBoundsException</code>440* if the index is out of range441* (<code>index < 0 || index >= size()</code>).442*443* @param index index of element to return444*/445public E get(int index) {446return delegate.elementAt(index);447}448449/**450* Replaces the element at the specified position in this list with the451* specified element.452* <p>453* Throws an <code>ArrayIndexOutOfBoundsException</code>454* if the index is out of range455* (<code>index < 0 || index >= size()</code>).456*457* @param index index of element to replace458* @param element element to be stored at the specified position459* @return the element previously at the specified position460*/461public E set(int index, E element) {462E rv = delegate.elementAt(index);463delegate.setElementAt(element, index);464fireContentsChanged(this, index, index);465return rv;466}467468/**469* Inserts the specified element at the specified position in this list.470* <p>471* Throws an <code>ArrayIndexOutOfBoundsException</code> if the472* index is out of range473* (<code>index < 0 || index > size()</code>).474*475* @param index index at which the specified element is to be inserted476* @param element element to be inserted477*/478public void add(int index, E element) {479delegate.insertElementAt(element, index);480fireIntervalAdded(this, index, index);481}482483/**484* Removes the element at the specified position in this list.485* Returns the element that was removed from the list.486* <p>487* Throws an <code>ArrayIndexOutOfBoundsException</code>488* if the index is out of range489* (<code>index < 0 || index >= size()</code>).490*491* @param index the index of the element to removed492* @return the element previously at the specified position493*/494public E remove(int index) {495E rv = delegate.elementAt(index);496delegate.removeElementAt(index);497fireIntervalRemoved(this, index, index);498return rv;499}500501/**502* Removes all of the elements from this list. The list will503* be empty after this call returns (unless it throws an exception).504*/505public void clear() {506int index1 = delegate.size()-1;507delegate.removeAllElements();508if (index1 >= 0) {509fireIntervalRemoved(this, 0, index1);510}511}512513/**514* Deletes the components at the specified range of indexes.515* The removal is inclusive, so specifying a range of (1,5)516* removes the component at index 1 and the component at index 5,517* as well as all components in between.518* <p>519* Throws an <code>ArrayIndexOutOfBoundsException</code>520* if the index was invalid.521* Throws an <code>IllegalArgumentException</code> if522* <code>fromIndex > toIndex</code>.523*524* @param fromIndex the index of the lower end of the range525* @param toIndex the index of the upper end of the range526* @see #remove(int)527*/528public void removeRange(int fromIndex, int toIndex) {529if (fromIndex > toIndex) {530throw new IllegalArgumentException("fromIndex must be <= toIndex");531}532for(int i = toIndex; i >= fromIndex; i--) {533delegate.removeElementAt(i);534}535fireIntervalRemoved(this, fromIndex, toIndex);536}537538/*539public void addAll(Collection c) {540}541542public void addAll(int index, Collection c) {543}544*/545}546547548