jdk/src/share/classes/javax/swing/DefaultComboBoxModel.java
author rupashka
Wed, 04 May 2011 10:20:14 +0400
changeset 9665 f43db0b310cc
parent 5506 202f599c92aa
child 17898 f47410f6015c
permissions -rw-r--r--
7031551: Generics: JComboBox Reviewed-by: alexp, malenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2004, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * The default model for combo boxes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    34
 * @param <E> the type of the elements of this model
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    35
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    40
public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable {
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    41
    Vector<E> objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    Object selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * Constructs an empty DefaultComboBoxModel object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    public DefaultComboBoxModel() {
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    48
        objects = new Vector<E>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Constructs a DefaultComboBoxModel object initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * an array of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @param items  an array of Object objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    57
    public DefaultComboBoxModel(final E items[]) {
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    58
        objects = new Vector<E>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        objects.ensureCapacity( items.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for ( i=0,c=items.length;i<c;i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            objects.addElement(items[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Constructs a DefaultComboBoxModel object initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * @param v  a Vector object ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    76
    public DefaultComboBoxModel(Vector<E> v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        objects = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Set the value of the selected item. The selected item may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param anObject The combo box value or null for no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void setSelectedItem(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if ((selectedObject != null && !selectedObject.equals( anObject )) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            selectedObject == null && anObject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            selectedObject = anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            fireContentsChanged(this, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public Object getSelectedItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // implements javax.swing.ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return objects.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // implements javax.swing.ListModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   109
    public E getElementAt(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if ( index >= 0 && index < objects.size() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            return objects.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * Returns the index-position of the specified object in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @param anObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return an int representing the index position, where 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *         the first position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int getIndexOf(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // implements javax.swing.MutableComboBoxModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   128
    public void addElement(E anObject) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        objects.addElement(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        fireIntervalAdded(this,objects.size()-1, objects.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            setSelectedItem( anObject );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // implements javax.swing.MutableComboBoxModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   137
    public void insertElementAt(E anObject,int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        objects.insertElementAt(anObject,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        fireIntervalAdded(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void removeElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if ( getElementAt( index ) == selectedObject ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if ( index == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                setSelectedItem( getElementAt( index - 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        objects.removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        fireIntervalRemoved(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void removeElement(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        int index = objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if ( index != -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * Empties the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public void removeAllElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if ( objects.size() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int firstIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            int lastIndex = objects.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            objects.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            fireIntervalRemoved(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
}