author | darcy |
Fri, 10 Jan 2014 09:24:47 -0800 | |
changeset 22260 | c9185e010e03 |
parent 21982 | fd6e5fe509df |
child 23697 | e556a715949f |
permissions | -rw-r--r-- |
2 | 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 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 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 |
* |
|
9665 | 34 |
* @param <E> the type of the elements of this model |
35 |
* |
|
2 | 36 |
* @author Arnaud Weber |
37 |
* @author Tom Santos |
|
38 |
*/ |
|
39 |
||
9665 | 40 |
public class DefaultComboBoxModel<E> extends AbstractListModel<E> implements MutableComboBoxModel<E>, Serializable { |
41 |
Vector<E> objects; |
|
2 | 42 |
Object selectedObject; |
43 |
||
44 |
/** |
|
45 |
* Constructs an empty DefaultComboBoxModel object. |
|
46 |
*/ |
|
47 |
public DefaultComboBoxModel() { |
|
9665 | 48 |
objects = new Vector<E>(); |
2 | 49 |
} |
50 |
||
51 |
/** |
|
52 |
* Constructs a DefaultComboBoxModel object initialized with |
|
53 |
* an array of objects. |
|
54 |
* |
|
55 |
* @param items an array of Object objects |
|
56 |
*/ |
|
9665 | 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 | 59 |
|
60 |
int i,c; |
|
61 |
for ( i=0,c=items.length;i<c;i++ ) |
|
62 |
objects.addElement(items[i]); |
|
63 |
||
64 |
if ( getSize() > 0 ) { |
|
65 |
selectedObject = getElementAt( 0 ); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Constructs a DefaultComboBoxModel object initialized with |
|
71 |
* a vector. |
|
72 |
* |
|
73 |
* @param v a Vector object ... |
|
74 |
*/ |
|
9665 | 75 |
public DefaultComboBoxModel(Vector<E> v) { |
2 | 76 |
objects = v; |
77 |
||
78 |
if ( getSize() > 0 ) { |
|
79 |
selectedObject = getElementAt( 0 ); |
|
80 |
} |
|
81 |
} |
|
82 |
||
83 |
// implements javax.swing.ComboBoxModel |
|
84 |
/** |
|
85 |
* Set the value of the selected item. The selected item may be null. |
|
21982 | 86 |
* |
2 | 87 |
* @param anObject The combo box value or null for no selection. |
88 |
*/ |
|
89 |
public void setSelectedItem(Object anObject) { |
|
90 |
if ((selectedObject != null && !selectedObject.equals( anObject )) || |
|
91 |
selectedObject == null && anObject != null) { |
|
92 |
selectedObject = anObject; |
|
93 |
fireContentsChanged(this, -1, -1); |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
// implements javax.swing.ComboBoxModel |
|
98 |
public Object getSelectedItem() { |
|
99 |
return selectedObject; |
|
100 |
} |
|
101 |
||
102 |
// implements javax.swing.ListModel |
|
103 |
public int getSize() { |
|
104 |
return objects.size(); |
|
105 |
} |
|
106 |
||
107 |
// implements javax.swing.ListModel |
|
9665 | 108 |
public E getElementAt(int index) { |
2 | 109 |
if ( index >= 0 && index < objects.size() ) |
110 |
return objects.elementAt(index); |
|
111 |
else |
|
112 |
return null; |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Returns the index-position of the specified object in the list. |
|
117 |
* |
|
22260
c9185e010e03
8031082: Fix non-missing doclint problems in client libraries
darcy
parents:
21982
diff
changeset
|
118 |
* @param anObject the object to return the index of |
2 | 119 |
* @return an int representing the index position, where 0 is |
120 |
* the first position |
|
121 |
*/ |
|
122 |
public int getIndexOf(Object anObject) { |
|
123 |
return objects.indexOf(anObject); |
|
124 |
} |
|
125 |
||
126 |
// implements javax.swing.MutableComboBoxModel |
|
9665 | 127 |
public void addElement(E anObject) { |
2 | 128 |
objects.addElement(anObject); |
129 |
fireIntervalAdded(this,objects.size()-1, objects.size()-1); |
|
130 |
if ( objects.size() == 1 && selectedObject == null && anObject != null ) { |
|
131 |
setSelectedItem( anObject ); |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
// implements javax.swing.MutableComboBoxModel |
|
9665 | 136 |
public void insertElementAt(E anObject,int index) { |
2 | 137 |
objects.insertElementAt(anObject,index); |
138 |
fireIntervalAdded(this, index, index); |
|
139 |
} |
|
140 |
||
141 |
// implements javax.swing.MutableComboBoxModel |
|
142 |
public void removeElementAt(int index) { |
|
143 |
if ( getElementAt( index ) == selectedObject ) { |
|
144 |
if ( index == 0 ) { |
|
145 |
setSelectedItem( getSize() == 1 ? null : getElementAt( index + 1 ) ); |
|
146 |
} |
|
147 |
else { |
|
148 |
setSelectedItem( getElementAt( index - 1 ) ); |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
objects.removeElementAt(index); |
|
153 |
||
154 |
fireIntervalRemoved(this, index, index); |
|
155 |
} |
|
156 |
||
157 |
// implements javax.swing.MutableComboBoxModel |
|
158 |
public void removeElement(Object anObject) { |
|
159 |
int index = objects.indexOf(anObject); |
|
160 |
if ( index != -1 ) { |
|
161 |
removeElementAt(index); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Empties the list. |
|
167 |
*/ |
|
168 |
public void removeAllElements() { |
|
169 |
if ( objects.size() > 0 ) { |
|
170 |
int firstIndex = 0; |
|
171 |
int lastIndex = objects.size() - 1; |
|
172 |
objects.removeAllElements(); |
|
173 |
selectedObject = null; |
|
174 |
fireIntervalRemoved(this, firstIndex, lastIndex); |
|
175 |
} else { |
|
176 |
selectedObject = null; |
|
177 |
} |
|
178 |
} |
|
179 |
} |