Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / javax / swing / DefaultComboBoxModel.java
blobc2ece643551afbef0a86aa6e1b04a17f732e3969
1 /* DefaultComboBoxModel.java --
2 Copyright (C) 2002, 2004 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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;
45 /**
46 * DefaultComboBoxModel is a data model for JComboBox. This model keeps track
47 * of elements contained in the JComboBox as well as the current combo box
48 * selection. Whenever selection in the JComboBox changes, the ComboBoxModel
49 * will fire ListDataEvents to ComboBox's ListDataListeners.
51 * @author Andrew Selkirk
52 * @author Olga Rodimina
53 * @author Robert Schuster
54 * @version 1.0
56 public class DefaultComboBoxModel extends AbstractListModel
57 implements MutableComboBoxModel, Serializable
59 private static final long serialVersionUID = 6698657703676921904L;
61 /**
62 * List containing items in the combo box
64 private Vector list;
66 /**
67 * Currently selected item in the combo box list
69 private Object selectedItem = null;
71 /**
72 * Constructor DefaultComboBoxModel. Create empty JComboBox.
74 public DefaultComboBoxModel()
76 list = new Vector();
79 /**
80 * Constructs new DefaultComboBoxModel object and initializes its item list
81 * to values in the given array.
83 * @param items array containing items of the combo box.
85 public DefaultComboBoxModel(Object[] items)
87 list = new Vector(Arrays.asList(items));
90 /**
91 * Consturcts new DefaultComboBoxModel object and initializes its item list
92 * to values in the given vector.
94 * @param vector Vector containing items for this combo box.
96 public DefaultComboBoxModel(Vector vector)
98 this.list = vector;
102 * This method adds element to the combo box list. It fires ListDataEvent
103 * indicating that component was added to the combo box to all of the
104 * JComboBox's registered ListDataListeners.
106 * @param object item to add to the combo box list
108 public void addElement(Object object)
110 list.add(object);
111 fireIntervalAdded(this, list.size(), list.size());
115 * This method removes element at the specified index from the combo box
116 * list. It fires ListDataEvent indicating that component was removed from
117 * the combo box list to all of the JComboBox's registered
118 * ListDataListeners.
120 * @param index index specifying location of the element to remove in the
121 * combo box list.
123 public void removeElementAt(int index)
125 list.remove(index);
126 fireIntervalRemoved(this, index, index);
130 * This method inserts given object to the combo box list at the specified
131 * index. It fires ListDataEvent indicating that component was inserted to
132 * the combo box list to all of the JComboBox's registered
133 * ListDataListeners.
135 * @param object element to insert
136 * @param index index specifing position in the list where given element
137 * should be inserted.
139 public void insertElementAt(Object object, int index)
141 list.insertElementAt(object, index);
142 fireIntervalAdded(this, index, index);
146 * Removes given object from the combo box list. It fires ListDataEvent
147 * indicating that component was removed from the combo box list to all of
148 * the JComboBox's registered ListDataListeners.
150 * @param object Element that will be removed from the combo box list
152 public void removeElement(Object object)
154 int index = getIndexOf(object);
155 if (index != -1)
156 removeElementAt(index);
160 * Removes all the items from the JComboBox's item list. It fires
161 * ListDataEvent indicating that all the elements were removed from the
162 * combo box list to all of the JComboBox's registered ListDataListeners.
164 public void removeAllElements()
166 int listSize = getSize();
167 list.clear();
168 fireIntervalAdded(this, 0, listSize - 1);
172 * Returns number of items in the combo box list
174 * @return number of items in the combo box list
176 public int getSize()
178 return list.size();
182 * Selects given object in the combo box list. This method fires
183 * ListDataEvent to all registered ListDataListeners of the JComboBox. The
184 * start and end index of the event is set to -1 to indicate combo box's
185 * selection has changed, and not its contents.
187 * <p>If the given object is not contained in the combo box list then nothing
188 * happens.</p>
190 * @param object item to select in the JComboBox
192 public void setSelectedItem(Object object)
195 /* Updates the selected item only if the given object
196 * is null or in the list (this is how the JDK behaves).
198 if(object == null || list.contains(object)) {
199 selectedItem = object;
200 fireContentsChanged(this, -1, -1);
206 * Returns currently selected item in the combo box list
208 * @return currently selected item in the combo box list
210 public Object getSelectedItem()
212 return selectedItem;
216 * Returns element in the combo box list located at the given index
218 * @param index specifying location of the element in the list
220 * @return return element in the combo box list located at the given index
222 public Object getElementAt(int index)
224 return list.elementAt(index);
228 * Returns index of the specified object in the combo box list.
230 * @param object element to look for in the combo box list .
232 * @return Index specifying position of the specified element in combo box
233 * list.
235 public int getIndexOf(Object object)
237 return list.indexOf(object);