0N/A/*
2362N/A * Copyright (c) 1997, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport java.util.Enumeration;
0N/A
0N/Aimport javax.swing.event.*;
0N/A
0N/A
0N/A/**
0N/A * This class loosely implements the <code>java.util.Vector</code>
0N/A * API, in that it implements the 1.1.x version of
0N/A * <code>java.util.Vector</code>, has no collection class support,
0N/A * and notifies the <code>ListDataListener</code>s when changes occur.
0N/A * Presently it delegates to a <code>Vector</code>,
0N/A * in a future release it will be a real Collection implementation.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A *
1983N/A * @param <E> the type of the elements of this model
1983N/A *
0N/A * @author Hans Muller
0N/A */
1983N/Apublic class DefaultListModel<E> extends AbstractListModel<E>
0N/A{
1983N/A private Vector<E> delegate = new Vector<E>();
0N/A
0N/A /**
0N/A * Returns the number of components in this list.
0N/A * <p>
0N/A * This method is identical to <code>size</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * This method exists in conjunction with <code>setSize</code> so that
0N/A * <code>size</code> is identifiable as a JavaBean property.
0N/A *
0N/A * @return the number of components in this list
0N/A * @see #size()
0N/A */
0N/A public int getSize() {
0N/A return delegate.size();
0N/A }
0N/A
0N/A /**
0N/A * Returns the component at the specified index.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>get(int)</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A * @param index an index into this list
0N/A * @return the component at the specified index
0N/A * @exception ArrayIndexOutOfBoundsException if the <code>index</code>
0N/A * is negative or greater than the current size of this
0N/A * list
0N/A * @see #get(int)
0N/A */
1983N/A public E getElementAt(int index) {
0N/A return delegate.elementAt(index);
0N/A }
0N/A
0N/A /**
0N/A * Copies the components of this list into the specified array.
0N/A * The array must be big enough to hold all the objects in this list,
0N/A * else an <code>IndexOutOfBoundsException</code> is thrown.
0N/A *
0N/A * @param anArray the array into which the components get copied
0N/A * @see Vector#copyInto(Object[])
0N/A */
0N/A public void copyInto(Object anArray[]) {
0N/A delegate.copyInto(anArray);
0N/A }
0N/A
0N/A /**
0N/A * Trims the capacity of this list to be the list's current size.
0N/A *
0N/A * @see Vector#trimToSize()
0N/A */
0N/A public void trimToSize() {
0N/A delegate.trimToSize();
0N/A }
0N/A
0N/A /**
0N/A * Increases the capacity of this list, if necessary, to ensure
0N/A * that it can hold at least the number of components specified by
0N/A * the minimum capacity argument.
0N/A *
0N/A * @param minCapacity the desired minimum capacity
0N/A * @see Vector#ensureCapacity(int)
0N/A */
0N/A public void ensureCapacity(int minCapacity) {
0N/A delegate.ensureCapacity(minCapacity);
0N/A }
0N/A
0N/A /**
0N/A * Sets the size of this list.
0N/A *
0N/A * @param newSize the new size of this list
0N/A * @see Vector#setSize(int)
0N/A */
0N/A public void setSize(int newSize) {
0N/A int oldSize = delegate.size();
0N/A delegate.setSize(newSize);
0N/A if (oldSize > newSize) {
0N/A fireIntervalRemoved(this, newSize, oldSize-1);
0N/A }
0N/A else if (oldSize < newSize) {
0N/A fireIntervalAdded(this, oldSize, newSize-1);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the current capacity of this list.
0N/A *
0N/A * @return the current capacity
0N/A * @see Vector#capacity()
0N/A */
0N/A public int capacity() {
0N/A return delegate.capacity();
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of components in this list.
0N/A *
0N/A * @return the number of components in this list
0N/A * @see Vector#size()
0N/A */
0N/A public int size() {
0N/A return delegate.size();
0N/A }
0N/A
0N/A /**
0N/A * Tests whether this list has any components.
0N/A *
0N/A * @return <code>true</code> if and only if this list has
0N/A * no components, that is, its size is zero;
0N/A * <code>false</code> otherwise
0N/A * @see Vector#isEmpty()
0N/A */
0N/A public boolean isEmpty() {
0N/A return delegate.isEmpty();
0N/A }
0N/A
0N/A /**
0N/A * Returns an enumeration of the components of this list.
0N/A *
0N/A * @return an enumeration of the components of this list
0N/A * @see Vector#elements()
0N/A */
1983N/A public Enumeration<E> elements() {
0N/A return delegate.elements();
0N/A }
0N/A
0N/A /**
0N/A * Tests whether the specified object is a component in this list.
0N/A *
0N/A * @param elem an object
0N/A * @return <code>true</code> if the specified object
0N/A * is the same as a component in this list
0N/A * @see Vector#contains(Object)
0N/A */
0N/A public boolean contains(Object elem) {
0N/A return delegate.contains(elem);
0N/A }
0N/A
0N/A /**
0N/A * Searches for the first occurrence of <code>elem</code>.
0N/A *
0N/A * @param elem an object
0N/A * @return the index of the first occurrence of the argument in this
0N/A * list; returns <code>-1</code> if the object is not found
0N/A * @see Vector#indexOf(Object)
0N/A */
0N/A public int indexOf(Object elem) {
0N/A return delegate.indexOf(elem);
0N/A }
0N/A
0N/A /**
0N/A * Searches for the first occurrence of <code>elem</code>, beginning
0N/A * the search at <code>index</code>.
0N/A *
0N/A * @param elem an desired component
0N/A * @param index the index from which to begin searching
0N/A * @return the index where the first occurrence of <code>elem</code>
0N/A * is found after <code>index</code>; returns <code>-1</code>
0N/A * if the <code>elem</code> is not found in the list
0N/A * @see Vector#indexOf(Object,int)
0N/A */
0N/A public int indexOf(Object elem, int index) {
0N/A return delegate.indexOf(elem, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the index of the last occurrence of <code>elem</code>.
0N/A *
0N/A * @param elem the desired component
0N/A * @return the index of the last occurrence of <code>elem</code>
0N/A * in the list; returns <code>-1</code> if the object is not found
0N/A * @see Vector#lastIndexOf(Object)
0N/A */
0N/A public int lastIndexOf(Object elem) {
0N/A return delegate.lastIndexOf(elem);
0N/A }
0N/A
0N/A /**
0N/A * Searches backwards for <code>elem</code>, starting from the
0N/A * specified index, and returns an index to it.
0N/A *
0N/A * @param elem the desired component
0N/A * @param index the index to start searching from
0N/A * @return the index of the last occurrence of the <code>elem</code>
0N/A * in this list at position less than <code>index</code>;
0N/A * returns <code>-1</code> if the object is not found
0N/A * @see Vector#lastIndexOf(Object,int)
0N/A */
0N/A public int lastIndexOf(Object elem, int index) {
0N/A return delegate.lastIndexOf(elem, index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the component at the specified index.
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
0N/A * is negative or not less than the size of the list.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>get(int)</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A *
0N/A * @param index an index into this list
0N/A * @return the component at the specified index
0N/A * @see #get(int)
0N/A * @see Vector#elementAt(int)
0N/A */
1983N/A public E elementAt(int index) {
0N/A return delegate.elementAt(index);
0N/A }
0N/A
0N/A /**
0N/A * Returns the first component of this list.
0N/A * Throws a <code>NoSuchElementException</code> if this
0N/A * vector has no components.
0N/A * @return the first component of this list
0N/A * @see Vector#firstElement()
0N/A */
1983N/A public E firstElement() {
0N/A return delegate.firstElement();
0N/A }
0N/A
0N/A /**
0N/A * Returns the last component of the list.
0N/A * Throws a <code>NoSuchElementException</code> if this vector
0N/A * has no components.
0N/A *
0N/A * @return the last component of the list
0N/A * @see Vector#lastElement()
0N/A */
1983N/A public E lastElement() {
0N/A return delegate.lastElement();
0N/A }
0N/A
0N/A /**
0N/A * Sets the component at the specified <code>index</code> of this
1983N/A * list to be the specified element. The previous component at that
0N/A * position is discarded.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
0N/A * is invalid.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>set(int,Object)</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A *
1983N/A * @param element what the component is to be set to
0N/A * @param index the specified index
0N/A * @see #set(int,Object)
0N/A * @see Vector#setElementAt(Object,int)
0N/A */
1983N/A public void setElementAt(E element, int index) {
1983N/A delegate.setElementAt(element, index);
0N/A fireContentsChanged(this, index, index);
0N/A }
0N/A
0N/A /**
0N/A * Deletes the component at the specified index.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
0N/A * is invalid.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>remove(int)</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A *
0N/A * @param index the index of the object to remove
0N/A * @see #remove(int)
0N/A * @see Vector#removeElementAt(int)
0N/A */
0N/A public void removeElementAt(int index) {
0N/A delegate.removeElementAt(index);
0N/A fireIntervalRemoved(this, index, index);
0N/A }
0N/A
0N/A /**
1983N/A * Inserts the specified element as a component in this list at the
0N/A * specified <code>index</code>.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
0N/A * is invalid.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>add(int,Object)</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A *
1983N/A * @param element the component to insert
0N/A * @param index where to insert the new component
0N/A * @exception ArrayIndexOutOfBoundsException if the index was invalid
0N/A * @see #add(int,Object)
0N/A * @see Vector#insertElementAt(Object,int)
0N/A */
1983N/A public void insertElementAt(E element, int index) {
1983N/A delegate.insertElementAt(element, index);
0N/A fireIntervalAdded(this, index, index);
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified component to the end of this list.
0N/A *
1983N/A * @param element the component to be added
0N/A * @see Vector#addElement(Object)
0N/A */
1983N/A public void addElement(E element) {
0N/A int index = delegate.size();
1983N/A delegate.addElement(element);
0N/A fireIntervalAdded(this, index, index);
0N/A }
0N/A
0N/A /**
0N/A * Removes the first (lowest-indexed) occurrence of the argument
0N/A * from this list.
0N/A *
0N/A * @param obj the component to be removed
0N/A * @return <code>true</code> if the argument was a component of this
0N/A * list; <code>false</code> otherwise
0N/A * @see Vector#removeElement(Object)
0N/A */
0N/A public boolean removeElement(Object obj) {
0N/A int index = indexOf(obj);
0N/A boolean rv = delegate.removeElement(obj);
0N/A if (index >= 0) {
0N/A fireIntervalRemoved(this, index, index);
0N/A }
0N/A return rv;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes all components from this list and sets its size to zero.
0N/A * <blockquote>
0N/A * <b>Note:</b> Although this method is not deprecated, the preferred
0N/A * method to use is <code>clear</code>, which implements the
0N/A * <code>List</code> interface defined in the 1.2 Collections framework.
0N/A * </blockquote>
0N/A *
0N/A * @see #clear()
0N/A * @see Vector#removeAllElements()
0N/A */
0N/A public void removeAllElements() {
0N/A int index1 = delegate.size()-1;
0N/A delegate.removeAllElements();
0N/A if (index1 >= 0) {
0N/A fireIntervalRemoved(this, 0, index1);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a string that displays and identifies this
0N/A * object's properties.
0N/A *
0N/A * @return a String representation of this object
0N/A */
0N/A public String toString() {
0N/A return delegate.toString();
0N/A }
0N/A
0N/A
0N/A /* The remaining methods are included for compatibility with the
0N/A * Java 2 platform Vector class.
0N/A */
0N/A
0N/A /**
0N/A * Returns an array containing all of the elements in this list in the
0N/A * correct order.
0N/A *
0N/A * @return an array containing the elements of the list
0N/A * @see Vector#toArray()
0N/A */
0N/A public Object[] toArray() {
0N/A Object[] rv = new Object[delegate.size()];
0N/A delegate.copyInto(rv);
0N/A return rv;
0N/A }
0N/A
0N/A /**
0N/A * Returns the element at the specified position in this list.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code>
0N/A * if the index is out of range
0N/A * (<code>index &lt; 0 || index &gt;= size()</code>).
0N/A *
0N/A * @param index index of element to return
0N/A */
1983N/A public E get(int index) {
0N/A return delegate.elementAt(index);
0N/A }
0N/A
0N/A /**
0N/A * Replaces the element at the specified position in this list with the
0N/A * specified element.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code>
0N/A * if the index is out of range
0N/A * (<code>index &lt; 0 || index &gt;= size()</code>).
0N/A *
0N/A * @param index index of element to replace
0N/A * @param element element to be stored at the specified position
0N/A * @return the element previously at the specified position
0N/A */
1983N/A public E set(int index, E element) {
1983N/A E rv = delegate.elementAt(index);
0N/A delegate.setElementAt(element, index);
0N/A fireContentsChanged(this, index, index);
0N/A return rv;
0N/A }
0N/A
0N/A /**
0N/A * Inserts the specified element at the specified position in this list.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
0N/A * index is out of range
0N/A * (<code>index &lt; 0 || index &gt; size()</code>).
0N/A *
0N/A * @param index index at which the specified element is to be inserted
0N/A * @param element element to be inserted
0N/A */
1983N/A public void add(int index, E element) {
0N/A delegate.insertElementAt(element, index);
0N/A fireIntervalAdded(this, index, index);
0N/A }
0N/A
0N/A /**
0N/A * Removes the element at the specified position in this list.
0N/A * Returns the element that was removed from the list.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code>
0N/A * if the index is out of range
0N/A * (<code>index &lt; 0 || index &gt;= size()</code>).
0N/A *
0N/A * @param index the index of the element to removed
1983N/A * @return the element previously at the specified position
0N/A */
1983N/A public E remove(int index) {
1983N/A E rv = delegate.elementAt(index);
0N/A delegate.removeElementAt(index);
0N/A fireIntervalRemoved(this, index, index);
0N/A return rv;
0N/A }
0N/A
0N/A /**
0N/A * Removes all of the elements from this list. The list will
0N/A * be empty after this call returns (unless it throws an exception).
0N/A */
0N/A public void clear() {
0N/A int index1 = delegate.size()-1;
0N/A delegate.removeAllElements();
0N/A if (index1 >= 0) {
0N/A fireIntervalRemoved(this, 0, index1);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Deletes the components at the specified range of indexes.
0N/A * The removal is inclusive, so specifying a range of (1,5)
0N/A * removes the component at index 1 and the component at index 5,
0N/A * as well as all components in between.
0N/A * <p>
0N/A * Throws an <code>ArrayIndexOutOfBoundsException</code>
0N/A * if the index was invalid.
0N/A * Throws an <code>IllegalArgumentException</code> if
0N/A * <code>fromIndex &gt; toIndex</code>.
0N/A *
0N/A * @param fromIndex the index of the lower end of the range
0N/A * @param toIndex the index of the upper end of the range
0N/A * @see #remove(int)
0N/A */
0N/A public void removeRange(int fromIndex, int toIndex) {
0N/A if (fromIndex > toIndex) {
0N/A throw new IllegalArgumentException("fromIndex must be <= toIndex");
0N/A }
0N/A for(int i = toIndex; i >= fromIndex; i--) {
0N/A delegate.removeElementAt(i);
0N/A }
0N/A fireIntervalRemoved(this, fromIndex, toIndex);
0N/A }
0N/A
0N/A /*
0N/A public void addAll(Collection c) {
0N/A }
0N/A
0N/A public void addAll(int index, Collection c) {
0N/A }
0N/A */
0N/A}