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