Home » openjdk-7 » javax » swing » [javadoc | source]

    1   /*
    2    * Copyright (c) 1997, 2004, 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   
   26   package javax.swing;
   27   
   28   import java.util.Vector;
   29   import java.util.Enumeration;
   30   
   31   import javax.swing.event;
   32   
   33   
   34   /**
   35    * This class loosely implements the <code>java.util.Vector</code>
   36    * API, in that it implements the 1.1.x version of
   37    * <code>java.util.Vector</code>, has no collection class support,
   38    * and notifies the <code>ListDataListener</code>s when changes occur.
   39    * Presently it delegates to a <code>Vector</code>,
   40    * in a future release it will be a real Collection implementation.
   41    * <p>
   42    * <strong>Warning:</strong>
   43    * Serialized objects of this class will not be compatible with
   44    * future Swing releases. The current serialization support is
   45    * appropriate for short term storage or RMI between applications running
   46    * the same version of Swing.  As of 1.4, support for long term storage
   47    * of all JavaBeans<sup><font size="-2">TM</font></sup>
   48    * has been added to the <code>java.beans</code> package.
   49    * Please see {@link java.beans.XMLEncoder}.
   50    *
   51    * @param <E> the type of the elements of this model
   52    *
   53    * @author Hans Muller
   54    */
   55   public class DefaultListModel<E> extends AbstractListModel<E>
   56   {
   57       private Vector<E> delegate = new Vector<E>();
   58   
   59       /**
   60        * Returns the number of components in this list.
   61        * <p>
   62        * This method is identical to <code>size</code>, which implements the
   63        * <code>List</code> interface defined in the 1.2 Collections framework.
   64        * This method exists in conjunction with <code>setSize</code> so that
   65        * <code>size</code> is identifiable as a JavaBean property.
   66        *
   67        * @return  the number of components in this list
   68        * @see #size()
   69        */
   70       public int getSize() {
   71           return delegate.size();
   72       }
   73   
   74       /**
   75        * Returns the component at the specified index.
   76        * <blockquote>
   77        * <b>Note:</b> Although this method is not deprecated, the preferred
   78        *    method to use is <code>get(int)</code>, which implements the
   79        *    <code>List</code> interface defined in the 1.2 Collections framework.
   80        * </blockquote>
   81        * @param      index   an index into this list
   82        * @return     the component at the specified index
   83        * @exception  ArrayIndexOutOfBoundsException  if the <code>index</code>
   84        *             is negative or greater than the current size of this
   85        *             list
   86        * @see #get(int)
   87        */
   88       public E getElementAt(int index) {
   89           return delegate.elementAt(index);
   90       }
   91   
   92       /**
   93        * Copies the components of this list into the specified array.
   94        * The array must be big enough to hold all the objects in this list,
   95        * else an <code>IndexOutOfBoundsException</code> is thrown.
   96        *
   97        * @param   anArray   the array into which the components get copied
   98        * @see Vector#copyInto(Object[])
   99        */
  100       public void copyInto(Object anArray[]) {
  101           delegate.copyInto(anArray);
  102       }
  103   
  104       /**
  105        * Trims the capacity of this list to be the list's current size.
  106        *
  107        * @see Vector#trimToSize()
  108        */
  109       public void trimToSize() {
  110           delegate.trimToSize();
  111       }
  112   
  113       /**
  114        * Increases the capacity of this list, if necessary, to ensure
  115        * that it can hold at least the number of components specified by
  116        * the minimum capacity argument.
  117        *
  118        * @param   minCapacity   the desired minimum capacity
  119        * @see Vector#ensureCapacity(int)
  120        */
  121       public void ensureCapacity(int minCapacity) {
  122           delegate.ensureCapacity(minCapacity);
  123       }
  124   
  125       /**
  126        * Sets the size of this list.
  127        *
  128        * @param   newSize   the new size of this list
  129        * @see Vector#setSize(int)
  130        */
  131       public void setSize(int newSize) {
  132           int oldSize = delegate.size();
  133           delegate.setSize(newSize);
  134           if (oldSize > newSize) {
  135               fireIntervalRemoved(this, newSize, oldSize-1);
  136           }
  137           else if (oldSize < newSize) {
  138               fireIntervalAdded(this, oldSize, newSize-1);
  139           }
  140       }
  141   
  142       /**
  143        * Returns the current capacity of this list.
  144        *
  145        * @return  the current capacity
  146        * @see Vector#capacity()
  147        */
  148       public int capacity() {
  149           return delegate.capacity();
  150       }
  151   
  152       /**
  153        * Returns the number of components in this list.
  154        *
  155        * @return  the number of components in this list
  156        * @see Vector#size()
  157        */
  158       public int size() {
  159           return delegate.size();
  160       }
  161   
  162       /**
  163        * Tests whether this list has any components.
  164        *
  165        * @return  <code>true</code> if and only if this list has
  166        *          no components, that is, its size is zero;
  167        *          <code>false</code> otherwise
  168        * @see Vector#isEmpty()
  169        */
  170       public boolean isEmpty() {
  171           return delegate.isEmpty();
  172       }
  173   
  174       /**
  175        * Returns an enumeration of the components of this list.
  176        *
  177        * @return  an enumeration of the components of this list
  178        * @see Vector#elements()
  179        */
  180       public Enumeration<E> elements() {
  181           return delegate.elements();
  182       }
  183   
  184       /**
  185        * Tests whether the specified object is a component in this list.
  186        *
  187        * @param   elem   an object
  188        * @return  <code>true</code> if the specified object
  189        *          is the same as a component in this list
  190        * @see Vector#contains(Object)
  191        */
  192       public boolean contains(Object elem) {
  193           return delegate.contains(elem);
  194       }
  195   
  196       /**
  197        * Searches for the first occurrence of <code>elem</code>.
  198        *
  199        * @param   elem   an object
  200        * @return  the index of the first occurrence of the argument in this
  201        *          list; returns <code>-1</code> if the object is not found
  202        * @see Vector#indexOf(Object)
  203        */
  204       public int indexOf(Object elem) {
  205           return delegate.indexOf(elem);
  206       }
  207   
  208       /**
  209        * Searches for the first occurrence of <code>elem</code>, beginning
  210        * the search at <code>index</code>.
  211        *
  212        * @param   elem    an desired component
  213        * @param   index   the index from which to begin searching
  214        * @return  the index where the first occurrence of <code>elem</code>
  215        *          is found after <code>index</code>; returns <code>-1</code>
  216        *          if the <code>elem</code> is not found in the list
  217        * @see Vector#indexOf(Object,int)
  218        */
  219        public int indexOf(Object elem, int index) {
  220           return delegate.indexOf(elem, index);
  221       }
  222   
  223       /**
  224        * Returns the index of the last occurrence of <code>elem</code>.
  225        *
  226        * @param   elem   the desired component
  227        * @return  the index of the last occurrence of <code>elem</code>
  228        *          in the list; returns <code>-1</code> if the object is not found
  229        * @see Vector#lastIndexOf(Object)
  230        */
  231       public int lastIndexOf(Object elem) {
  232           return delegate.lastIndexOf(elem);
  233       }
  234   
  235       /**
  236        * Searches backwards for <code>elem</code>, starting from the
  237        * specified index, and returns an index to it.
  238        *
  239        * @param  elem    the desired component
  240        * @param  index   the index to start searching from
  241        * @return the index of the last occurrence of the <code>elem</code>
  242        *          in this list at position less than <code>index</code>;
  243        *          returns <code>-1</code> if the object is not found
  244        * @see Vector#lastIndexOf(Object,int)
  245        */
  246       public int lastIndexOf(Object elem, int index) {
  247           return delegate.lastIndexOf(elem, index);
  248       }
  249   
  250       /**
  251        * Returns the component at the specified index.
  252        * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  253        * is negative or not less than the size of the list.
  254        * <blockquote>
  255        * <b>Note:</b> Although this method is not deprecated, the preferred
  256        *    method to use is <code>get(int)</code>, which implements the
  257        *    <code>List</code> interface defined in the 1.2 Collections framework.
  258        * </blockquote>
  259        *
  260        * @param      index   an index into this list
  261        * @return     the component at the specified index
  262        * @see #get(int)
  263        * @see Vector#elementAt(int)
  264        */
  265       public E elementAt(int index) {
  266           return delegate.elementAt(index);
  267       }
  268   
  269       /**
  270        * Returns the first component of this list.
  271        * Throws a <code>NoSuchElementException</code> if this
  272        * vector has no components.
  273        * @return     the first component of this list
  274        * @see Vector#firstElement()
  275        */
  276       public E firstElement() {
  277           return delegate.firstElement();
  278       }
  279   
  280       /**
  281        * Returns the last component of the list.
  282        * Throws a <code>NoSuchElementException</code> if this vector
  283        * has no components.
  284        *
  285        * @return  the last component of the list
  286        * @see Vector#lastElement()
  287        */
  288       public E lastElement() {
  289           return delegate.lastElement();
  290       }
  291   
  292       /**
  293        * Sets the component at the specified <code>index</code> of this
  294        * list to be the specified element. The previous component at that
  295        * position is discarded.
  296        * <p>
  297        * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  298        * is invalid.
  299        * <blockquote>
  300        * <b>Note:</b> Although this method is not deprecated, the preferred
  301        *    method to use is <code>set(int,Object)</code>, which implements the
  302        *    <code>List</code> interface defined in the 1.2 Collections framework.
  303        * </blockquote>
  304        *
  305        * @param      element what the component is to be set to
  306        * @param      index   the specified index
  307        * @see #set(int,Object)
  308        * @see Vector#setElementAt(Object,int)
  309        */
  310       public void setElementAt(E element, int index) {
  311           delegate.setElementAt(element, index);
  312           fireContentsChanged(this, index, index);
  313       }
  314   
  315       /**
  316        * Deletes the component at the specified index.
  317        * <p>
  318        * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  319        * is invalid.
  320        * <blockquote>
  321        * <b>Note:</b> Although this method is not deprecated, the preferred
  322        *    method to use is <code>remove(int)</code>, which implements the
  323        *    <code>List</code> interface defined in the 1.2 Collections framework.
  324        * </blockquote>
  325        *
  326        * @param      index   the index of the object to remove
  327        * @see #remove(int)
  328        * @see Vector#removeElementAt(int)
  329        */
  330       public void removeElementAt(int index) {
  331           delegate.removeElementAt(index);
  332           fireIntervalRemoved(this, index, index);
  333       }
  334   
  335       /**
  336        * Inserts the specified element as a component in this list at the
  337        * specified <code>index</code>.
  338        * <p>
  339        * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  340        * is invalid.
  341        * <blockquote>
  342        * <b>Note:</b> Although this method is not deprecated, the preferred
  343        *    method to use is <code>add(int,Object)</code>, which implements the
  344        *    <code>List</code> interface defined in the 1.2 Collections framework.
  345        * </blockquote>
  346        *
  347        * @param      element the component to insert
  348        * @param      index   where to insert the new component
  349        * @exception  ArrayIndexOutOfBoundsException  if the index was invalid
  350        * @see #add(int,Object)
  351        * @see Vector#insertElementAt(Object,int)
  352        */
  353       public void insertElementAt(E element, int index) {
  354           delegate.insertElementAt(element, index);
  355           fireIntervalAdded(this, index, index);
  356       }
  357   
  358       /**
  359        * Adds the specified component to the end of this list.
  360        *
  361        * @param   element   the component to be added
  362        * @see Vector#addElement(Object)
  363        */
  364       public void addElement(E element) {
  365           int index = delegate.size();
  366           delegate.addElement(element);
  367           fireIntervalAdded(this, index, index);
  368       }
  369   
  370       /**
  371        * Removes the first (lowest-indexed) occurrence of the argument
  372        * from this list.
  373        *
  374        * @param   obj   the component to be removed
  375        * @return  <code>true</code> if the argument was a component of this
  376        *          list; <code>false</code> otherwise
  377        * @see Vector#removeElement(Object)
  378        */
  379       public boolean removeElement(Object obj) {
  380           int index = indexOf(obj);
  381           boolean rv = delegate.removeElement(obj);
  382           if (index >= 0) {
  383               fireIntervalRemoved(this, index, index);
  384           }
  385           return rv;
  386       }
  387   
  388   
  389       /**
  390        * Removes all components from this list and sets its size to zero.
  391        * <blockquote>
  392        * <b>Note:</b> Although this method is not deprecated, the preferred
  393        *    method to use is <code>clear</code>, which implements the
  394        *    <code>List</code> interface defined in the 1.2 Collections framework.
  395        * </blockquote>
  396        *
  397        * @see #clear()
  398        * @see Vector#removeAllElements()
  399        */
  400       public void removeAllElements() {
  401           int index1 = delegate.size()-1;
  402           delegate.removeAllElements();
  403           if (index1 >= 0) {
  404               fireIntervalRemoved(this, 0, index1);
  405           }
  406       }
  407   
  408   
  409       /**
  410        * Returns a string that displays and identifies this
  411        * object's properties.
  412        *
  413        * @return a String representation of this object
  414        */
  415      public String toString() {
  416           return delegate.toString();
  417       }
  418   
  419   
  420       /* The remaining methods are included for compatibility with the
  421        * Java 2 platform Vector class.
  422        */
  423   
  424       /**
  425        * Returns an array containing all of the elements in this list in the
  426        * correct order.
  427        *
  428        * @return an array containing the elements of the list
  429        * @see Vector#toArray()
  430        */
  431       public Object[] toArray() {
  432           Object[] rv = new Object[delegate.size()];
  433           delegate.copyInto(rv);
  434           return rv;
  435       }
  436   
  437       /**
  438        * Returns the element at the specified position in this list.
  439        * <p>
  440        * Throws an <code>ArrayIndexOutOfBoundsException</code>
  441        * if the index is out of range
  442        * (<code>index &lt; 0 || index &gt;= size()</code>).
  443        *
  444        * @param index index of element to return
  445        */
  446       public E get(int index) {
  447           return delegate.elementAt(index);
  448       }
  449   
  450       /**
  451        * Replaces the element at the specified position in this list with the
  452        * specified element.
  453        * <p>
  454        * Throws an <code>ArrayIndexOutOfBoundsException</code>
  455        * if the index is out of range
  456        * (<code>index &lt; 0 || index &gt;= size()</code>).
  457        *
  458        * @param index index of element to replace
  459        * @param element element to be stored at the specified position
  460        * @return the element previously at the specified position
  461        */
  462       public E set(int index, E element) {
  463           E rv = delegate.elementAt(index);
  464           delegate.setElementAt(element, index);
  465           fireContentsChanged(this, index, index);
  466           return rv;
  467       }
  468   
  469       /**
  470        * Inserts the specified element at the specified position in this list.
  471        * <p>
  472        * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
  473        * index is out of range
  474        * (<code>index &lt; 0 || index &gt; size()</code>).
  475        *
  476        * @param index index at which the specified element is to be inserted
  477        * @param element element to be inserted
  478        */
  479       public void add(int index, E element) {
  480           delegate.insertElementAt(element, index);
  481           fireIntervalAdded(this, index, index);
  482       }
  483   
  484       /**
  485        * Removes the element at the specified position in this list.
  486        * Returns the element that was removed from the list.
  487        * <p>
  488        * Throws an <code>ArrayIndexOutOfBoundsException</code>
  489        * if the index is out of range
  490        * (<code>index &lt; 0 || index &gt;= size()</code>).
  491        *
  492        * @param index the index of the element to removed
  493        * @return the element previously at the specified position
  494        */
  495       public E remove(int index) {
  496           E rv = delegate.elementAt(index);
  497           delegate.removeElementAt(index);
  498           fireIntervalRemoved(this, index, index);
  499           return rv;
  500       }
  501   
  502       /**
  503        * Removes all of the elements from this list.  The list will
  504        * be empty after this call returns (unless it throws an exception).
  505        */
  506       public void clear() {
  507           int index1 = delegate.size()-1;
  508           delegate.removeAllElements();
  509           if (index1 >= 0) {
  510               fireIntervalRemoved(this, 0, index1);
  511           }
  512       }
  513   
  514       /**
  515        * Deletes the components at the specified range of indexes.
  516        * The removal is inclusive, so specifying a range of (1,5)
  517        * removes the component at index 1 and the component at index 5,
  518        * as well as all components in between.
  519        * <p>
  520        * Throws an <code>ArrayIndexOutOfBoundsException</code>
  521        * if the index was invalid.
  522        * Throws an <code>IllegalArgumentException</code> if
  523        * <code>fromIndex &gt; toIndex</code>.
  524        *
  525        * @param      fromIndex the index of the lower end of the range
  526        * @param      toIndex   the index of the upper end of the range
  527        * @see        #remove(int)
  528        */
  529       public void removeRange(int fromIndex, int toIndex) {
  530           if (fromIndex > toIndex) {
  531               throw new IllegalArgumentException("fromIndex must be <= toIndex");
  532           }
  533           for(int i = toIndex; i >= fromIndex; i--) {
  534               delegate.removeElementAt(i);
  535           }
  536           fireIntervalRemoved(this, fromIndex, toIndex);
  537       }
  538   
  539       /*
  540       public void addAll(Collection c) {
  541       }
  542   
  543       public void addAll(int index, Collection c) {
  544       }
  545       */
  546   }

Home » openjdk-7 » javax » swing » [javadoc | source]