summaryrefslogtreecommitdiff
path: root/javax/swing/DefaultComboBoxModel.java
blob: 496551e00fc36c0df2ef4160c0ddbb7f193627c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/* DefaultComboBoxModel.java --
   Copyright (C) 2002, 2004 Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package javax.swing;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Vector;


/**
 * DefaultComboBoxModel is a data model for JComboBox. This model keeps track
 * of elements contained in the JComboBox as well as the current combo box
 * selection. Whenever selection in the JComboBox changes, the ComboBoxModel
 * will fire ListDataEvents to ComboBox's ListDataListeners.
 *
 * @author Andrew Selkirk
 * @author Olga Rodimina
 * @author Robert Schuster
 * @version 1.0
 */
public class DefaultComboBoxModel extends AbstractListModel
  implements MutableComboBoxModel, Serializable
{
  private static final long serialVersionUID = 6698657703676921904L;

  /**
   * List containing items in the combo box
   */
  private Vector list;

  /**
   * Currently selected item in the combo box list
   */
  private Object selectedItem = null;

  /**
   * Constructor DefaultComboBoxModel. Create empty JComboBox.
   */
  public DefaultComboBoxModel()
  {
    list = new Vector();
  }

  /**
   * Constructs new DefaultComboBoxModel object and initializes its item list
   * to values in the given array.
   *
   * @param items array containing items of the combo box.
   */
  public DefaultComboBoxModel(Object[] items)
  {
    list = new Vector(Arrays.asList(items));
  }

  /**
   * Consturcts new DefaultComboBoxModel object and initializes its item list
   * to values in the given vector.
   *
   * @param vector Vector containing items for this combo box.
   */
  public DefaultComboBoxModel(Vector vector)
  {
    this.list = vector;
  }

  /**
   * This method adds element to the combo box list. It fires ListDataEvent
   * indicating that component was added to the combo box  to all of the
   * JComboBox's registered ListDataListeners.
   *
   * @param object item to add to the combo box list
   */
  public void addElement(Object object)
  {
    list.add(object);
    fireIntervalAdded(this, list.size() - 1, list.size());
  }

  /**
   * This method removes element at the specified index from the combo box
   * list. It fires ListDataEvent indicating that component was removed from
   * the combo box list to all of the JComboBox's registered
   * ListDataListeners.
   *
   * @param index index specifying location of the element to remove in the
   *        combo box list.
   */
  public void removeElementAt(int index)
  {
    list.remove(index);
    fireIntervalRemoved(this, index, index);
  }

  /**
   * This method inserts given object to the combo box list at the specified
   * index. It fires ListDataEvent indicating that component was inserted to
   * the combo box list to all of the JComboBox's registered
   * ListDataListeners.
   *
   * @param object element to insert
   * @param index index specifing position in the list where given element
   *        should be inserted.
   */
  public void insertElementAt(Object object, int index)
  {
    list.insertElementAt(object, index);
    fireIntervalAdded(this, index, index);
  }

  /**
   * Removes given object from the combo box list. It fires ListDataEvent
   * indicating that component was removed from the combo box list to all of
   * the JComboBox's registered ListDataListeners.
   *
   * @param object Element that will be removed from the combo box list
   */
  public void removeElement(Object object)
  {
    int index = getIndexOf(object);
    if (index != -1)
      removeElementAt(index);
  }

  /**
   * Removes all the items from the JComboBox's item list. It fires
   * ListDataEvent indicating that all the elements were removed from the
   * combo box list to all of the JComboBox's registered ListDataListeners.
   */
  public void removeAllElements()
  {
    list.clear();
    int listSize = getSize();
    fireIntervalAdded(this, 0, listSize);
  }

  /**
   * Returns number of items in the combo box list
   *
   * @return number of items in the combo box list
   */
  public int getSize()
  {
    return list.size();
  }

  /**
   * Selects given object in the combo box list. This method fires
   * ListDataEvent to all registered ListDataListeners of the JComboBox. The
   * start and end index of the event is set to -1 to indicate combo box's
   * selection has changed, and not its contents.
   * 
   * <p>If the given object is not contained in the combo box list then nothing
   * happens.</p>
   *
   * @param object item to select in the JComboBox
   */
  public void setSelectedItem(Object object)
  {
    
    /* Updates the selected item only if the given object
     * is null or in the list (this is how the JDK behaves).
     */ 
    if(object == null || list.contains(object)) {
	selectedItem = object;
	fireContentsChanged(this, -1, -1);
    }
  	
  }

  /**
   * Returns currently selected item in the combo box list
   *
   * @return currently selected item in the combo box list
   */
  public Object getSelectedItem()
  {
    return selectedItem;
  }

  /**
   * Returns element in the combo box list located at the given index
   *
   * @param index specifying location of the element in the list
   *
   * @return return element in the combo box list located at the given index
   */
  public Object getElementAt(int index)
  {
    return list.elementAt(index);
  }

  /**
   * Returns index of the specified object in the combo box list.
   *
   * @param object element to look for in the combo box list .
   *
   * @return Index specifying position of the specified element in combo box
   *         list.
   */
  public int getIndexOf(Object object)
  {
    return list.indexOf(object);
  }
}