jdk/src/share/classes/javax/swing/DefaultComboBoxModel.java
author idk
Mon, 23 Jun 2008 15:21:37 -0400
changeset 1273 3b2eba521268
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6623943: javax.swing.TimerQueue's thread occasionally fails to start Reviewed-by: alexp
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2004 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.beans.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.border.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The default model for combo boxes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @author Arnaud Weber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author Tom Santos
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public class DefaultComboBoxModel extends AbstractListModel implements MutableComboBoxModel, Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    Vector objects;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    Object selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Constructs an empty DefaultComboBoxModel object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public DefaultComboBoxModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        objects = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Constructs a DefaultComboBoxModel object initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * an array of objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param items  an array of Object objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public DefaultComboBoxModel(final Object items[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        objects = new Vector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        objects.ensureCapacity( items.length );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        int i,c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        for ( i=0,c=items.length;i<c;i++ )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            objects.addElement(items[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Constructs a DefaultComboBoxModel object initialized with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * a vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param v  a Vector object ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public DefaultComboBoxModel(Vector<?> v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        objects = v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if ( getSize() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            selectedObject = getElementAt( 0 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Set the value of the selected item. The selected item may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param anObject The combo box value or null for no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public void setSelectedItem(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((selectedObject != null && !selectedObject.equals( anObject )) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            selectedObject == null && anObject != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            selectedObject = anObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            fireContentsChanged(this, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // implements javax.swing.ComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public Object getSelectedItem() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return selectedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    // implements javax.swing.ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return objects.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    // implements javax.swing.ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Object getElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if ( index >= 0 && index < objects.size() )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            return objects.elementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Returns the index-position of the specified object in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @param anObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return an int representing the index position, where 0 is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *         the first position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public int getIndexOf(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public void addElement(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        objects.addElement(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        fireIntervalAdded(this,objects.size()-1, objects.size()-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if ( objects.size() == 1 && selectedObject == null && anObject != null ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            setSelectedItem( anObject );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    public void insertElementAt(Object anObject,int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        objects.insertElementAt(anObject,index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        fireIntervalAdded(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void removeElementAt(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if ( getElementAt( index ) == selectedObject ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            if ( index == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                setSelectedItem( getElementAt( index - 1 ) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        objects.removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        fireIntervalRemoved(this, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    // implements javax.swing.MutableComboBoxModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public void removeElement(Object anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        int index = objects.indexOf(anObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if ( index != -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            removeElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Empties the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public void removeAllElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if ( objects.size() > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            int firstIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            int lastIndex = objects.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            objects.removeAllElements();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            fireIntervalRemoved(this, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            selectedObject = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
}