Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / swing / DefaultComboBoxModel.java
blobab80b61f1f94337e6714151df71adbe222250934
1 /* DefaultComboBoxModel.java --
2 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package javax.swing;
40 import java.io.Serializable;
41 import java.util.Arrays;
42 import java.util.Vector;
44 import javax.swing.event.ListDataEvent;
47 /**
48 * A model that stores a list of elements and a selected item (which may be
49 * <code>null</code>). Changes to the model are signalled to listeners using
50 * {@link ListDataEvent}. This model is designed for use by the
51 * {@link JComboBox} component.
53 * @author Andrew Selkirk
54 * @author Olga Rodimina
55 * @author Robert Schuster
57 public class DefaultComboBoxModel extends AbstractListModel
58 implements MutableComboBoxModel, Serializable
60 private static final long serialVersionUID = 6698657703676921904L;
62 /**
63 * Storage for the elements in the model's list.
65 private Vector list;
67 /**
68 * The selected item (<code>null</code> indicates no selection).
70 private Object selectedItem = null;
72 /**
73 * Creates a new model, initially empty.
75 public DefaultComboBoxModel()
77 list = new Vector();
80 /**
81 * Creates a new model and initializes its item list to the values in the
82 * 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.
85 * @param items an array containing items for the model (<code>null</code>
86 * not permitted).
88 * @throws NullPointerException if <code>items</code> is <code>null</code>.
90 public DefaultComboBoxModel(Object[] items)
92 list = new Vector(Arrays.asList(items));
93 if (list.size() > 0)
94 selectedItem = list.get(0);
97 /**
98 * Creates a new model and initializes its item list to the values in the
99 * 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.
102 * @param vector a vector containing items for the model (<code>null</code>
103 * not permitted).
105 * @throws NullPointerException if <code>vector</code> is <code>null</code>.
107 public DefaultComboBoxModel(Vector vector)
109 this.list = vector;
110 if (getSize() > 0)
111 selectedItem = vector.get(0);
115 * Adds an element to the model's item list and sends a {@link ListDataEvent}
116 * to all registered listeners. If the new element is the first item added
117 * to the list, and the selected item is <code>null</code>, the new element
118 * is set as the selected item.
120 * @param object item to add to the model's item list.
122 public void addElement(Object object)
124 list.addElement(object);
125 int index = list.size() - 1;
126 fireIntervalAdded(this, index, index);
127 if (list.size() == 1 && selectedItem == null)
128 setSelectedItem(object);
132 * Removes the element at the specified index from the model's item list
133 * and sends a {@link ListDataEvent} to all registered listeners. If the
134 * element removed was the selected item, then the preceding element becomes
135 * the new selected item (or the next element, if there is no preceding
136 * element).
138 * @param index the index of the item to remove.
140 * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
141 * bounds.
143 public void removeElementAt(int index)
145 int selected = getIndexOf(selectedItem);
146 if (selected == index) // choose a new selected item
148 if (selected > 0)
149 selectedItem = getElementAt(selected - 1);
150 else
151 selectedItem = getElementAt(selected + 1);
153 list.removeElementAt(index);
154 fireIntervalRemoved(this, index, index);
158 * Adds an element at the specified index in the model's item list
159 * and sends a {@link ListDataEvent} to all registered listeners.
161 * @param object element to insert
162 * @param index index specifing position in the list where given element
163 * should be inserted.
165 * @throws ArrayIndexOutOfBoundsException if <code>index</code> is out of
166 * bounds.
168 * @see #addElement(Object)
170 public void insertElementAt(Object object, int index)
172 list.insertElementAt(object, index);
173 fireIntervalAdded(this, index, index);
177 * Removes an element from the model's item list and sends a
178 * {@link ListDataEvent} to all registered listeners. If the item to be
179 * removed is the current selected item, a new selected item will be set.
180 * If the element is not found in the model's item list, this method does
181 * nothing.
183 * @param object the element to remove.
185 public void removeElement(Object object)
187 int index = getIndexOf(object);
188 if (index != -1)
189 removeElementAt(index);
193 * Removes all the items from the model's item list, resets and selected item
194 * to <code>null</code>, and sends a {@link ListDataEvent} to all registered
195 * listeners.
197 public void removeAllElements()
199 selectedItem = null;
200 int size = getSize();
201 if (size > 0)
203 list.clear();
204 fireIntervalRemoved(this, 0, size - 1);
209 * Returns the number of items in the model's item list.
211 * @return The number of items in the model's item list.
213 public int getSize()
215 return list.size();
219 * Sets the selected item for the model and sends a {@link ListDataEvent} to
220 * all registered listeners. The start and end index of the event is set to
221 * -1 to indicate the model's selection has changed, and not its contents.
223 * @param object the new selected item (<code>null</code> permitted).
225 public void setSelectedItem(Object object)
227 if (selectedItem == null)
229 if (object == null)
230 return;
232 else
234 if (selectedItem.equals(object))
235 return;
237 selectedItem = object;
238 fireContentsChanged(this, -1, -1);
242 * Returns the selected item.
244 * @return The selected item (possibly <code>null</code>).
246 public Object getSelectedItem()
248 return selectedItem;
252 * Returns the element at the specified index in the model's item list.
254 * @param index the element index.
256 * @return The element at the specified index in the model's item list, or
257 * <code>null</code> if the <code>index</code> is outside the bounds
258 * of the list.
260 public Object getElementAt(int index)
262 if (index < 0 || index >= list.size())
263 return null;
264 return list.elementAt(index);
268 * Returns the index of the specified element in the model's item list.
270 * @param object the element.
272 * @return The index of the specified element in the model's item list.
274 public int getIndexOf(Object object)
276 return list.indexOf(object);