001 /*
002 * Copyright 1997-2004 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import java.util.Vector;
029 import java.util.Enumeration;
030
031 import javax.swing.event.*;
032
033 /**
034 * This class loosely implements the <code>java.util.Vector</code>
035 * API, in that it implements the 1.1.x version of
036 * <code>java.util.Vector</code>, has no collection class support,
037 * and notifies the <code>ListDataListener</code>s when changes occur.
038 * Presently it delegates to a <code>Vector</code>,
039 * in a future release it will be a real Collection implementation.
040 * <p>
041 * <strong>Warning:</strong>
042 * Serialized objects of this class will not be compatible with
043 * future Swing releases. The current serialization support is
044 * appropriate for short term storage or RMI between applications running
045 * the same version of Swing. As of 1.4, support for long term storage
046 * of all JavaBeans<sup><font size="-2">TM</font></sup>
047 * has been added to the <code>java.beans</code> package.
048 * Please see {@link java.beans.XMLEncoder}.
049 *
050 * @version 1.42 05/05/07
051 * @author Hans Muller
052 */
053 public class DefaultListModel extends AbstractListModel {
054 private Vector delegate = new Vector();
055
056 /**
057 * Returns the number of components in this list.
058 * <p>
059 * This method is identical to <code>size</code>, which implements the
060 * <code>List</code> interface defined in the 1.2 Collections framework.
061 * This method exists in conjunction with <code>setSize</code> so that
062 * <code>size</code> is identifiable as a JavaBean property.
063 *
064 * @return the number of components in this list
065 * @see #size()
066 */
067 public int getSize() {
068 return delegate.size();
069 }
070
071 /**
072 * Returns the component at the specified index.
073 * <blockquote>
074 * <b>Note:</b> Although this method is not deprecated, the preferred
075 * method to use is <code>get(int)</code>, which implements the
076 * <code>List</code> interface defined in the 1.2 Collections framework.
077 * </blockquote>
078 * @param index an index into this list
079 * @return the component at the specified index
080 * @exception ArrayIndexOutOfBoundsException if the <code>index</code>
081 * is negative or greater than the current size of this
082 * list
083 * @see #get(int)
084 */
085 public Object getElementAt(int index) {
086 return delegate.elementAt(index);
087 }
088
089 /**
090 * Copies the components of this list into the specified array.
091 * The array must be big enough to hold all the objects in this list,
092 * else an <code>IndexOutOfBoundsException</code> is thrown.
093 *
094 * @param anArray the array into which the components get copied
095 * @see Vector#copyInto(Object[])
096 */
097 public void copyInto(Object anArray[]) {
098 delegate.copyInto(anArray);
099 }
100
101 /**
102 * Trims the capacity of this list to be the list's current size.
103 *
104 * @see Vector#trimToSize()
105 */
106 public void trimToSize() {
107 delegate.trimToSize();
108 }
109
110 /**
111 * Increases the capacity of this list, if necessary, to ensure
112 * that it can hold at least the number of components specified by
113 * the minimum capacity argument.
114 *
115 * @param minCapacity the desired minimum capacity
116 * @see Vector#ensureCapacity(int)
117 */
118 public void ensureCapacity(int minCapacity) {
119 delegate.ensureCapacity(minCapacity);
120 }
121
122 /**
123 * Sets the size of this list.
124 *
125 * @param newSize the new size of this list
126 * @see Vector#setSize(int)
127 */
128 public void setSize(int newSize) {
129 int oldSize = delegate.size();
130 delegate.setSize(newSize);
131 if (oldSize > newSize) {
132 fireIntervalRemoved(this , newSize, oldSize - 1);
133 } else if (oldSize < newSize) {
134 fireIntervalAdded(this , oldSize, newSize - 1);
135 }
136 }
137
138 /**
139 * Returns the current capacity of this list.
140 *
141 * @return the current capacity
142 * @see Vector#capacity()
143 */
144 public int capacity() {
145 return delegate.capacity();
146 }
147
148 /**
149 * Returns the number of components in this list.
150 *
151 * @return the number of components in this list
152 * @see Vector#size()
153 */
154 public int size() {
155 return delegate.size();
156 }
157
158 /**
159 * Tests whether this list has any components.
160 *
161 * @return <code>true</code> if and only if this list has
162 * no components, that is, its size is zero;
163 * <code>false</code> otherwise
164 * @see Vector#isEmpty()
165 */
166 public boolean isEmpty() {
167 return delegate.isEmpty();
168 }
169
170 /**
171 * Returns an enumeration of the components of this list.
172 *
173 * @return an enumeration of the components of this list
174 * @see Vector#elements()
175 */
176 public Enumeration<?> elements() {
177 return delegate.elements();
178 }
179
180 /**
181 * Tests whether the specified object is a component in this list.
182 *
183 * @param elem an object
184 * @return <code>true</code> if the specified object
185 * is the same as a component in this list
186 * @see Vector#contains(Object)
187 */
188 public boolean contains(Object elem) {
189 return delegate.contains(elem);
190 }
191
192 /**
193 * Searches for the first occurrence of <code>elem</code>.
194 *
195 * @param elem an object
196 * @return the index of the first occurrence of the argument in this
197 * list; returns <code>-1</code> if the object is not found
198 * @see Vector#indexOf(Object)
199 */
200 public int indexOf(Object elem) {
201 return delegate.indexOf(elem);
202 }
203
204 /**
205 * Searches for the first occurrence of <code>elem</code>, beginning
206 * the search at <code>index</code>.
207 *
208 * @param elem an desired component
209 * @param index the index from which to begin searching
210 * @return the index where the first occurrence of <code>elem</code>
211 * is found after <code>index</code>; returns <code>-1</code>
212 * if the <code>elem</code> is not found in the list
213 * @see Vector#indexOf(Object,int)
214 */
215 public int indexOf(Object elem, int index) {
216 return delegate.indexOf(elem, index);
217 }
218
219 /**
220 * Returns the index of the last occurrence of <code>elem</code>.
221 *
222 * @param elem the desired component
223 * @return the index of the last occurrence of <code>elem</code>
224 * in the list; returns <code>-1</code> if the object is not found
225 * @see Vector#lastIndexOf(Object)
226 */
227 public int lastIndexOf(Object elem) {
228 return delegate.lastIndexOf(elem);
229 }
230
231 /**
232 * Searches backwards for <code>elem</code>, starting from the
233 * specified index, and returns an index to it.
234 *
235 * @param elem the desired component
236 * @param index the index to start searching from
237 * @return the index of the last occurrence of the <code>elem</code>
238 * in this list at position less than <code>index</code>;
239 * returns <code>-1</code> if the object is not found
240 * @see Vector#lastIndexOf(Object,int)
241 */
242 public int lastIndexOf(Object elem, int index) {
243 return delegate.lastIndexOf(elem, index);
244 }
245
246 /**
247 * Returns the component at the specified index.
248 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
249 * is negative or not less than the size of the list.
250 * <blockquote>
251 * <b>Note:</b> Although this method is not deprecated, the preferred
252 * method to use is <code>get(int)</code>, which implements the
253 * <code>List</code> interface defined in the 1.2 Collections framework.
254 * </blockquote>
255 *
256 * @param index an index into this list
257 * @return the component at the specified index
258 * @see #get(int)
259 * @see Vector#elementAt(int)
260 */
261 public Object elementAt(int index) {
262 return delegate.elementAt(index);
263 }
264
265 /**
266 * Returns the first component of this list.
267 * Throws a <code>NoSuchElementException</code> if this
268 * vector has no components.
269 * @return the first component of this list
270 * @see Vector#firstElement()
271 */
272 public Object firstElement() {
273 return delegate.firstElement();
274 }
275
276 /**
277 * Returns the last component of the list.
278 * Throws a <code>NoSuchElementException</code> if this vector
279 * has no components.
280 *
281 * @return the last component of the list
282 * @see Vector#lastElement()
283 */
284 public Object lastElement() {
285 return delegate.lastElement();
286 }
287
288 /**
289 * Sets the component at the specified <code>index</code> of this
290 * list to be the specified object. The previous component at that
291 * position is discarded.
292 * <p>
293 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
294 * is invalid.
295 * <blockquote>
296 * <b>Note:</b> Although this method is not deprecated, the preferred
297 * method to use is <code>set(int,Object)</code>, which implements the
298 * <code>List</code> interface defined in the 1.2 Collections framework.
299 * </blockquote>
300 *
301 * @param obj what the component is to be set to
302 * @param index the specified index
303 * @see #set(int,Object)
304 * @see Vector#setElementAt(Object,int)
305 */
306 public void setElementAt(Object obj, int index) {
307 delegate.setElementAt(obj, index);
308 fireContentsChanged(this , index, index);
309 }
310
311 /**
312 * Deletes the component at the specified index.
313 * <p>
314 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
315 * is invalid.
316 * <blockquote>
317 * <b>Note:</b> Although this method is not deprecated, the preferred
318 * method to use is <code>remove(int)</code>, which implements the
319 * <code>List</code> interface defined in the 1.2 Collections framework.
320 * </blockquote>
321 *
322 * @param index the index of the object to remove
323 * @see #remove(int)
324 * @see Vector#removeElementAt(int)
325 */
326 public void removeElementAt(int index) {
327 delegate.removeElementAt(index);
328 fireIntervalRemoved(this , index, index);
329 }
330
331 /**
332 * Inserts the specified object as a component in this list at the
333 * specified <code>index</code>.
334 * <p>
335 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
336 * is invalid.
337 * <blockquote>
338 * <b>Note:</b> Although this method is not deprecated, the preferred
339 * method to use is <code>add(int,Object)</code>, which implements the
340 * <code>List</code> interface defined in the 1.2 Collections framework.
341 * </blockquote>
342 *
343 * @param obj the component to insert
344 * @param index where to insert the new component
345 * @exception ArrayIndexOutOfBoundsException if the index was invalid
346 * @see #add(int,Object)
347 * @see Vector#insertElementAt(Object,int)
348 */
349 public void insertElementAt(Object obj, int index) {
350 delegate.insertElementAt(obj, index);
351 fireIntervalAdded(this , index, index);
352 }
353
354 /**
355 * Adds the specified component to the end of this list.
356 *
357 * @param obj the component to be added
358 * @see Vector#addElement(Object)
359 */
360 public void addElement(Object obj) {
361 int index = delegate.size();
362 delegate.addElement(obj);
363 fireIntervalAdded(this , index, index);
364 }
365
366 /**
367 * Removes the first (lowest-indexed) occurrence of the argument
368 * from this list.
369 *
370 * @param obj the component to be removed
371 * @return <code>true</code> if the argument was a component of this
372 * list; <code>false</code> otherwise
373 * @see Vector#removeElement(Object)
374 */
375 public boolean removeElement(Object obj) {
376 int index = indexOf(obj);
377 boolean rv = delegate.removeElement(obj);
378 if (index >= 0) {
379 fireIntervalRemoved(this , index, index);
380 }
381 return rv;
382 }
383
384 /**
385 * Removes all components from this list and sets its size to zero.
386 * <blockquote>
387 * <b>Note:</b> Although this method is not deprecated, the preferred
388 * method to use is <code>clear</code>, which implements the
389 * <code>List</code> interface defined in the 1.2 Collections framework.
390 * </blockquote>
391 *
392 * @see #clear()
393 * @see Vector#removeAllElements()
394 */
395 public void removeAllElements() {
396 int index1 = delegate.size() - 1;
397 delegate.removeAllElements();
398 if (index1 >= 0) {
399 fireIntervalRemoved(this , 0, index1);
400 }
401 }
402
403 /**
404 * Returns a string that displays and identifies this
405 * object's properties.
406 *
407 * @return a String representation of this object
408 */
409 public String toString() {
410 return delegate.toString();
411 }
412
413 /* The remaining methods are included for compatibility with the
414 * Java 2 platform Vector class.
415 */
416
417 /**
418 * Returns an array containing all of the elements in this list in the
419 * correct order.
420 *
421 * @return an array containing the elements of the list
422 * @see Vector#toArray()
423 */
424 public Object[] toArray() {
425 Object[] rv = new Object[delegate.size()];
426 delegate.copyInto(rv);
427 return rv;
428 }
429
430 /**
431 * Returns the element at the specified position in this list.
432 * <p>
433 * Throws an <code>ArrayIndexOutOfBoundsException</code>
434 * if the index is out of range
435 * (<code>index < 0 || index >= size()</code>).
436 *
437 * @param index index of element to return
438 */
439 public Object get(int index) {
440 return delegate.elementAt(index);
441 }
442
443 /**
444 * Replaces the element at the specified position in this list with the
445 * specified element.
446 * <p>
447 * Throws an <code>ArrayIndexOutOfBoundsException</code>
448 * if the index is out of range
449 * (<code>index < 0 || index >= size()</code>).
450 *
451 * @param index index of element to replace
452 * @param element element to be stored at the specified position
453 * @return the element previously at the specified position
454 */
455 public Object set(int index, Object element) {
456 Object rv = delegate.elementAt(index);
457 delegate.setElementAt(element, index);
458 fireContentsChanged(this , index, index);
459 return rv;
460 }
461
462 /**
463 * Inserts the specified element at the specified position in this list.
464 * <p>
465 * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
466 * index is out of range
467 * (<code>index < 0 || index > size()</code>).
468 *
469 * @param index index at which the specified element is to be inserted
470 * @param element element to be inserted
471 */
472 public void add(int index, Object element) {
473 delegate.insertElementAt(element, index);
474 fireIntervalAdded(this , index, index);
475 }
476
477 /**
478 * Removes the element at the specified position in this list.
479 * Returns the element that was removed from the list.
480 * <p>
481 * Throws an <code>ArrayIndexOutOfBoundsException</code>
482 * if the index is out of range
483 * (<code>index < 0 || index >= size()</code>).
484 *
485 * @param index the index of the element to removed
486 */
487 public Object remove(int index) {
488 Object rv = delegate.elementAt(index);
489 delegate.removeElementAt(index);
490 fireIntervalRemoved(this , index, index);
491 return rv;
492 }
493
494 /**
495 * Removes all of the elements from this list. The list will
496 * be empty after this call returns (unless it throws an exception).
497 */
498 public void clear() {
499 int index1 = delegate.size() - 1;
500 delegate.removeAllElements();
501 if (index1 >= 0) {
502 fireIntervalRemoved(this , 0, index1);
503 }
504 }
505
506 /**
507 * Deletes the components at the specified range of indexes.
508 * The removal is inclusive, so specifying a range of (1,5)
509 * removes the component at index 1 and the component at index 5,
510 * as well as all components in between.
511 * <p>
512 * Throws an <code>ArrayIndexOutOfBoundsException</code>
513 * if the index was invalid.
514 * Throws an <code>IllegalArgumentException</code> if
515 * <code>fromIndex > toIndex</code>.
516 *
517 * @param fromIndex the index of the lower end of the range
518 * @param toIndex the index of the upper end of the range
519 * @see #remove(int)
520 */
521 public void removeRange(int fromIndex, int toIndex) {
522 if (fromIndex > toIndex) {
523 throw new IllegalArgumentException(
524 "fromIndex must be <= toIndex");
525 }
526 for (int i = toIndex; i >= fromIndex; i--) {
527 delegate.removeElementAt(i);
528 }
529 fireIntervalRemoved(this , fromIndex, toIndex);
530 }
531
532 /*
533 public void addAll(Collection c) {
534 }
535
536 public void addAll(int index, Collection c) {
537 }
538 */
539 }
|