blob: 01a9f2066044c8b3295be4bc5cba79dd3c9c91db [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
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
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
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 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing;
27
28import java.util.Vector;
29import java.util.Enumeration;
30
31import javax.swing.event.*;
32
33
34/**
35 * This class loosely implements the <code>java.util.Vector</code>
36 * API, in that it implements the 1.1.x version of
37 * <code>java.util.Vector</code>, has no collection class support,
38 * and notifies the <code>ListDataListener</code>s when changes occur.
39 * Presently it delegates to a <code>Vector</code>,
40 * in a future release it will be a real Collection implementation.
41 * <p>
42 * <strong>Warning:</strong>
43 * Serialized objects of this class will not be compatible with
44 * future Swing releases. The current serialization support is
45 * appropriate for short term storage or RMI between applications running
46 * the same version of Swing. As of 1.4, support for long term storage
47 * of all JavaBeans<sup><font size="-2">TM</font></sup>
48 * has been added to the <code>java.beans</code> package.
49 * Please see {@link java.beans.XMLEncoder}.
50 *
51 * @author Hans Muller
52 */
53public class DefaultListModel extends AbstractListModel
54{
55 private Vector delegate = new Vector();
56
57 /**
58 * Returns the number of components in this list.
59 * <p>
60 * This method is identical to <code>size</code>, which implements the
61 * <code>List</code> interface defined in the 1.2 Collections framework.
62 * This method exists in conjunction with <code>setSize</code> so that
63 * <code>size</code> is identifiable as a JavaBean property.
64 *
65 * @return the number of components in this list
66 * @see #size()
67 */
68 public int getSize() {
69 return delegate.size();
70 }
71
72 /**
73 * Returns the component at the specified index.
74 * <blockquote>
75 * <b>Note:</b> Although this method is not deprecated, the preferred
76 * method to use is <code>get(int)</code>, which implements the
77 * <code>List</code> interface defined in the 1.2 Collections framework.
78 * </blockquote>
79 * @param index an index into this list
80 * @return the component at the specified index
81 * @exception ArrayIndexOutOfBoundsException if the <code>index</code>
82 * is negative or greater than the current size of this
83 * list
84 * @see #get(int)
85 */
86 public Object getElementAt(int index) {
87 return delegate.elementAt(index);
88 }
89
90 /**
91 * Copies the components of this list into the specified array.
92 * The array must be big enough to hold all the objects in this list,
93 * else an <code>IndexOutOfBoundsException</code> is thrown.
94 *
95 * @param anArray the array into which the components get copied
96 * @see Vector#copyInto(Object[])
97 */
98 public void copyInto(Object anArray[]) {
99 delegate.copyInto(anArray);
100 }
101
102 /**
103 * Trims the capacity of this list to be the list's current size.
104 *
105 * @see Vector#trimToSize()
106 */
107 public void trimToSize() {
108 delegate.trimToSize();
109 }
110
111 /**
112 * Increases the capacity of this list, if necessary, to ensure
113 * that it can hold at least the number of components specified by
114 * the minimum capacity argument.
115 *
116 * @param minCapacity the desired minimum capacity
117 * @see Vector#ensureCapacity(int)
118 */
119 public void ensureCapacity(int minCapacity) {
120 delegate.ensureCapacity(minCapacity);
121 }
122
123 /**
124 * Sets the size of this list.
125 *
126 * @param newSize the new size of this list
127 * @see Vector#setSize(int)
128 */
129 public void setSize(int newSize) {
130 int oldSize = delegate.size();
131 delegate.setSize(newSize);
132 if (oldSize > newSize) {
133 fireIntervalRemoved(this, newSize, oldSize-1);
134 }
135 else if (oldSize < newSize) {
136 fireIntervalAdded(this, oldSize, newSize-1);
137 }
138 }
139
140 /**
141 * Returns the current capacity of this list.
142 *
143 * @return the current capacity
144 * @see Vector#capacity()
145 */
146 public int capacity() {
147 return delegate.capacity();
148 }
149
150 /**
151 * Returns the number of components in this list.
152 *
153 * @return the number of components in this list
154 * @see Vector#size()
155 */
156 public int size() {
157 return delegate.size();
158 }
159
160 /**
161 * Tests whether this list has any components.
162 *
163 * @return <code>true</code> if and only if this list has
164 * no components, that is, its size is zero;
165 * <code>false</code> otherwise
166 * @see Vector#isEmpty()
167 */
168 public boolean isEmpty() {
169 return delegate.isEmpty();
170 }
171
172 /**
173 * Returns an enumeration of the components of this list.
174 *
175 * @return an enumeration of the components of this list
176 * @see Vector#elements()
177 */
178 public Enumeration<?> elements() {
179 return delegate.elements();
180 }
181
182 /**
183 * Tests whether the specified object is a component in this list.
184 *
185 * @param elem an object
186 * @return <code>true</code> if the specified object
187 * is the same as a component in this list
188 * @see Vector#contains(Object)
189 */
190 public boolean contains(Object elem) {
191 return delegate.contains(elem);
192 }
193
194 /**
195 * Searches for the first occurrence of <code>elem</code>.
196 *
197 * @param elem an object
198 * @return the index of the first occurrence of the argument in this
199 * list; returns <code>-1</code> if the object is not found
200 * @see Vector#indexOf(Object)
201 */
202 public int indexOf(Object elem) {
203 return delegate.indexOf(elem);
204 }
205
206 /**
207 * Searches for the first occurrence of <code>elem</code>, beginning
208 * the search at <code>index</code>.
209 *
210 * @param elem an desired component
211 * @param index the index from which to begin searching
212 * @return the index where the first occurrence of <code>elem</code>
213 * is found after <code>index</code>; returns <code>-1</code>
214 * if the <code>elem</code> is not found in the list
215 * @see Vector#indexOf(Object,int)
216 */
217 public int indexOf(Object elem, int index) {
218 return delegate.indexOf(elem, index);
219 }
220
221 /**
222 * Returns the index of the last occurrence of <code>elem</code>.
223 *
224 * @param elem the desired component
225 * @return the index of the last occurrence of <code>elem</code>
226 * in the list; returns <code>-1</code> if the object is not found
227 * @see Vector#lastIndexOf(Object)
228 */
229 public int lastIndexOf(Object elem) {
230 return delegate.lastIndexOf(elem);
231 }
232
233 /**
234 * Searches backwards for <code>elem</code>, starting from the
235 * specified index, and returns an index to it.
236 *
237 * @param elem the desired component
238 * @param index the index to start searching from
239 * @return the index of the last occurrence of the <code>elem</code>
240 * in this list at position less than <code>index</code>;
241 * returns <code>-1</code> if the object is not found
242 * @see Vector#lastIndexOf(Object,int)
243 */
244 public int lastIndexOf(Object elem, int index) {
245 return delegate.lastIndexOf(elem, index);
246 }
247
248 /**
249 * Returns the component at the specified index.
250 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
251 * is negative or not less than the size of the list.
252 * <blockquote>
253 * <b>Note:</b> Although this method is not deprecated, the preferred
254 * method to use is <code>get(int)</code>, which implements the
255 * <code>List</code> interface defined in the 1.2 Collections framework.
256 * </blockquote>
257 *
258 * @param index an index into this list
259 * @return the component at the specified index
260 * @see #get(int)
261 * @see Vector#elementAt(int)
262 */
263 public Object elementAt(int index) {
264 return delegate.elementAt(index);
265 }
266
267 /**
268 * Returns the first component of this list.
269 * Throws a <code>NoSuchElementException</code> if this
270 * vector has no components.
271 * @return the first component of this list
272 * @see Vector#firstElement()
273 */
274 public Object firstElement() {
275 return delegate.firstElement();
276 }
277
278 /**
279 * Returns the last component of the list.
280 * Throws a <code>NoSuchElementException</code> if this vector
281 * has no components.
282 *
283 * @return the last component of the list
284 * @see Vector#lastElement()
285 */
286 public Object lastElement() {
287 return delegate.lastElement();
288 }
289
290 /**
291 * Sets the component at the specified <code>index</code> of this
292 * list to be the specified object. The previous component at that
293 * position is discarded.
294 * <p>
295 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
296 * is invalid.
297 * <blockquote>
298 * <b>Note:</b> Although this method is not deprecated, the preferred
299 * method to use is <code>set(int,Object)</code>, which implements the
300 * <code>List</code> interface defined in the 1.2 Collections framework.
301 * </blockquote>
302 *
303 * @param obj what the component is to be set to
304 * @param index the specified index
305 * @see #set(int,Object)
306 * @see Vector#setElementAt(Object,int)
307 */
308 public void setElementAt(Object obj, int index) {
309 delegate.setElementAt(obj, index);
310 fireContentsChanged(this, index, index);
311 }
312
313 /**
314 * Deletes the component at the specified index.
315 * <p>
316 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
317 * is invalid.
318 * <blockquote>
319 * <b>Note:</b> Although this method is not deprecated, the preferred
320 * method to use is <code>remove(int)</code>, which implements the
321 * <code>List</code> interface defined in the 1.2 Collections framework.
322 * </blockquote>
323 *
324 * @param index the index of the object to remove
325 * @see #remove(int)
326 * @see Vector#removeElementAt(int)
327 */
328 public void removeElementAt(int index) {
329 delegate.removeElementAt(index);
330 fireIntervalRemoved(this, index, index);
331 }
332
333 /**
334 * Inserts the specified object as a component in this list at the
335 * specified <code>index</code>.
336 * <p>
337 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
338 * is invalid.
339 * <blockquote>
340 * <b>Note:</b> Although this method is not deprecated, the preferred
341 * method to use is <code>add(int,Object)</code>, which implements the
342 * <code>List</code> interface defined in the 1.2 Collections framework.
343 * </blockquote>
344 *
345 * @param obj the component to insert
346 * @param index where to insert the new component
347 * @exception ArrayIndexOutOfBoundsException if the index was invalid
348 * @see #add(int,Object)
349 * @see Vector#insertElementAt(Object,int)
350 */
351 public void insertElementAt(Object obj, int index) {
352 delegate.insertElementAt(obj, index);
353 fireIntervalAdded(this, index, index);
354 }
355
356 /**
357 * Adds the specified component to the end of this list.
358 *
359 * @param obj the component to be added
360 * @see Vector#addElement(Object)
361 */
362 public void addElement(Object obj) {
363 int index = delegate.size();
364 delegate.addElement(obj);
365 fireIntervalAdded(this, index, index);
366 }
367
368 /**
369 * Removes the first (lowest-indexed) occurrence of the argument
370 * from this list.
371 *
372 * @param obj the component to be removed
373 * @return <code>true</code> if the argument was a component of this
374 * list; <code>false</code> otherwise
375 * @see Vector#removeElement(Object)
376 */
377 public boolean removeElement(Object obj) {
378 int index = indexOf(obj);
379 boolean rv = delegate.removeElement(obj);
380 if (index >= 0) {
381 fireIntervalRemoved(this, index, index);
382 }
383 return rv;
384 }
385
386
387 /**
388 * Removes all components from this list and sets its size to zero.
389 * <blockquote>
390 * <b>Note:</b> Although this method is not deprecated, the preferred
391 * method to use is <code>clear</code>, which implements the
392 * <code>List</code> interface defined in the 1.2 Collections framework.
393 * </blockquote>
394 *
395 * @see #clear()
396 * @see Vector#removeAllElements()
397 */
398 public void removeAllElements() {
399 int index1 = delegate.size()-1;
400 delegate.removeAllElements();
401 if (index1 >= 0) {
402 fireIntervalRemoved(this, 0, index1);
403 }
404 }
405
406
407 /**
408 * Returns a string that displays and identifies this
409 * object's properties.
410 *
411 * @return a String representation of this object
412 */
413 public String toString() {
414 return delegate.toString();
415 }
416
417
418 /* The remaining methods are included for compatibility with the
419 * Java 2 platform Vector class.
420 */
421
422 /**
423 * Returns an array containing all of the elements in this list in the
424 * correct order.
425 *
426 * @return an array containing the elements of the list
427 * @see Vector#toArray()
428 */
429 public Object[] toArray() {
430 Object[] rv = new Object[delegate.size()];
431 delegate.copyInto(rv);
432 return rv;
433 }
434
435 /**
436 * Returns the element at the specified position in this list.
437 * <p>
438 * Throws an <code>ArrayIndexOutOfBoundsException</code>
439 * if the index is out of range
440 * (<code>index &lt; 0 || index &gt;= size()</code>).
441 *
442 * @param index index of element to return
443 */
444 public Object get(int index) {
445 return delegate.elementAt(index);
446 }
447
448 /**
449 * Replaces the element at the specified position in this list with the
450 * specified element.
451 * <p>
452 * Throws an <code>ArrayIndexOutOfBoundsException</code>
453 * if the index is out of range
454 * (<code>index &lt; 0 || index &gt;= size()</code>).
455 *
456 * @param index index of element to replace
457 * @param element element to be stored at the specified position
458 * @return the element previously at the specified position
459 */
460 public Object set(int index, Object element) {
461 Object rv = delegate.elementAt(index);
462 delegate.setElementAt(element, index);
463 fireContentsChanged(this, index, index);
464 return rv;
465 }
466
467 /**
468 * Inserts the specified element at the specified position in this list.
469 * <p>
470 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
471 * index is out of range
472 * (<code>index &lt; 0 || index &gt; size()</code>).
473 *
474 * @param index index at which the specified element is to be inserted
475 * @param element element to be inserted
476 */
477 public void add(int index, Object element) {
478 delegate.insertElementAt(element, index);
479 fireIntervalAdded(this, index, index);
480 }
481
482 /**
483 * Removes the element at the specified position in this list.
484 * Returns the element that was removed from the list.
485 * <p>
486 * Throws an <code>ArrayIndexOutOfBoundsException</code>
487 * if the index is out of range
488 * (<code>index &lt; 0 || index &gt;= size()</code>).
489 *
490 * @param index the index of the element to removed
491 */
492 public Object remove(int index) {
493 Object rv = delegate.elementAt(index);
494 delegate.removeElementAt(index);
495 fireIntervalRemoved(this, index, index);
496 return rv;
497 }
498
499 /**
500 * Removes all of the elements from this list. The list will
501 * be empty after this call returns (unless it throws an exception).
502 */
503 public void clear() {
504 int index1 = delegate.size()-1;
505 delegate.removeAllElements();
506 if (index1 >= 0) {
507 fireIntervalRemoved(this, 0, index1);
508 }
509 }
510
511 /**
512 * Deletes the components at the specified range of indexes.
513 * The removal is inclusive, so specifying a range of (1,5)
514 * removes the component at index 1 and the component at index 5,
515 * as well as all components in between.
516 * <p>
517 * Throws an <code>ArrayIndexOutOfBoundsException</code>
518 * if the index was invalid.
519 * Throws an <code>IllegalArgumentException</code> if
520 * <code>fromIndex &gt; toIndex</code>.
521 *
522 * @param fromIndex the index of the lower end of the range
523 * @param toIndex the index of the upper end of the range
524 * @see #remove(int)
525 */
526 public void removeRange(int fromIndex, int toIndex) {
527 if (fromIndex > toIndex) {
528 throw new IllegalArgumentException("fromIndex must be <= toIndex");
529 }
530 for(int i = toIndex; i >= fromIndex; i--) {
531 delegate.removeElementAt(i);
532 }
533 fireIntervalRemoved(this, fromIndex, toIndex);
534 }
535
536 /*
537 public void addAll(Collection c) {
538 }
539
540 public void addAll(int index, Collection c) {
541 }
542 */
543}