< prev index next >

## src/java.desktop/share/classes/javax/swing/DefaultComboBoxModel.java

Print this page

    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.

      23  * questions.
      24  */
      25 package javax.swing;
      26
      27 import java.util.*;
      28
      29 import java.io.Serializable;
      30
      31 /**
      32  * The default model for combo boxes.
      33  *
      34  * @param <E> the type of the elements of this model
      35  *
      36  * @author Arnaud Weber
      37  * @author Tom Santos
      38  * @since 1.2
      39  */
      40 @SuppressWarnings("serial") // Superclass is not serializable across versions
      41 public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {
      42     Vector<E> objects;
      43     Object selectedObject;
      44
      45     /**
      46      * Constructs an empty DefaultComboBoxModel object.
      47      */
      48     public DefaultComboBoxModel() {
      49         objects = new Vector<E>();
      50     }
      51
      52     /**
      53      * Constructs a DefaultComboBoxModel object initialized with
      54      * an array of objects.
      55      *
      56      * @param items  an array of Object objects
      57      */
      58     public DefaultComboBoxModel(final E items[]) {
      59         objects = new Vector<E>(items.length);
      60
      61         int i,c;
      62         for ( i=0,c=items.length;i<c;i++ )
      63             objects.addElement(items[i]);
      67         }
      68     }
      69
      70     /**
      71      * Constructs a DefaultComboBoxModel object initialized with
      72      * a vector.
      73      *
      74      * @param v  a Vector object ...
      75      */
      76     public DefaultComboBoxModel(Vector<E> v) {
      77         objects = v;
      78
      79         if ( getSize() > 0 ) {
      80             selectedObject = getElementAt( 0 );
      81         }
      82     }
      83
      84     // implements javax.swing.ComboBoxModel
      85     /**
      86      * Set the value of the selected item. The selected item may be null.
      87      *
      88      * @param anObject The combo box value or null for no selection.
      89      */
      90     public void setSelectedItem(Object anObject) {

      91         if ((selectedObject != null && !selectedObject.equals( anObject )) ||
      92             selectedObject == null && anObject != null) {
      93             selectedObject = anObject;
      94             fireContentsChanged(this, -1, -1);
      95         }
      96     }
      97
      98     // implements javax.swing.ComboBoxModel
      99     public Object getSelectedItem() {

     100         return selectedObject;
     101     }
     102
     103     // implements javax.swing.ListModel
     104     public int getSize() {
     105         return objects.size();
     106     }
     107
     108     // implements javax.swing.ListModel
     109     public E getElementAt(int index) {
     110         if ( index >= 0 && index < objects.size() )
     111             return objects.elementAt(index);
     112         else
     113             return null;
     114     }
     115
     116     /**
     117      * Returns the index-position of the specified object in the list.
     118      *

     119      * @param anObject the object to return the index of
     120      * @return an int representing the index position, where 0 is
     121      *         the first position
     122      */
     123     public int getIndexOf(Object anObject) {
     124         return objects.indexOf(anObject);
     125     }
     126
     127     // implements javax.swing.MutableComboBoxModel
     128     public void addElement(E anObject) {
     129         objects.addElement(anObject);
     130         fireIntervalAdded(this,objects.size()-1, objects.size()-1);
     131         if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
     132             setSelectedItem( anObject );
     133         }
     134     }
     135
     136     // implements javax.swing.MutableComboBoxModel
     137     public void insertElementAt(E anObject,int index) {
     138         objects.insertElementAt(anObject,index);
     139         fireIntervalAdded(this, index, index);
     140     }
     141
     142     // implements javax.swing.MutableComboBoxModel
     143     public void removeElementAt(int index) {
     144         if ( getElementAt( index ) == selectedObject ) {
     145             if ( index == 0 ) {
     146                 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
     147             }
     148             else {
     149                 setSelectedItem( getElementAt( index - 1 ) );
     150             }
     151         }
     152
     153         objects.removeElementAt(index);
     154
     155         fireIntervalRemoved(this, index, index);
     156     }
     157
     158     // implements javax.swing.MutableComboBoxModel
     159     public void removeElement(Object anObject) {
     160         int index = objects.indexOf(anObject);
     161         if ( index != -1 ) {
     162             removeElementAt(index);
     163         }
     164     }
     165
     166     /**
     167      * Empties the list.
     168      */
     169     public void removeAllElements() {
     170         if ( objects.size() > 0 ) {
     171             int firstIndex = 0;
     172             int lastIndex = objects.size() - 1;
     173             objects.removeAllElements();
     174             selectedObject = null;
     175             fireIntervalRemoved(this, firstIndex, lastIndex);
     176         } else {
     177             selectedObject = null;
     178         }
     179     }
      23  * questions.
      24  */
      25 package javax.swing;
      26
      27 import java.util.*;
      28
      29 import java.io.Serializable;
      30
      31 /**
      32  * The default model for combo boxes.
      33  *
      34  * @param <E> the type of the elements of this model
      35  *
      36  * @author Arnaud Weber
      37  * @author Tom Santos
      38  * @since 1.2
      39  */
      40 @SuppressWarnings("serial") // Superclass is not serializable across versions
      41 public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {
      42     Vector<E> objects;
      43     E selectedObject;
      44
      45     /**
      46      * Constructs an empty DefaultComboBoxModel object.
      47      */
      48     public DefaultComboBoxModel() {
      49         objects = new Vector<E>();
      50     }
      51
      52     /**
      53      * Constructs a DefaultComboBoxModel object initialized with
      54      * an array of objects.
      55      *
      56      * @param items  an array of Object objects
      57      */
      58     public DefaultComboBoxModel(final E items[]) {
      59         objects = new Vector<E>(items.length);
      60
      61         int i,c;
      62         for ( i=0,c=items.length;i<c;i++ )
      63             objects.addElement(items[i]);
      67         }
      68     }
      69
      70     /**
      71      * Constructs a DefaultComboBoxModel object initialized with
      72      * a vector.
      73      *
      74      * @param v  a Vector object ...
      75      */
      76     public DefaultComboBoxModel(Vector<E> v) {
      77         objects = v;
      78
      79         if ( getSize() > 0 ) {
      80             selectedObject = getElementAt( 0 );
      81         }
      82     }
      83
      84     // implements javax.swing.ComboBoxModel
      85     /**
      86      * Set the value of the selected item. The selected item may be null.
      87      * @param <E> the type of the elements of this model.
      88      * @param anObject The combo box value or null for no selection.
      89      */
      90     @Override
      91     public void setSelectedItem(E anObject) {
      92         if ((selectedObject != null && !selectedObject.equals( anObject )) ||
      93             selectedObject == null && anObject != null) {
      94             selectedObject = anObject;
      95             fireContentsChanged(this, -1, -1);
      96         }
      97     }
      98
      99     // implements javax.swing.ComboBoxModel
     100     @Override
     101     public E getSelectedItem() {
     102         return selectedObject;
     103     }
     104
     105     // implements javax.swing.ListModel
     106     public int getSize() {
     107         return objects.size();
     108     }
     109
     110     // implements javax.swing.ListModel
     111     public E getElementAt(int index) {
     112         if ( index >= 0 && index < objects.size() )
     113             return objects.elementAt(index);
     114         else
     115             return null;
     116     }
     117
     118     /**
     119      * Returns the index-position of the specified object in the list.
     120      *
     121      * @param <E> the type of the elements of this model.
     122      * @param anObject the object to return the index of
     123      * @return an int representing the index position, where 0 is
     124      *         the first position
     125      */
     126     public int getIndexOf(E anObject) {
     127         return objects.indexOf(anObject);
     128     }
     129
     130     // implements javax.swing.MutableComboBoxModel
     131     public void addElement(E anObject) {
     132         objects.addElement(anObject);
     133         fireIntervalAdded(this,objects.size()-1, objects.size()-1);
     134         if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
     135             setSelectedItem( anObject );
     136         }
     137     }
     138
     139     // implements javax.swing.MutableComboBoxModel
     140     public void insertElementAt(E anObject,int index) {
     141         objects.insertElementAt(anObject,index);
     142         fireIntervalAdded(this, index, index);
     143     }
     144
     145     // implements javax.swing.MutableComboBoxModel
     146     public void removeElementAt(int index) {
     147         if ( getElementAt( index ) == selectedObject ) {
     148             if ( index == 0 ) {
     149                 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
     150             }
     151             else {
     152                 setSelectedItem( getElementAt( index - 1 ) );
     153             }
     154         }
     155
     156         objects.removeElementAt(index);
     157
     158         fireIntervalRemoved(this, index, index);
     159     }
     160
     161     // implements javax.swing.MutableComboBoxModel
     162     public void removeElement(E anObject) {
     163         int index = objects.indexOf(anObject);
     164         if ( index != -1 ) {
     165             removeElementAt(index);
     166         }
     167     }
     168
     169     /**
     170      * Empties the list.
     171      */
     172     public void removeAllElements() {
     173         if ( objects.size() > 0 ) {
     174             int firstIndex = 0;
     175             int lastIndex = objects.size() - 1;
     176             objects.removeAllElements();
     177             selectedObject = null;
     178             fireIntervalRemoved(this, firstIndex, lastIndex);
     179         } else {
     180             selectedObject = null;
     181         }
     182     }

< prev index next >