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     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]);
  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      *
  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     }
 180 }
--- EOF ---