< prev index next >

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

Print this page

    rev 48358 : 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.

      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]);
      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]);
      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     @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 anObject the object to return the index of
     122      * @return an int representing the index position, where 0 is
     123      *         the first position
     124      */
     125     public int getIndexOf(E anObject) {
     126         return objects.indexOf(anObject);
     127     }
     128
     129     // implements javax.swing.MutableComboBoxModel
     130     public void addElement(E anObject) {
     131         objects.addElement(anObject);
     132         fireIntervalAdded(this,objects.size()-1, objects.size()-1);
     133         if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
     134             setSelectedItem( anObject );
     135         }
     136     }
     137
     138     // implements javax.swing.MutableComboBoxModel
     139     public void insertElementAt(E anObject,int index) {
     140         objects.insertElementAt(anObject,index);
     141         fireIntervalAdded(this, index, index);
     142     }
     143
     144     // implements javax.swing.MutableComboBoxModel
     145     public void removeElementAt(int index) {
     146         if ( getElementAt( index ) == selectedObject ) {
     147             if ( index == 0 ) {
     148                 setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
     149             }
     150             else {
     151                 setSelectedItem( getElementAt( index - 1 ) );
     152             }
     153         }
     154
     155         objects.removeElementAt(index);
     156
     157         fireIntervalRemoved(this, index, index);
     158     }
     159
     160     // implements javax.swing.MutableComboBoxModel
     161     public void removeElement(E anObject) {
     162         int index = objects.indexOf(anObject);
     163         if ( index != -1 ) {
     164             removeElementAt(index);
     165         }
     166     }
     167
     168     /**
     169      * Empties the list.
     170      */
     171     public void removeAllElements() {
     172         if ( objects.size() > 0 ) {
     173             int firstIndex = 0;
     174             int lastIndex = objects.size() - 1;
     175             objects.removeAllElements();
     176             selectedObject = null;
     177             fireIntervalRemoved(this, firstIndex, lastIndex);
     178         } else {
     179             selectedObject = null;
     180         }
     181     }

< prev index next >