1 |
/* DefaultComboBoxModel.java -- |
/* DefaultComboBoxModel.java -- |
2 |
Copyright (C) 2002, 2004 Free Software Foundation, Inc. |
Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc. |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
41 |
import java.util.Arrays; |
import java.util.Arrays; |
42 |
import java.util.Vector; |
import java.util.Vector; |
43 |
|
|
44 |
|
import javax.swing.event.ListDataEvent; |
45 |
|
|
46 |
|
|
47 |
/** |
/** |
48 |
* The default implementation of {@link MutableComboBoxModel}. |
* A model that stores a list of elements and a selected item (which may be |
49 |
* This model keeps track |
* <code>null</code>). Changes to the model are signalled to listeners using |
50 |
* of elements contained in the JComboBox as well as the current combo box |
* {@link ListDataEvent}. This model is designed for use by the |
51 |
* selection. Whenever selection in the JComboBox changes, the ComboBoxModel |
* {@link JComboBox} component. |
|
* will fire ListDataEvents to ComboBox's ListDataListeners. |
|
52 |
* |
* |
53 |
* @author Andrew Selkirk |
* @author Andrew Selkirk |
54 |
* @author Olga Rodimina |
* @author Olga Rodimina |
60 |
private static final long serialVersionUID = 6698657703676921904L; |
private static final long serialVersionUID = 6698657703676921904L; |
61 |
|
|
62 |
/** |
/** |
63 |
* List containing items in the combo box |
* Storage for the elements in the model's list. |
64 |
*/ |
*/ |
65 |
private Vector list; |
private Vector list; |
66 |
|
|
67 |
/** |
/** |
68 |
* Currently selected item in the combo box list |
* The selected item (<code>null</code> indicates no selection). |
69 |
*/ |
*/ |
70 |
private Object selectedItem = null; |
private Object selectedItem = null; |
71 |
|
|
72 |
/** |
/** |
73 |
* Constructor DefaultComboBoxModel. Create empty JComboBox. |
* Creates a new model, initially empty. |
74 |
*/ |
*/ |
75 |
public DefaultComboBoxModel() |
public DefaultComboBoxModel() |
76 |
{ |
{ |
78 |
} |
} |
79 |
|
|
80 |
/** |
/** |
81 |
* Constructs new DefaultComboBoxModel object and initializes its item list |
* Creates a new model and initializes its item list to the values in the |
82 |
* to values in the given array. |
* given array. The selected item is set to the first item in the array, or |
83 |
|
* <code>null</code> if the array length is zero. |
84 |
* |
* |
85 |
* @param items array containing items of the combo box. |
* @param items an array containing items for the model (<code>null</code> |
86 |
|
* not permitted). |
87 |
|
* |
88 |
|
* @throws NullPointerException if <code>items</code> is <code>null</code>. |
89 |
*/ |
*/ |
90 |
public DefaultComboBoxModel(Object[] items) |
public DefaultComboBoxModel(Object[] items) |
91 |
{ |
{ |
95 |
} |
} |
96 |
|
|
97 |
/** |
/** |
98 |
* Consturcts new DefaultComboBoxModel object and initializes its item list |
* Creates a new model and initializes its item list to the values in the |
99 |
* to values in the given vector. |
* given vector. The selected item is set to the first item in the vector, |
100 |
|
* or <code>null</code> if the vector length is zero. |
101 |
* |
* |
102 |
* @param vector Vector containing items for this combo box. |
* @param vector a vector containing items for the model (<code>null</code> |
103 |
|
* not permitted). |
104 |
|
* |
105 |
|
* @throws NullPointerException if <code>vector</code> is <code>null</code>. |
106 |
*/ |
*/ |
107 |
public DefaultComboBoxModel(Vector vector) |
public DefaultComboBoxModel(Vector vector) |
108 |
{ |
{ |
112 |
} |
} |
113 |
|
|
114 |
/** |
/** |
115 |
* This method adds element to the combo box list. It fires ListDataEvent |
* Adds an element to the model's item list and sends a {@link ListDataEvent} |
116 |
* indicating that component was added to the combo box to all of the |
* to all registered listeners. If the item list is empty, the new element |
117 |
* JComboBox's registered ListDataListeners. |
* also becomes the model's selected item. |
118 |
* |
* |
119 |
* @param object item to add to the combo box list |
* @param object item to add to the model's item list. |
120 |
*/ |
*/ |
121 |
public void addElement(Object object) |
public void addElement(Object object) |
122 |
{ |
{ |
134 |
} |
} |
135 |
|
|
136 |
/** |
/** |
137 |
* This method removes element at the specified index from the combo box |
* Removes the element at the specified index from the model's item list |
138 |
* list. It fires ListDataEvent indicating that component was removed from |
* and sends a {@link ListDataEvent} to all registered listeners. If the |
139 |
* the combo box list to all of the JComboBox's registered |
* element removed was the selected item, then the preceding element becomes |
140 |
* ListDataListeners. |
* the new selected item (or the next element, if there is no preceding |
141 |
|
* element). |
142 |
* |
* |
143 |
* @param index index specifying location of the element to remove in the |
* @param index the index of the item to remove. |
144 |
* combo box list. |
* |
145 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
146 |
|
* bounds. |
147 |
*/ |
*/ |
148 |
public void removeElementAt(int index) |
public void removeElementAt(int index) |
149 |
{ |
{ |
155 |
selectedItem = getElementAt(selected - 1); |
selectedItem = getElementAt(selected - 1); |
156 |
else |
else |
157 |
selectedItem = getElementAt(selected); |
selectedItem = getElementAt(selected); |
|
|
|
|
|
|
158 |
} |
} |
159 |
fireIntervalRemoved(this, index, index); |
fireIntervalRemoved(this, index, index); |
160 |
} |
} |
161 |
|
|
162 |
/** |
/** |
163 |
* This method inserts given object to the combo box list at the specified |
* Adds an element at the specified index in the model's item list |
164 |
* index. It fires ListDataEvent indicating that component was inserted to |
* and sends a {@link ListDataEvent} to all registered listeners. |
|
* the combo box list to all of the JComboBox's registered |
|
|
* ListDataListeners. |
|
165 |
* |
* |
166 |
* @param object element to insert |
* @param object element to insert |
167 |
* @param index index specifing position in the list where given element |
* @param index index specifing position in the list where given element |
168 |
* should be inserted. |
* should be inserted. |
169 |
|
* |
170 |
|
* @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of |
171 |
|
* bounds. |
172 |
|
* |
173 |
|
* @see #addElement(Object) |
174 |
*/ |
*/ |
175 |
public void insertElementAt(Object object, int index) |
public void insertElementAt(Object object, int index) |
176 |
{ |
{ |
179 |
} |
} |
180 |
|
|
181 |
/** |
/** |
182 |
* Removes given object from the combo box list. It fires ListDataEvent |
* Removes an element from the model's item list and sends a |
183 |
* indicating that component was removed from the combo box list to all of |
* {@link ListDataEvent} to all registered listeners. If the item to be |
184 |
* the JComboBox's registered ListDataListeners. |
* removed is the current selected item, a new selected item will be set. |
185 |
|
* If the element is not found in the model's item list, this method does |
186 |
|
* nothing. |
187 |
* |
* |
188 |
* @param object Element that will be removed from the combo box list |
* @param object the element to remove. |
189 |
*/ |
*/ |
190 |
public void removeElement(Object object) |
public void removeElement(Object object) |
191 |
{ |
{ |
195 |
} |
} |
196 |
|
|
197 |
/** |
/** |
198 |
* Removes all the items from the JComboBox's item list. It fires |
* Removes all the items from the model's item list, resets and selected item |
199 |
* ListDataEvent indicating that all the elements were removed from the |
* to <code>null</code>, and sends a {@link ListDataEvent} to all registered |
200 |
* combo box list to all of the JComboBox's registered ListDataListeners. |
* listeners. |
201 |
*/ |
*/ |
202 |
public void removeAllElements() |
public void removeAllElements() |
203 |
{ |
{ |
211 |
} |
} |
212 |
|
|
213 |
/** |
/** |
214 |
* Returns number of items in the combo box list |
* Returns the number of items in the model's item list. |
215 |
* |
* |
216 |
* @return number of items in the combo box list |
* @return The number of items in the model's item list. |
217 |
*/ |
*/ |
218 |
public int getSize() |
public int getSize() |
219 |
{ |
{ |
221 |
} |
} |
222 |
|
|
223 |
/** |
/** |
224 |
* Selects given object in the combo box list. This method fires |
* Sets the selected item for the model and sends a {@link ListDataEvent} to |
225 |
* ListDataEvent to all registered ListDataListeners of the JComboBox. The |
* all registered listeners. The start and end index of the event is set to |
226 |
* start and end index of the event is set to -1 to indicate combo box's |
* -1 to indicate the model's selection has changed, and not its contents. |
|
* selection has changed, and not its contents. |
|
|
* |
|
|
* <p>If the given object is not contained in the combo box list then nothing |
|
|
* happens.</p> |
|
227 |
* |
* |
228 |
* @param object item to select in the JComboBox |
* @param object the new selected item (<code>null</code> permitted). |
229 |
*/ |
*/ |
230 |
public void setSelectedItem(Object object) |
public void setSelectedItem(Object object) |
231 |
{ |
{ |
235 |
} |
} |
236 |
|
|
237 |
/** |
/** |
238 |
* Returns currently selected item in the combo box list |
* Returns the selected item. |
239 |
* |
* |
240 |
* @return currently selected item in the combo box list |
* @return The selected item (possibly <code>null</code>). |
241 |
*/ |
*/ |
242 |
public Object getSelectedItem() |
public Object getSelectedItem() |
243 |
{ |
{ |
245 |
} |
} |
246 |
|
|
247 |
/** |
/** |
248 |
* Returns element in the combo box list located at the given index |
* Returns the element at the specified index in the model's item list. |
249 |
* |
* |
250 |
* @param index specifying location of the element in the list |
* @param index the element index. |
251 |
* |
* |
252 |
* @return return element in the combo box list located at the given index |
* @return The element at the specified index in the model's item list, or |
253 |
|
* <code>null</code> if the <code>index</code> is outside the bounds |
254 |
|
* of the list. |
255 |
*/ |
*/ |
256 |
public Object getElementAt(int index) |
public Object getElementAt(int index) |
257 |
{ |
{ |
261 |
} |
} |
262 |
|
|
263 |
/** |
/** |
264 |
* Returns index of the specified object in the combo box list. |
* Returns the index of the specified element in the model's item list. |
265 |
* |
* |
266 |
* @param object element to look for in the combo box list . |
* @param object the element. |
267 |
* |
* |
268 |
* @return Index specifying position of the specified element in combo box |
* @return The index of the specified element in the model's item list. |
|
* list. |
|
269 |
*/ |
*/ |
270 |
public int getIndexOf(Object object) |
public int getIndexOf(Object object) |
271 |
{ |
{ |