KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > comp > DefaultListModel


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultListModel.java,v 1.12 2004/02/01 05:16:27 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.comp;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 /**
28  * This class provides a default implementation for List Model.
29  * This implementation is backed by a java.util.ArrayList.
30  */

31 //csc_031103.2 public class DefaultListModel extends AbstractListModel implements List {
32
public class DefaultListModel extends AbstractListModel implements List, Cloneable JavaDoc, Serializable { //csc_031103.2
33

34     //public vars
35
protected static final Logger logger = Logger.getLogger(DefaultListModel.class.getName());
36
37     protected List items = new ArrayList();
38
39     //--------------- ListModel ----------------------------------
40
/**
41      * Get an element at a specific index
42      *
43      * @param index the target index
44      * @return the element at the specific index
45      */

46 // public Object getItemAt(int index, ViewContext vc) {
47
public Object JavaDoc getItemAt(int index) {
48         return get(index);
49     }
50
51     /**
52      * Get the size of the list
53      *
54      * @return the size of the list
55      * @see List#size()
56      */

57     public int getSize() {
58         return size();
59     }
60
61
62
63
64     //--------------- List ---------------------------------------
65
/**
66      * Returns the number of elements in this list.
67      *
68      * @return the number of elements in this list.
69      * @see List#size()
70      */

71     public int size() {
72         return items.size();
73     }
74
75     /**
76      * Returns <tt>true</tt> if this list contains no elements.
77      *
78      * @return <tt>true</tt> if this list contains no elements.
79      * @see List#isEmpty()
80      */

81     public boolean isEmpty() {
82         return items.isEmpty();
83     }
84
85     /**
86      *
87      * Returns <tt>true</tt> if this list contains the specified element.
88      *
89      * @param o element whose presence in this list is to be tested.
90      * @return <tt>true</tt> if this list contains the specified element.
91      * @see List#contains(Object)
92      */

93     public boolean contains(Object JavaDoc o) {
94         return items.contains(o);
95     }
96
97     /**
98      * Returns an iterator over the elements in this list in proper sequence.
99      *
100      * @return an iterator over the elements in this list in proper sequence.
101      * @see List#iterator()
102      */

103     public Iterator iterator() {
104         return items.iterator();
105     }
106
107     /**
108      * Returns an array containing all of the elements in this list in proper
109      * sequence. Obeys the general contract of the
110      * <tt>Collection.toArray</tt> method.
111      *
112      * @return an array containing all of the elements in this list in proper
113      * sequence.
114      * @see List#toArray()
115      */

116     public Object JavaDoc[] toArray() {
117         return items.toArray();
118     }
119
120     /**
121      * Returns an array containing all of the elements in this list in proper
122      * sequence; the runtime type of the returned array is that of the
123      * specified array. Obeys the general contract of the
124      * <tt>Collection.toArray(Object[])</tt> method.
125      *
126      * @param a the array into which the elements of this list are to
127      * be stored, if it is big enough; otherwise, a new array of the
128      * same runtime type is allocated for this purpose.
129      * @return an array containing the elements of this list.
130      * @throws ArrayStoreException if the runtime type of the specified array
131      * is not a supertype of the runtime type of every element in
132      * this list.
133      * @see List#toArray(Object[])
134      */

135     public Object JavaDoc[] toArray(Object JavaDoc a[]) {
136         return items.toArray(a);
137     }
138
139     /**
140      * Appends the specified element to the end of this list (optional
141      * operation). <p>
142      *
143      * @param o element to be appended to this list.
144      * @return <tt>true</tt> (as per the general contract of the
145      * <tt>Collection.add</tt> method).
146      * @throws UnsupportedOperationException if the <tt>add</tt> method is not
147      * supported by this list.
148      * @throws ClassCastException if the class of the specified element
149      * prevents it from being added to this list.
150      * @throws IllegalArgumentException if some aspect of this element
151      * prevents it from being added to this collection.
152      * @see List#add(Object)
153      */

154     public boolean add(Object JavaDoc o) {
155         boolean result = items.add(o);
156         if (result) {
157 // int index = items.size()-1;
158
// fireIntervalAdded(this, index, index);
159
fireModelChanged();
160         }
161         return result;
162     }
163
164     /**
165      * Removes the first occurrence in this list of the specified element
166      * (optional operation). If this list does not contain the element, it is
167      * unchanged.
168      *
169      * @param o element to be removed from this list, if present.
170      * @return <tt>true</tt> if this list contained the specified element.
171      * @throws UnsupportedOperationException if the <tt>remove</tt> method is
172      * not supported by this list.
173      * @see List#remove(Object)
174      */

175     public boolean remove(Object JavaDoc o) {
176         int pos = items.indexOf(o);
177         boolean result = items.remove(o);
178 // if (result) fireIntervalRemoved(this, pos, pos);
179
if (result) fireModelChanged();
180         return result;
181     }
182
183     /**
184      *
185      * Returns <tt>true</tt> if this list contains all of the elements of the
186      * specified collection.
187      *
188      * @param c collection to be checked for containment in this list.
189      * @return <tt>true</tt> if this list contains all of the elements of the
190      * specified collection.
191      * @see List#containsAll(Collection)
192      */

193     public boolean containsAll(Collection c) {
194         return items.containsAll(c);
195     }
196
197     /**
198      * Appends all of the elements in the specified collection to the end of
199      * this list, in the order that they are returned by the specified
200      * collection's iterator (optional operation).
201      *
202      * @param c collection whose elements are to be added to this list.
203      * @return <tt>true</tt> if this list changed as a result of the call.
204      * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
205      * not supported by this list.
206      * @throws ClassCastException if the class of an element in the specified
207      * collection prevents it from being added to this list.
208      * @throws IllegalArgumentException if some aspect of an element in the
209      * specified collection prevents it from being added to this
210      * list.
211      * @see List#addAll(Collection)
212      */

213     public boolean addAll(Collection c) {
214         int start = items.size()-1;
215         boolean result = items.addAll(c);
216 // if (result) fireIntervalAdded(this, start, items.size()-1);
217
if (result) fireModelChanged();
218         return result;
219     }
220
221     /**
222      * Inserts all of the elements in the specified collection into this
223      * list at the specified position (optional operation). Shifts the
224      * element currently at that position (if any) and any subsequent
225      * elements to the right (increases their indices).
226      *
227      * @param index index at which to insert first element from the specified
228      * collection.
229      * @param c elements to be inserted into this list.
230      * @return <tt>true</tt> if this list changed as a result of the call.
231      * @throws UnsupportedOperationException if the <tt>addAll</tt> method is
232      * not supported by this list.
233      * @throws ClassCastException if the class of one of elements of the
234      * specified collection prevents it from being added to this
235      * list.
236      * @throws IllegalArgumentException if some aspect of one of elements of
237      * the specified collection prevents it from being added to
238      * this list.
239      * @throws IndexOutOfBoundsException if the index is out of range (index
240      * &lt; 0 || index &gt; size()).
241      * @see List#addAll(int,Collection)
242      */

243     public boolean addAll(int index, Collection c) {
244         boolean result = items.addAll(index, c);
245 // if (result) fireIntervalAdded(this, index, items.size()-1);
246
if (result) fireModelChanged();
247         return result;
248     }
249
250     /**
251      * Removes from this list all the elements that are contained in the
252      * specified collection (optional operation).
253      *
254      * @param c collection that defines which elements will be removed from
255      * this list.
256      * @return <tt>true</tt> if this list changed as a result of the call.
257      * @throws UnsupportedOperationException if the <tt>removeAll</tt> method
258      * is not supported by this list.
259      * @see List#removeAll(Collection)
260      */

261     public boolean removeAll(Collection c) {
262         boolean result = items.removeAll(c);
263 // if (result) fireContentsChanged(this, 0, items.size()-1);
264
if (result) fireModelChanged();
265         return result;
266     }
267
268     /**
269      * Retains only the elements in this list that are contained in the
270      * specified collection (optional operation). In other words, removes
271      * from this list all the elements that are not contained in the specified
272      * collection.
273      *
274      * @param c collection that defines which elements this set will retain.
275      * @return <tt>true</tt> if this list changed as a result of the call.
276      * @throws UnsupportedOperationException if the <tt>retainAll</tt> method
277      * is not supported by this list.
278      *
279      * @see List#retainAll(Collection)
280      */

281     public boolean retainAll(Collection c) {
282         boolean result = items.retainAll(c);
283 // if (result) fireContentsChanged(this, 0, items.size()-1);
284
if (result) fireModelChanged();
285         return result;
286     }
287
288     /**
289      * Removes all of the elements from this list
290      *
291      * @throws UnsupportedOperationException if the <tt>clear</tt> method is
292      * not supported by this list.
293      * @see List#clear()
294      */

295     public void clear() {
296         items.clear();
297     }
298
299     /**
300      * Compares the specified object with this list for equality. Returns
301      * <tt>true</tt> if and only if the specified object is also a list, both
302      * lists have the same size, and all corresponding pairs of elements in
303      * the two lists are <i>equal</i>.
304      *
305      * @param o the object to be compared for equality with this list.
306      * @return <tt>true</tt> if the specified object is equal to this list.
307      * @see List#equals(Object)
308      */

309     public boolean equals(Object JavaDoc o) {
310         return items.equals(o);
311     }
312
313     /**
314      * Returns the hash code value for this list.
315      *
316      * @return the hash code value for this list.
317      * @see List#hashCode()
318      */

319     public int hashCode() {
320         return items.hashCode();
321     }
322
323     /**
324      * Returns the element at the specified position in this list.
325      *
326      * @param index index of element to return.
327      * @return the element at the specified position in this list.
328      * @throws IndexOutOfBoundsException if the index is out of range (index
329      * &lt; 0 || index &gt;= size()).
330      * @see List#get(int)
331      */

332     public Object JavaDoc get(int index) {
333         return items.get(index);
334     }
335
336     /**
337      * Replaces the element at the specified position in this list with the
338      * specified element (optional operation).
339      *
340      * @param index index of element to replace.
341      * @param element element to be stored at the specified position.
342      * @return the element previously at the specified position.
343      * @throws UnsupportedOperationException if the <tt>set</tt> method is not
344      * supported by this list.
345      * @throws ClassCastException if the class of the specified element
346      * prevents it from being added to this list.
347      * @throws IllegalArgumentException if some aspect of the specified
348      * element prevents it from being added to this list.
349      * @throws IndexOutOfBoundsException if the index is out of range
350      * (index &lt; 0 || index &gt;= size()).
351      * @see List#set(int,Object)
352      */

353     public Object JavaDoc set(int index, Object JavaDoc element) {
354         Object JavaDoc result = items.set(index, element);
355 // if (result!=null) fireContentsChanged(this, index, index);
356
if (result!=null) fireModelChanged();
357         return result;
358     }
359
360     /**
361      * Inserts the specified element at the specified position in this list
362      * (optional operation). Shifts the element currently at that position
363      * (if any) and any subsequent elements to the right (adds one to their
364      * indices).
365      *
366      * @param index index at which the specified element is to be inserted.
367      * @param element element to be inserted.
368      * @throws UnsupportedOperationException if the <tt>add</tt> method is not
369      * supported by this list.
370      * @throws ClassCastException if the class of the specified element
371      * prevents it from being added to this list.
372      * @throws IllegalArgumentException if some aspect of the specified
373      * element prevents it from being added to this list.
374      * @throws IndexOutOfBoundsException if the index is out of range
375      * (index &lt; 0 || index &gt; size()).
376      * @see List#add(int,Object)
377      */

378     public void add(int index, Object JavaDoc element) {
379         items.add(index, element);
380 // fireIntervalAdded(this, index, index);
381
fireModelChanged();
382     }
383
384     /**
385      * Removes the element at the specified position in this list (optional
386      * operation). Shifts any subsequent elements to the left (subtracts one
387      * from their indices). Returns the element that was removed from the
388      * list.
389      *
390      * @param index the index of the element to removed.
391      * @return the element previously at the specified position.
392      * @throws UnsupportedOperationException if the <tt>remove</tt> method is
393      * not supported by this list.
394      * @throws IndexOutOfBoundsException if the index is out of range (index
395      * &lt; 0 || index &gt;= size()).
396      * @see List#remove(int)
397      */

398     public Object JavaDoc remove(int index) {
399         Object JavaDoc result = items.remove(index);
400 // if (result!=null) fireIntervalRemoved(this, index, index);
401
if (result!=null) fireModelChanged();
402         return result;
403     }
404
405     /**
406      * Returns the index in this list of the first occurrence of the specified
407      * element, or -1 if this list does not contain this element.
408      *
409      * @param o element to search for.
410      * @return the index in this list of the first occurrence of the specified
411      * element, or -1 if this list does not contain this element.
412      * @see List#indexOf(Object)
413      */

414     public int indexOf(Object JavaDoc o) {
415         return items.indexOf(o);
416     }
417
418     /**
419      * Returns the index in this list of the last occurrence of the specified
420      * element, or -1 if this list does not contain this element.
421      *
422      * @param o element to search for.
423      * @return the index in this list of the last occurrence of the specified
424      * element, or -1 if this list does not contain this element.
425      * @see List#lastIndexOf(Object)
426      */

427     public int lastIndexOf(Object JavaDoc o) {
428         return items.lastIndexOf(o);
429     }
430
431     /**
432      * Returns a list iterator of the elements in this list (in proper
433      * sequence).
434      *
435      * @return a list iterator of the elements in this list (in proper
436      * sequence).
437      * @see List#listIterator()
438      */

439     public ListIterator listIterator() {
440         return items.listIterator();
441     }
442
443     /**
444      * Returns a list iterator of the elements in this list (in proper
445      * sequence), starting at the specified position in this list.
446      *
447      * @param index index of first element to be returned from the
448      * list iterator (by a call to the <tt>next</tt> method).
449      * @return a list iterator of the elements in this list (in proper
450      * sequence), starting at the specified position in this list.
451      * @throws IndexOutOfBoundsException if the index is out of range (index
452      * &lt; 0 || index &gt; size()).
453      * @see List#listIterator(int)
454      */

455     public ListIterator listIterator(int index) {
456         return items.listIterator(index);
457     }
458
459     /**
460      * Returns a view of the portion of this list between the specified
461      * <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
462      * <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
463      * empty.) The returned list is backed by this list, so changes in the
464      * returned list are reflected in this list, and vice-versa. The returned
465      * list supports all of the optional list operations supported by this
466      * list.<p>
467      *
468      * @param fromIndex low endpoint (inclusive) of the subList.
469      * @param toIndex high endpoint (exclusive) of the subList.
470      * @return a view of the specified range within this list.
471      * @throws IndexOutOfBoundsException for an illegal endpoint index value
472      * (fromIndex &lt; 0 || toIndex &gt; size || fromIndex &gt; toIndex).
473      * @see List#subList(int,int)
474      */

475     public List subList(int fromIndex, int toIndex) {
476         return items.subList(fromIndex, toIndex);
477     }
478
479
480
481
482     //--------------- Object -------------------------------------
483
/**
484      * Create a string representation of the list and return it.
485      * This method is invoked by the list component when rendering
486      * a list into a text based item that does not inherantly support
487      * a list structure
488      *
489      * @return a string representation of the list
490      */

491     public String JavaDoc toString() {
492         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(500);
493         String JavaDoc sep = "";
494         Iterator it = items.iterator();
495         while (it.hasNext()) {
496             sb.append(sep+it.next().toString());
497             sep = ", ";
498         }
499         String JavaDoc result = sb.toString().trim();
500         if (result==null || result.length()<1) result = "[empty list]";
501         return result;
502     }
503
504     //csc_031103.2 - added
505
/**
506      * Returns a clone of this list model with the same selection.
507      * <code>listenerLists</code> are not duplicated.
508      *
509      * @exception CloneNotSupportedException if the selection model does not
510      * both (a) implement the Cloneable interface and (b) define a
511      * <code>clone</code> method.
512      */

513     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
514         DefaultListModel clone = (DefaultListModel) super.clone();
515         clone.items = new ArrayList(items);
516         clone.listeners = new ArrayList();
517         return clone;
518     }
519 }
Popular Tags