    rev 48858 : Work in progress for JDK-6303622
    Use generic in Swing components like JComboBox
    1. Added generic return type and parameter type for JComboBox.
    2. Modified JList related classes as well.
    3. Reverted the changes made to "getSelectedValues" as suggested by Joe.
    4. Modified files that were failing on Linux and Mac as well.

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