svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / DefaultListModel.java @ 40561
History | View | Annotate | Download (15.3 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.utils; |
25 |
import java.util.Collection; |
26 |
import java.util.Enumeration; |
27 |
import java.util.Vector; |
28 |
|
29 |
import javax.swing.AbstractListModel; |
30 |
|
31 |
|
32 |
/**
|
33 |
* Esta es la misma clase que javax.swing.DefaultListModel
|
34 |
* pero tiene un constructor que toma como par?metro una
|
35 |
* collection.
|
36 |
*/
|
37 |
public class DefaultListModel extends AbstractListModel |
38 |
{ |
39 |
private Vector delegate; |
40 |
|
41 |
public DefaultListModel(Collection c){ |
42 |
delegate = new Vector(c); |
43 |
} |
44 |
|
45 |
public DefaultListModel(){ |
46 |
delegate = new Vector(); |
47 |
} |
48 |
|
49 |
/**
|
50 |
* Returns the number of components in this list.
|
51 |
* <p>
|
52 |
* This method is identical to <code>size</code>, which implements the
|
53 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
54 |
* This method exists in conjunction with <code>setSize</code> so that
|
55 |
* <code>size</code> is identifiable as a JavaBean property.
|
56 |
*
|
57 |
* @return the number of components in this list
|
58 |
* @see #size()
|
59 |
*/
|
60 |
public int getSize() { |
61 |
return delegate.size();
|
62 |
} |
63 |
|
64 |
/**
|
65 |
* Returns the component at the specified index.
|
66 |
* <blockquote>
|
67 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
68 |
* method to use is <code>get(int)</code>, which implements the
|
69 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
70 |
* </blockquote>
|
71 |
* @param index an index into this list
|
72 |
* @return the component at the specified index
|
73 |
* @exception ArrayIndexOutOfBoundsException if the <code>index</code>
|
74 |
* is negative or greater than the current size of this
|
75 |
* list
|
76 |
* @see #get(int)
|
77 |
*/
|
78 |
public Object getElementAt(int index) { |
79 |
return delegate.elementAt(index);
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Copies the components of this list into the specified array.
|
84 |
* The array must be big enough to hold all the objects in this list,
|
85 |
* else an <code>IndexOutOfBoundsException</code> is thrown.
|
86 |
*
|
87 |
* @param anArray the array into which the components get copied
|
88 |
* @see Vector#copyInto(Object[])
|
89 |
*/
|
90 |
public void copyInto(Object anArray[]) { |
91 |
delegate.copyInto(anArray); |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Trims the capacity of this list to be the list's current size.
|
96 |
*
|
97 |
* @see Vector#trimToSize()
|
98 |
*/
|
99 |
public void trimToSize() { |
100 |
delegate.trimToSize(); |
101 |
} |
102 |
|
103 |
/**
|
104 |
* Increases the capacity of this list, if necessary, to ensure
|
105 |
* that it can hold at least the number of components specified by
|
106 |
* the minimum capacity argument.
|
107 |
*
|
108 |
* @param minCapacity the desired minimum capacity
|
109 |
* @see Vector#ensureCapacity(int)
|
110 |
*/
|
111 |
public void ensureCapacity(int minCapacity) { |
112 |
delegate.ensureCapacity(minCapacity); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Sets the size of this list.
|
117 |
*
|
118 |
* @param newSize the new size of this list
|
119 |
* @see Vector#setSize(int)
|
120 |
*/
|
121 |
public void setSize(int newSize) { |
122 |
int oldSize = delegate.size();
|
123 |
delegate.setSize(newSize); |
124 |
if (oldSize > newSize) {
|
125 |
fireIntervalRemoved(this, newSize, oldSize-1); |
126 |
} |
127 |
else if (oldSize < newSize) { |
128 |
fireIntervalAdded(this, oldSize, newSize-1); |
129 |
} |
130 |
} |
131 |
|
132 |
/**
|
133 |
* Returns the current capacity of this list.
|
134 |
*
|
135 |
* @return the current capacity
|
136 |
* @see Vector#capacity()
|
137 |
*/
|
138 |
public int capacity() { |
139 |
return delegate.capacity();
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* Returns the number of components in this list.
|
144 |
*
|
145 |
* @return the number of components in this list
|
146 |
* @see Vector#size()
|
147 |
*/
|
148 |
public int size() { |
149 |
return delegate.size();
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Tests whether this list has any components.
|
154 |
*
|
155 |
* @return <code>true</code> if and only if this list has
|
156 |
* no components, that is, its size is zero;
|
157 |
* <code>false</code> otherwise
|
158 |
* @see Vector#isEmpty()
|
159 |
*/
|
160 |
public boolean isEmpty() { |
161 |
return delegate.isEmpty();
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* Returns an enumeration of the components of this list.
|
166 |
*
|
167 |
* @return an enumeration of the components of this list
|
168 |
* @see Vector#elements()
|
169 |
*/
|
170 |
public Enumeration elements() { |
171 |
return delegate.elements();
|
172 |
} |
173 |
|
174 |
/**
|
175 |
* Tests whether the specified object is a component in this list.
|
176 |
*
|
177 |
* @param elem an object
|
178 |
* @return <code>true</code> if the specified object
|
179 |
* is the same as a component in this list
|
180 |
* @see Vector#contains(Object)
|
181 |
*/
|
182 |
public boolean contains(Object elem) { |
183 |
return delegate.contains(elem);
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* Searches for the first occurrence of <code>elem</code>.
|
188 |
*
|
189 |
* @param elem an object
|
190 |
* @return the index of the first occurrence of the argument in this
|
191 |
* list; returns <code>-1</code> if the object is not found
|
192 |
* @see Vector#indexOf(Object)
|
193 |
*/
|
194 |
public int indexOf(Object elem) { |
195 |
return delegate.indexOf(elem);
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* Searches for the first occurrence of <code>elem</code>, beginning
|
200 |
* the search at <code>index</code>.
|
201 |
*
|
202 |
* @param elem an desired component
|
203 |
* @param index the index from which to begin searching
|
204 |
* @return the index where the first occurrence of <code>elem</code>
|
205 |
* is found after <code>index</code>; returns <code>-1</code>
|
206 |
* if the <code>elem</code> is not found in the list
|
207 |
* @see Vector#indexOf(Object,int)
|
208 |
*/
|
209 |
public int indexOf(Object elem, int index) { |
210 |
return delegate.indexOf(elem, index);
|
211 |
} |
212 |
|
213 |
/**
|
214 |
* Returns the index of the last occurrence of <code>elem</code>.
|
215 |
*
|
216 |
* @param elem the desired component
|
217 |
* @return the index of the last occurrence of <code>elem</code>
|
218 |
* in the list; returns <code>-1</code> if the object is not found
|
219 |
* @see Vector#lastIndexOf(Object)
|
220 |
*/
|
221 |
public int lastIndexOf(Object elem) { |
222 |
return delegate.lastIndexOf(elem);
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* Searches backwards for <code>elem</code>, starting from the
|
227 |
* specified index, and returns an index to it.
|
228 |
*
|
229 |
* @param elem the desired component
|
230 |
* @param index the index to start searching from
|
231 |
* @return the index of the last occurrence of the <code>elem</code>
|
232 |
* in this list at position less than <code>index</code>;
|
233 |
* returns <code>-1</code> if the object is not found
|
234 |
* @see Vector#lastIndexOf(Object,int)
|
235 |
*/
|
236 |
public int lastIndexOf(Object elem, int index) { |
237 |
return delegate.lastIndexOf(elem, index);
|
238 |
} |
239 |
|
240 |
/**
|
241 |
* Returns the component at the specified index.
|
242 |
* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
|
243 |
* is negative or not less than the size of the list.
|
244 |
* <blockquote>
|
245 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
246 |
* method to use is <code>get(int)</code>, which implements the
|
247 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
248 |
* </blockquote>
|
249 |
*
|
250 |
* @param index an index into this list
|
251 |
* @return the component at the specified index
|
252 |
* @see #get(int)
|
253 |
* @see Vector#elementAt(int)
|
254 |
*/
|
255 |
public Object elementAt(int index) { |
256 |
return delegate.elementAt(index);
|
257 |
} |
258 |
|
259 |
/**
|
260 |
* Returns the first component of this list.
|
261 |
* Throws a <code>NoSuchElementException</code> if this
|
262 |
* vector has no components.
|
263 |
* @return the first component of this list
|
264 |
* @see Vector#firstElement()
|
265 |
*/
|
266 |
public Object firstElement() { |
267 |
return delegate.firstElement();
|
268 |
} |
269 |
|
270 |
/**
|
271 |
* Returns the last component of the list.
|
272 |
* Throws a <code>NoSuchElementException</code> if this vector
|
273 |
* has no components.
|
274 |
*
|
275 |
* @return the last component of the list
|
276 |
* @see Vector#lastElement()
|
277 |
*/
|
278 |
public Object lastElement() { |
279 |
return delegate.lastElement();
|
280 |
} |
281 |
|
282 |
/**
|
283 |
* Sets the component at the specified <code>index</code> of this
|
284 |
* list to be the specified object. The previous component at that
|
285 |
* position is discarded.
|
286 |
* <p>
|
287 |
* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
|
288 |
* is invalid.
|
289 |
* <blockquote>
|
290 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
291 |
* method to use is <code>set(int,Object)</code>, which implements the
|
292 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
293 |
* </blockquote>
|
294 |
*
|
295 |
* @param obj what the component is to be set to
|
296 |
* @param index the specified index
|
297 |
* @see #set(int,Object)
|
298 |
* @see Vector#setElementAt(Object,int)
|
299 |
*/
|
300 |
public void setElementAt(Object obj, int index) { |
301 |
delegate.setElementAt(obj, index); |
302 |
fireContentsChanged(this, index, index);
|
303 |
} |
304 |
|
305 |
/**
|
306 |
* Deletes the component at the specified index.
|
307 |
* <p>
|
308 |
* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
|
309 |
* is invalid.
|
310 |
* <blockquote>
|
311 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
312 |
* method to use is <code>remove(int)</code>, which implements the
|
313 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
314 |
* </blockquote>
|
315 |
*
|
316 |
* @param index the index of the object to remove
|
317 |
* @see #remove(int)
|
318 |
* @see Vector#removeElementAt(int)
|
319 |
*/
|
320 |
public void removeElementAt(int index) { |
321 |
delegate.removeElementAt(index); |
322 |
fireIntervalRemoved(this, index, index);
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Inserts the specified object as a component in this list at the
|
327 |
* specified <code>index</code>.
|
328 |
* <p>
|
329 |
* Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
|
330 |
* is invalid.
|
331 |
* <blockquote>
|
332 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
333 |
* method to use is <code>add(int,Object)</code>, which implements the
|
334 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
335 |
* </blockquote>
|
336 |
*
|
337 |
* @param obj the component to insert
|
338 |
* @param index where to insert the new component
|
339 |
* @exception ArrayIndexOutOfBoundsException if the index was invalid
|
340 |
* @see #add(int,Object)
|
341 |
* @see Vector#insertElementAt(Object,int)
|
342 |
*/
|
343 |
public void insertElementAt(Object obj, int index) { |
344 |
delegate.insertElementAt(obj, index); |
345 |
fireIntervalAdded(this, index, index);
|
346 |
} |
347 |
|
348 |
/**
|
349 |
* Adds the specified component to the end of this list.
|
350 |
*
|
351 |
* @param obj the component to be added
|
352 |
* @see Vector#addElement(Object)
|
353 |
*/
|
354 |
public void addElement(Object obj) { |
355 |
int index = delegate.size();
|
356 |
delegate.addElement(obj); |
357 |
fireIntervalAdded(this, index, index);
|
358 |
} |
359 |
|
360 |
/**
|
361 |
* Removes the first (lowest-indexed) occurrence of the argument
|
362 |
* from this list.
|
363 |
*
|
364 |
* @param obj the component to be removed
|
365 |
* @return <code>true</code> if the argument was a component of this
|
366 |
* list; <code>false</code> otherwise
|
367 |
* @see Vector#removeElement(Object)
|
368 |
*/
|
369 |
public boolean removeElement(Object obj) { |
370 |
int index = indexOf(obj);
|
371 |
boolean rv = delegate.removeElement(obj);
|
372 |
if (index >= 0) { |
373 |
fireIntervalRemoved(this, index, index);
|
374 |
} |
375 |
return rv;
|
376 |
} |
377 |
|
378 |
|
379 |
/**
|
380 |
* Removes all components from this list and sets its size to zero.
|
381 |
* <blockquote>
|
382 |
* <b>Note:</b> Although this method is not deprecated, the preferred
|
383 |
* method to use is <code>clear</code>, which implements the
|
384 |
* <code>List</code> interface defined in the 1.2 Collections framework.
|
385 |
* </blockquote>
|
386 |
*
|
387 |
* @see #clear()
|
388 |
* @see Vector#removeAllElements()
|
389 |
*/
|
390 |
public void removeAllElements() { |
391 |
int index1 = delegate.size()-1; |
392 |
delegate.removeAllElements(); |
393 |
if (index1 >= 0) { |
394 |
fireIntervalRemoved(this, 0, index1); |
395 |
} |
396 |
} |
397 |
|
398 |
|
399 |
/**
|
400 |
* Returns a string that displays and identifies this
|
401 |
* object's properties.
|
402 |
*
|
403 |
* @return a String representation of this object
|
404 |
*/
|
405 |
public String toString() { |
406 |
return delegate.toString();
|
407 |
} |
408 |
|
409 |
|
410 |
/* The remaining methods are included for compatibility with the
|
411 |
* Java 2 platform Vector class.
|
412 |
*/
|
413 |
|
414 |
/**
|
415 |
* Returns an array containing all of the elements in this list in the
|
416 |
* correct order.
|
417 |
*
|
418 |
* @return an array containing the elements of the list
|
419 |
* @see Vector#toArray()
|
420 |
*/
|
421 |
public Object[] toArray() { |
422 |
Object[] rv = new Object[delegate.size()]; |
423 |
delegate.copyInto(rv); |
424 |
return rv;
|
425 |
} |
426 |
|
427 |
/**
|
428 |
* Returns the element at the specified position in this list.
|
429 |
* <p>
|
430 |
* Throws an <code>ArrayIndexOutOfBoundsException</code>
|
431 |
* if the index is out of range
|
432 |
* (<code>index < 0 || index >= size()</code>).
|
433 |
*
|
434 |
* @param index index of element to return
|
435 |
*/
|
436 |
public Object get(int index) { |
437 |
return delegate.elementAt(index);
|
438 |
} |
439 |
|
440 |
/**
|
441 |
* Replaces the element at the specified position in this list with the
|
442 |
* specified element.
|
443 |
* <p>
|
444 |
* Throws an <code>ArrayIndexOutOfBoundsException</code>
|
445 |
* if the index is out of range
|
446 |
* (<code>index < 0 || index >= size()</code>).
|
447 |
*
|
448 |
* @param index index of element to replace
|
449 |
* @param element element to be stored at the specified position
|
450 |
* @return the element previously at the specified position
|
451 |
*/
|
452 |
public Object set(int index, Object element) { |
453 |
Object rv = delegate.elementAt(index);
|
454 |
delegate.setElementAt(element, index); |
455 |
fireContentsChanged(this, index, index);
|
456 |
return rv;
|
457 |
} |
458 |
|
459 |
/**
|
460 |
* Inserts the specified element at the specified position in this list.
|
461 |
* <p>
|
462 |
* Throws an <code>ArrayIndexOutOfBoundsException</code> if the
|
463 |
* index is out of range
|
464 |
* (<code>index < 0 || index > size()</code>).
|
465 |
*
|
466 |
* @param index index at which the specified element is to be inserted
|
467 |
* @param element element to be inserted
|
468 |
*/
|
469 |
public void add(int index, Object element) { |
470 |
delegate.insertElementAt(element, index); |
471 |
fireIntervalAdded(this, index, index);
|
472 |
} |
473 |
|
474 |
/**
|
475 |
* Removes the element at the specified position in this list.
|
476 |
* Returns the element that was removed from the list.
|
477 |
* <p>
|
478 |
* Throws an <code>ArrayIndexOutOfBoundsException</code>
|
479 |
* if the index is out of range
|
480 |
* (<code>index < 0 || index >= size()</code>).
|
481 |
*
|
482 |
* @param index the index of the element to removed
|
483 |
*/
|
484 |
public Object remove(int index) { |
485 |
Object rv = delegate.elementAt(index);
|
486 |
delegate.removeElementAt(index); |
487 |
fireIntervalRemoved(this, index, index);
|
488 |
return rv;
|
489 |
} |
490 |
|
491 |
/**
|
492 |
* Removes all of the elements from this list. The list will
|
493 |
* be empty after this call returns (unless it throws an exception).
|
494 |
*/
|
495 |
public void clear() { |
496 |
int index1 = delegate.size()-1; |
497 |
delegate.removeAllElements(); |
498 |
if (index1 >= 0) { |
499 |
fireIntervalRemoved(this, 0, index1); |
500 |
} |
501 |
} |
502 |
|
503 |
/**
|
504 |
* Deletes the components at the specified range of indexes.
|
505 |
* The removal is inclusive, so specifying a range of (1,5)
|
506 |
* removes the component at index 1 and the component at index 5,
|
507 |
* as well as all components in between.
|
508 |
* <p>
|
509 |
* Throws an <code>ArrayIndexOutOfBoundsException</code>
|
510 |
* if the index was invalid.
|
511 |
* Throws an <code>IllegalArgumentException</code> if
|
512 |
* <code>fromIndex > toIndex</code>.
|
513 |
*
|
514 |
* @param fromIndex the index of the lower end of the range
|
515 |
* @param toIndex the index of the upper end of the range
|
516 |
* @see #remove(int)
|
517 |
*/
|
518 |
public void removeRange(int fromIndex, int toIndex) { |
519 |
for(int i = toIndex; i >= fromIndex; i--) { |
520 |
delegate.removeElementAt(i); |
521 |
} |
522 |
fireIntervalRemoved(this, fromIndex, toIndex);
|
523 |
} |
524 |
|
525 |
/*
|
526 |
public void addAll(Collection c) {
|
527 |
}
|
528 |
|
529 |
public void addAll(int index, Collection c) {
|
530 |
}
|
531 |
*/
|
532 |
} |