       1 /*
       2  * Copyright (c) 1998, 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 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]);
      64
      65         if ( getSize() > 0 ) {
      66             selectedObject = getElementAt( 0 );
      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     }
     183 }