KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > DefaultListModel


1 /*
2  * @(#)DefaultListModel.java 1.35 04/05/05
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.swing;
9
10 import java.util.Vector JavaDoc;
11 import java.util.Enumeration JavaDoc;
12
13 import javax.swing.event.*;
14
15
16 /**
17  * This class loosely implements the <code>java.util.Vector</code>
18  * API, in that it implements the 1.1.x version of
19  * <code>java.util.Vector</code>, has no collection class support,
20  * and notifies the <code>ListDataListener</code>s when changes occur.
21  * Presently it delegates to a <code>Vector</code>,
22  * in a future release it will be a real Collection implementation.
23  * <p>
24  * <strong>Warning:</strong>
25  * Serialized objects of this class will not be compatible with
26  * future Swing releases. The current serialization support is
27  * appropriate for short term storage or RMI between applications running
28  * the same version of Swing. As of 1.4, support for long term storage
29  * of all JavaBeans<sup><font size="-2">TM</font></sup>
30  * has been added to the <code>java.beans</code> package.
31  * Please see {@link java.beans.XMLEncoder}.
32  *
33  * @version 1.35 05/05/04
34  * @author Hans Muller
35  */

36 public class DefaultListModel extends AbstractListModel JavaDoc
37 {
38     private Vector JavaDoc delegate = new Vector JavaDoc();
39
40     /**
41      * Returns the number of components in this list.
42      * <p>
43      * This method is identical to <code>size</code>, which implements the
44      * <code>List</code> interface defined in the 1.2 Collections framework.
45      * This method exists in conjunction with <code>setSize</code> so that
46      * <code>size</code> is identifiable as a JavaBean property.
47      *
48      * @return the number of components in this list
49      * @see #size()
50      */

51     public int getSize() {
52     return delegate.size();
53     }
54
55     /**
56      * Returns the component at the specified index.
57      * <blockquote>
58      * <b>Note:</b> Although this method is not deprecated, the preferred
59      * method to use is <code>get(int)</code>, which implements the
60      * <code>List</code> interface defined in the 1.2 Collections framework.
61      * </blockquote>
62      * @param index an index into this list
63      * @return the component at the specified index
64      * @exception ArrayIndexOutOfBoundsException if the <code>index</code>
65      * is negative or greater than the current size of this
66      * list
67      * @see #get(int)
68      */

69     public Object JavaDoc getElementAt(int index) {
70     return delegate.elementAt(index);
71     }
72
73     /**
74      * Copies the components of this list into the specified array.
75      * The array must be big enough to hold all the objects in this list,
76      * else an <code>IndexOutOfBoundsException</code> is thrown.
77      *
78      * @param anArray the array into which the components get copied
79      * @see Vector#copyInto(Object[])
80      */

81     public void copyInto(Object JavaDoc anArray[]) {
82     delegate.copyInto(anArray);
83     }
84
85     /**
86      * Trims the capacity of this list to be the list's current size.
87      *
88      * @see Vector#trimToSize()
89      */

90     public void trimToSize() {
91     delegate.trimToSize();
92     }
93
94     /**
95      * Increases the capacity of this list, if necessary, to ensure
96      * that it can hold at least the number of components specified by
97      * the minimum capacity argument.
98      *
99      * @param minCapacity the desired minimum capacity
100      * @see Vector#ensureCapacity(int)
101      */

102     public void ensureCapacity(int minCapacity) {
103     delegate.ensureCapacity(minCapacity);
104     }
105
106     /**
107      * Sets the size of this list.
108      *
109      * @param newSize the new size of this list
110      * @see Vector#setSize(int)
111      */

112     public void setSize(int newSize) {
113     int oldSize = delegate.size();
114     delegate.setSize(newSize);
115     if (oldSize > newSize) {
116         fireIntervalRemoved(this, newSize, oldSize-1);
117     }
118     else if (oldSize < newSize) {
119         fireIntervalAdded(this, oldSize, newSize-1);
120     }
121     }
122
123     /**
124      * Returns the current capacity of this list.
125      *
126      * @return the current capacity
127      * @see Vector#capacity()
128      */

129     public int capacity() {
130     return delegate.capacity();
131     }
132
133     /**
134      * Returns the number of components in this list.
135      *
136      * @return the number of components in this list
137      * @see Vector#size()
138      */

139     public int size() {
140     return delegate.size();
141     }
142
143     /**
144      * Tests whether this list has any components.
145      *
146      * @return <code>true</code> if and only if this list has
147      * no components, that is, its size is zero;
148      * <code>false</code> otherwise
149      * @see Vector#isEmpty()
150      */

151     public boolean isEmpty() {
152     return delegate.isEmpty();
153     }
154
155     /**
156      * Returns an enumeration of the components of this list.
157      *
158      * @return an enumeration of the components of this list
159      * @see Vector#elements()
160      */

161     public Enumeration JavaDoc<?> elements() {
162     return delegate.elements();
163     }
164
165     /**
166      * Tests whether the specified object is a component in this list.
167      *
168      * @param elem an object
169      * @return <code>true</code> if the specified object
170      * is the same as a component in this list
171      * @see Vector#contains(Object)
172      */

173     public boolean contains(Object JavaDoc elem) {
174     return delegate.contains(elem);
175     }
176
177     /**
178      * Searches for the first occurrence of <code>elem</code>.
179      *
180      * @param elem an object
181      * @return the index of the first occurrence of the argument in this
182      * list; returns <code>-1</code> if the object is not found
183      * @see Vector#indexOf(Object)
184      */

185     public int indexOf(Object JavaDoc elem) {
186     return delegate.indexOf(elem);
187     }
188
189     /**
190      * Searches for the first occurrence of <code>elem</code>, beginning
191      * the search at <code>index</code>.
192      *
193      * @param elem an desired component
194      * @param index the index from which to begin searching
195      * @return the index where the first occurrence of <code>elem</code>
196      * is found after <code>index</code>; returns <code>-1</code>
197      * if the <code>elem</code> is not found in the list
198      * @see Vector#indexOf(Object,int)
199      */

200      public int indexOf(Object JavaDoc elem, int index) {
201     return delegate.indexOf(elem, index);
202     }
203
204     /**
205      * Returns the index of the last occurrence of <code>elem</code>.
206      *
207      * @param elem the desired component
208      * @return the index of the last occurrence of <code>elem</code>
209      * in the list; returns <code>-1</code> if the object is not found
210      * @see Vector#lastIndexOf(Object)
211      */

212     public int lastIndexOf(Object JavaDoc elem) {
213     return delegate.lastIndexOf(elem);
214     }
215
216     /**
217      * Searches backwards for <code>elem</code>, starting from the
218      * specified index, and returns an index to it.
219      *
220      * @param elem the desired component
221      * @param index the index to start searching from
222      * @return the index of the last occurrence of the <code>elem</code>
223      * in this list at position less than <code>index</code>;
224      * returns <code>-1</code> if the object is not found
225      * @see Vector#lastIndexOf(Object,int)
226      */

227     public int lastIndexOf(Object JavaDoc elem, int index) {
228     return delegate.lastIndexOf(elem, index);
229     }
230
231     /**
232      * Returns the component at the specified index.
233      * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
234      * is negative or not less than the size of the list.
235      * <blockquote>
236      * <b>Note:</b> Although this method is not deprecated, the preferred
237      * method to use is <code>get(int)</code>, which implements the
238      * <code>List</code> interface defined in the 1.2 Collections framework.
239      * </blockquote>
240      *
241      * @param index an index into this list
242      * @return the component at the specified index
243      * @see #get(int)
244      * @see Vector#elementAt(int)
245      */

246     public Object JavaDoc elementAt(int index) {
247     return delegate.elementAt(index);
248     }
249
250     /**
251      * Returns the first component of this list.
252      * Throws a <code>NoSuchElementException</code> if this
253      * vector has no components.
254      * @return the first component of this list
255      * @see Vector#firstElement()
256      */

257     public Object JavaDoc firstElement() {
258     return delegate.firstElement();
259     }
260
261     /**
262      * Returns the last component of the list.
263      * Throws a <code>NoSuchElementException</code> if this vector
264      * has no components.
265      *
266      * @return the last component of the list
267      * @see Vector#lastElement()
268      */

269     public Object JavaDoc lastElement() {
270     return delegate.lastElement();
271     }
272
273     /**
274      * Sets the component at the specified <code>index</code> of this
275      * list to be the specified object. The previous component at that
276      * position is discarded.
277      * <p>
278      * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
279      * is invalid.
280      * <blockquote>
281      * <b>Note:</b> Although this method is not deprecated, the preferred
282      * method to use is <code>set(int,Object)</code>, which implements the
283      * <code>List</code> interface defined in the 1.2 Collections framework.
284      * </blockquote>
285      *
286      * @param obj what the component is to be set to
287      * @param index the specified index
288      * @see #set(int,Object)
289      * @see Vector#setElementAt(Object,int)
290      */

291     public void setElementAt(Object JavaDoc obj, int index) {
292     delegate.setElementAt(obj, index);
293     fireContentsChanged(this, index, index);
294     }
295
296     /**
297      * Deletes the component at the specified index.
298      * <p>
299      * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
300      * is invalid.
301      * <blockquote>
302      * <b>Note:</b> Although this method is not deprecated, the preferred
303      * method to use is <code>remove(int)</code>, which implements the
304      * <code>List</code> interface defined in the 1.2 Collections framework.
305      * </blockquote>
306      *
307      * @param index the index of the object to remove
308      * @see #remove(int)
309      * @see Vector#removeElementAt(int)
310      */

311     public void removeElementAt(int index) {
312     delegate.removeElementAt(index);
313     fireIntervalRemoved(this, index, index);
314     }
315
316     /**
317      * Inserts the specified object as a component in this list at the
318      * specified <code>index</code>.
319      * <p>
320      * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
321      * is invalid.
322      * <blockquote>
323      * <b>Note:</b> Although this method is not deprecated, the preferred
324      * method to use is <code>add(int,Object)</code>, which implements the
325      * <code>List</code> interface defined in the 1.2 Collections framework.
326      * </blockquote>
327      *
328      * @param obj the component to insert
329      * @param index where to insert the new component
330      * @exception ArrayIndexOutOfBoundsException if the index was invalid
331      * @see #add(int,Object)
332      * @see Vector#insertElementAt(Object,int)
333      */

334     public void insertElementAt(Object JavaDoc obj, int index) {
335     delegate.insertElementAt(obj, index);
336     fireIntervalAdded(this, index, index);
337     }
338
339     /**
340      * Adds the specified component to the end of this list.
341      *
342      * @param obj the component to be added
343      * @see Vector#addElement(Object)
344      */

345     public void addElement(Object JavaDoc obj) {
346     int index = delegate.size();
347     delegate.addElement(obj);
348     fireIntervalAdded(this, index, index);
349     }
350
351     /**
352      * Removes the first (lowest-indexed) occurrence of the argument
353      * from this list.
354      *
355      * @param obj the component to be removed
356      * @return <code>true</code> if the argument was a component of this
357      * list; <code>false</code> otherwise
358      * @see Vector#removeElement(Object)
359      */

360     public boolean removeElement(Object JavaDoc obj) {
361     int index = indexOf(obj);
362     boolean rv = delegate.removeElement(obj);
363     if (index >= 0) {
364         fireIntervalRemoved(this, index, index);
365     }
366     return rv;
367     }
368
369
370     /**
371      * Removes all components from this list and sets its size to zero.
372      * <blockquote>
373      * <b>Note:</b> Although this method is not deprecated, the preferred
374      * method to use is <code>clear</code>, which implements the
375      * <code>List</code> interface defined in the 1.2 Collections framework.
376      * </blockquote>
377      *
378      * @see #clear()
379      * @see Vector#removeAllElements()
380      */

381     public void removeAllElements() {
382     int index1 = delegate.size()-1;
383     delegate.removeAllElements();
384     if (index1 >= 0) {
385         fireIntervalRemoved(this, 0, index1);
386     }
387     }
388
389
390     /**
391      * Returns a string that displays and identifies this
392      * object's properties.
393      *
394      * @return a String representation of this object
395      */

396    public String JavaDoc toString() {
397     return delegate.toString();
398     }
399
400
401     /* The remaining methods are included for compatibility with the
402      * Java 2 platform Vector class.
403      */

404
405     /**
406      * Returns an array containing all of the elements in this list in the
407      * correct order.
408      *
409      * @return an array containing the elements of the list
410      * @see Vector#toArray()
411      */

412     public Object JavaDoc[] toArray() {
413     Object JavaDoc[] rv = new Object JavaDoc[delegate.size()];
414     delegate.copyInto(rv);
415     return rv;
416     }
417
418     /**
419      * Returns the element at the specified position in this list.
420      * <p>
421      * Throws an <code>ArrayIndexOutOfBoundsException</code>
422      * if the index is out of range
423      * (<code>index &lt; 0 || index &gt;= size()</code>).
424      *
425      * @param index index of element to return
426      */

427     public Object JavaDoc get(int index) {
428     return delegate.elementAt(index);
429     }
430
431     /**
432      * Replaces the element at the specified position in this list with the
433      * specified element.
434      * <p>
435      * Throws an <code>ArrayIndexOutOfBoundsException</code>
436      * if the index is out of range
437      * (<code>index &lt; 0 || index &gt;= size()</code>).
438      *
439      * @param index index of element to replace
440      * @param element element to be stored at the specified position
441      * @return the element previously at the specified position
442      */

443     public Object JavaDoc set(int index, Object JavaDoc element) {
444     Object JavaDoc rv = delegate.elementAt(index);
445     delegate.setElementAt(element, index);
446     fireContentsChanged(this, index, index);
447     return rv;
448     }
449
450     /**
451      * Inserts the specified element at the specified position in this list.
452      * <p>
453      * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
454      * index is out of range
455      * (<code>index &lt; 0 || index &gt; size()</code>).
456      *
457      * @param index index at which the specified element is to be inserted
458      * @param element element to be inserted
459      */

460     public void add(int index, Object JavaDoc element) {
461     delegate.insertElementAt(element, index);
462     fireIntervalAdded(this, index, index);
463     }
464
465     /**
466      * Removes the element at the specified position in this list.
467      * Returns the element that was removed from the list.
468      * <p>
469      * Throws an <code>ArrayIndexOutOfBoundsException</code>
470      * if the index is out of range
471      * (<code>index &lt; 0 || index &gt;= size()</code>).
472      *
473      * @param index the index of the element to removed
474      */

475     public Object JavaDoc remove(int index) {
476     Object JavaDoc rv = delegate.elementAt(index);
477     delegate.removeElementAt(index);
478     fireIntervalRemoved(this, index, index);
479     return rv;
480     }
481
482     /**
483      * Removes all of the elements from this list. The list will
484      * be empty after this call returns (unless it throws an exception).
485      */

486     public void clear() {
487     int index1 = delegate.size()-1;
488     delegate.removeAllElements();
489     if (index1 >= 0) {
490         fireIntervalRemoved(this, 0, index1);
491     }
492     }
493
494     /**
495      * Deletes the components at the specified range of indexes.
496      * The removal is inclusive, so specifying a range of (1,5)
497      * removes the component at index 1 and the component at index 5,
498      * as well as all components in between.
499      * <p>
500      * Throws an <code>ArrayIndexOutOfBoundsException</code>
501      * if the index was invalid.
502      * Throws an <code>IllegalArgumentException</code> if
503      * <code>fromIndex &gt; toIndex</code>.
504      *
505      * @param fromIndex the index of the lower end of the range
506      * @param toIndex the index of the upper end of the range
507      * @see #remove(int)
508      */

509     public void removeRange(int fromIndex, int toIndex) {
510     if (fromIndex > toIndex) {
511         throw new IllegalArgumentException JavaDoc("fromIndex must be <= toIndex");
512     }
513     for(int i = toIndex; i >= fromIndex; i--) {
514         delegate.removeElementAt(i);
515     }
516     fireIntervalRemoved(this, fromIndex, toIndex);
517     }
518
519     /*
520     public void addAll(Collection c) {
521     }
522
523     public void addAll(int index, Collection c) {
524     }
525     */

526 }
527
Popular Tags