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