jdk/src/share/classes/javax/swing/DefaultComboBoxModel.java
author yan
Thu, 05 Dec 2013 18:04:12 +0400
changeset 21982 fd6e5fe509df
parent 17898 f47410f6015c
child 22260 c9185e010e03
permissions -rw-r--r--
8029264: [doclint] more doclint and tidy cleanup Reviewed-by: alexsch, serb, malenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17898
f47410f6015c 6436314: Vector could be created with appropriate size in DefaultComboBoxModel
vkarnauk
parents: 9665
diff changeset
     2
 * Copyright (c) 1998, 2013, 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[]) {
17898
f47410f6015c 6436314: Vector could be created with appropriate size in DefaultComboBoxModel
vkarnauk
parents: 9665
diff changeset
    58
        objects = new Vector<E>(items.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        for ( i=0,c=items.length;i<c;i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            objects.addElement(items[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
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
     * Constructs a DefaultComboBoxModel object initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @param v  a Vector object ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
    75
    public DefaultComboBoxModel(Vector<E> v) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        objects = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Set the value of the selected item. The selected item may be null.
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 17898
diff changeset
    86
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @param anObject The combo box value or null for no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void setSelectedItem(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if ((selectedObject != null && !selectedObject.equals( anObject )) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            selectedObject == null && anObject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            selectedObject = anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            fireContentsChanged(this, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public Object getSelectedItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // implements javax.swing.ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return objects.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    // implements javax.swing.ListModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   108
    public E getElementAt(int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if ( index >= 0 && index < objects.size() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            return objects.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Returns the index-position of the specified object in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * @param anObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @return an int representing the index position, where 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *         the first position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public int getIndexOf(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    // implements javax.swing.MutableComboBoxModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   127
    public void addElement(E anObject) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        objects.addElement(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        fireIntervalAdded(this,objects.size()-1, objects.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            setSelectedItem( anObject );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // implements javax.swing.MutableComboBoxModel
9665
f43db0b310cc 7031551: Generics: JComboBox
rupashka
parents: 5506
diff changeset
   136
    public void insertElementAt(E anObject,int index) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        objects.insertElementAt(anObject,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        fireIntervalAdded(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public void removeElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if ( getElementAt( index ) == selectedObject ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if ( index == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                setSelectedItem( getElementAt( index - 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        objects.removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        fireIntervalRemoved(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void removeElement(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int index = objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        if ( index != -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
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
     * Empties the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void removeAllElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ( objects.size() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            int firstIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int lastIndex = objects.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            objects.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            fireIntervalRemoved(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
}