- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathDefaultListModel.java
573 lines (526 loc) · 18.8 KB
/
DefaultListModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
packagejavax.swing;
importjava.util.Vector;
importjava.util.Collection;
importjava.util.Enumeration;
/**
* This class loosely implements the {@code java.util.Vector}
* API, in that it implements the 1.1.x version of
* {@code java.util.Vector}, has no collection class support,
* and notifies the {@code ListDataListener}s when changes occur.
* Presently it delegates to a {@code Vector},
* in a future release it will be a real Collection implementation.
* <p>
* <strong>Warning:</strong>
* Serialized objects of this class will not be compatible with
* future Swing releases. The current serialization support is
* appropriate for short term storage or RMI between applications running
* the same version of Swing. As of 1.4, support for long term storage
* of all JavaBeans
* has been added to the {@code java.beans} package.
* Please see {@link java.beans.XMLEncoder}.
*
* @param <E> the type of the elements of this model
*
* @author Hans Muller
* @since 1.2
*/
@SuppressWarnings("serial") // Same-version serialization only
publicclassDefaultListModel<E> extendsAbstractListModel<E>
{
privateVector<E> delegate = newVector<E>();
/**
* Constructs a {@code DefaultListModel}.
*/
publicDefaultListModel() {}
/**
* Returns the number of components in this list.
* <p>
* This method is identical to {@code size}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* This method exists in conjunction with {@code setSize} so that
* {@code size} is identifiable as a JavaBean property.
*
* @return the number of components in this list
* @see #size()
*/
publicintgetSize() {
returndelegate.size();
}
/**
* Returns the component at the specified index.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code get(int)}, which implements the {@code List}
* interface defined in the 1.2 Collections framework.
* </blockquote>
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException if the {@code index}
* is negative or greater than the current size of this
* list
* @see #get(int)
*/
publicEgetElementAt(intindex) {
returndelegate.elementAt(index);
}
/**
* Copies the components of this list into the specified array.
* The array must be big enough to hold all the objects in this list,
* else an {@code IndexOutOfBoundsException} is thrown.
*
* @param anArray the array into which the components get copied
* @see Vector#copyInto(Object[])
*/
publicvoidcopyInto(Object[] anArray) {
delegate.copyInto(anArray);
}
/**
* Trims the capacity of this list to be the list's current size.
*
* @see Vector#trimToSize()
*/
publicvoidtrimToSize() {
delegate.trimToSize();
}
/**
* Increases the capacity of this list, if necessary, to ensure
* that it can hold at least the number of components specified by
* the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
* @see Vector#ensureCapacity(int)
*/
publicvoidensureCapacity(intminCapacity) {
delegate.ensureCapacity(minCapacity);
}
/**
* Sets the size of this list.
*
* @param newSize the new size of this list
* @see Vector#setSize(int)
*/
publicvoidsetSize(intnewSize) {
intoldSize = delegate.size();
delegate.setSize(newSize);
if (oldSize > newSize) {
fireIntervalRemoved(this, newSize, oldSize-1);
}
elseif (oldSize < newSize) {
fireIntervalAdded(this, oldSize, newSize-1);
}
}
/**
* Returns the current capacity of this list.
*
* @return the current capacity
* @see Vector#capacity()
*/
publicintcapacity() {
returndelegate.capacity();
}
/**
* Returns the number of components in this list.
*
* @return the number of components in this list
* @see Vector#size()
*/
publicintsize() {
returndelegate.size();
}
/**
* Tests whether this list has any components.
*
* @return {@code true} if and only if this list has
* no components, that is, its size is zero;
* {@code false} otherwise
* @see Vector#isEmpty()
*/
publicbooleanisEmpty() {
returndelegate.isEmpty();
}
/**
* Returns an enumeration of the components of this list.
*
* @return an enumeration of the components of this list
* @see Vector#elements()
*/
publicEnumeration<E> elements() {
returndelegate.elements();
}
/**
* Tests whether the specified object is a component in this list.
*
* @param elem an object
* @return {@code true} if the specified object
* is the same as a component in this list
* @see Vector#contains(Object)
*/
publicbooleancontains(Objectelem) {
returndelegate.contains(elem);
}
/**
* Searches for the first occurrence of {@code elem}.
*
* @param elem an object
* @return the index of the first occurrence of the argument in this
* list; returns {@code -1} if the object is not found
* @see Vector#indexOf(Object)
*/
publicintindexOf(Objectelem) {
returndelegate.indexOf(elem);
}
/**
* Searches for the first occurrence of {@code elem}, beginning
* the search at {@code index}.
*
* @param elem the desired component
* @param index the index from which to begin searching
* @return the index where the first occurrence of {@code elem}
* is found after {@code index}; returns {@code -1}
* if the {@code elem} is not found in the list
* @see Vector#indexOf(Object,int)
*/
publicintindexOf(Objectelem, intindex) {
returndelegate.indexOf(elem, index);
}
/**
* Returns the index of the last occurrence of {@code elem}.
*
* @param elem the desired component
* @return the index of the last occurrence of {@code elem}
* in the list; returns {@code elem} if the object is not found
* @see Vector#lastIndexOf(Object)
*/
publicintlastIndexOf(Objectelem) {
returndelegate.lastIndexOf(elem);
}
/**
* Searches backwards for {@code elem}, starting from the
* specified index, and returns an index to it.
*
* @param elem the desired component
* @param index the index to start searching from
* @return the index of the last occurrence of the {@code elem}
* in this list at position less than {@code index};
* returns {@code -1} if the object is not found
* @see Vector#lastIndexOf(Object,int)
*/
publicintlastIndexOf(Objectelem, intindex) {
returndelegate.lastIndexOf(elem, index);
}
/**
* Returns the component at the specified index.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code get(int)}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* </blockquote>
*
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException if the index
* is negative or not less than the size of the list
* @see #get(int)
* @see Vector#elementAt(int)
*/
publicEelementAt(intindex) {
returndelegate.elementAt(index);
}
/**
* Returns the first component of this list.
* @return the first component of this list
* @see Vector#firstElement()
* @throws java.util.NoSuchElementException if this
* vector has no components
*/
publicEfirstElement() {
returndelegate.firstElement();
}
/**
* Returns the last component of the list.
*
* @return the last component of the list
* @see Vector#lastElement()
* @throws java.util.NoSuchElementException if this vector
* has no components
*/
publicElastElement() {
returndelegate.lastElement();
}
/**
* Sets the component at the specified {@code index} of this
* list to be the specified element. The previous component at that
* position is discarded.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code set(int,Object)}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* </blockquote>
*
* @param element what the component is to be set to
* @param index the specified index
* @throws ArrayIndexOutOfBoundsException if the index is invalid
* @see #set(int,Object)
* @see Vector#setElementAt(Object,int)
*/
publicvoidsetElementAt(Eelement, intindex) {
delegate.setElementAt(element, index);
fireContentsChanged(this, index, index);
}
/**
* Deletes the component at the specified index.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code remove(int)}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* </blockquote>
*
* @param index the index of the object to remove
* @see #remove(int)
* @see Vector#removeElementAt(int)
* @throws ArrayIndexOutOfBoundsException if the index is invalid
*/
publicvoidremoveElementAt(intindex) {
delegate.removeElementAt(index);
fireIntervalRemoved(this, index, index);
}
/**
* Inserts the specified element as a component in this list at the
* specified <code>index</code>.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code add(int,Object)}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* </blockquote>
*
* @param element the component to insert
* @param index where to insert the new component
* @throws ArrayIndexOutOfBoundsException if the index was invalid
* @see #add(int,Object)
* @see Vector#insertElementAt(Object,int)
*/
publicvoidinsertElementAt(Eelement, intindex) {
delegate.insertElementAt(element, index);
fireIntervalAdded(this, index, index);
}
/**
* Adds the specified component to the end of this list.
*
* @param element the component to be added
* @see Vector#addElement(Object)
*/
publicvoidaddElement(Eelement) {
intindex = delegate.size();
delegate.addElement(element);
fireIntervalAdded(this, index, index);
}
/**
* Removes the first (lowest-indexed) occurrence of the argument
* from this list.
*
* @param obj the component to be removed
* @return {@code true} if the argument was a component of this
* list; {@code false} otherwise
* @see Vector#removeElement(Object)
*/
publicbooleanremoveElement(Objectobj) {
intindex = indexOf(obj);
booleanrv = delegate.removeElement(obj);
if (index >= 0) {
fireIntervalRemoved(this, index, index);
}
returnrv;
}
/**
* Removes all components from this list and sets its size to zero.
* <blockquote>
* <b>Note:</b> Although this method is not deprecated, the preferred
* method to use is {@code clear}, which implements the
* {@code List} interface defined in the 1.2 Collections framework.
* </blockquote>
*
* @see #clear()
* @see Vector#removeAllElements()
*/
publicvoidremoveAllElements() {
intindex1 = delegate.size()-1;
delegate.removeAllElements();
if (index1 >= 0) {
fireIntervalRemoved(this, 0, index1);
}
}
/**
* Returns a string that displays and identifies this
* object's properties.
*
* @return a String representation of this object
*/
publicStringtoString() {
returndelegate.toString();
}
/* The remaining methods are included for compatibility with the
* Java 2 platform Vector class.
*/
/**
* Returns an array containing all of the elements in this list in the
* correct order.
*
* @return an array containing the elements of the list
* @see Vector#toArray()
*/
publicObject[] toArray() {
Object[] rv = newObject[delegate.size()];
delegate.copyInto(rv);
returnrv;
}
/**
* Returns the element at the specified position in this list.
*
* @param index index of element to return
* @return the element at the specified position in this list
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
publicEget(intindex) {
returndelegate.elementAt(index);
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of element to replace
* @param element element to be stored at the specified position
* @return the element previously at the specified position
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
publicEset(intindex, Eelement) {
Erv = delegate.elementAt(index);
delegate.setElementAt(element, index);
fireContentsChanged(this, index, index);
returnrv;
}
/**
* Inserts the specified element at the specified position in this list.
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index > size()})
*/
publicvoidadd(intindex, Eelement) {
delegate.insertElementAt(element, index);
fireIntervalAdded(this, index, index);
}
/**
* Removes the element at the specified position in this list.
* Returns the element that was removed from the list
*
* @param index the index of the element to removed
* @return the element previously at the specified position
* @throws ArrayIndexOutOfBoundsException if the index is out of range
* ({@code index < 0 || index >= size()})
*/
publicEremove(intindex) {
Erv = delegate.elementAt(index);
delegate.removeElementAt(index);
fireIntervalRemoved(this, index, index);
returnrv;
}
/**
* Removes all of the elements from this list. The list will
* be empty after this call returns (unless it throws an exception).
*/
publicvoidclear() {
intindex1 = delegate.size()-1;
delegate.removeAllElements();
if (index1 >= 0) {
fireIntervalRemoved(this, 0, index1);
}
}
/**
* Deletes the components at the specified range of indexes.
* The removal is inclusive, so specifying a range of (1,5)
* removes the component at index 1 and the component at index 5,
* as well as all components in between.
*
* @param fromIndex the index of the lower end of the range
* @param toIndex the index of the upper end of the range
* @throws ArrayIndexOutOfBoundsException if the index was invalid
* @throws IllegalArgumentException if {@code fromIndex > toIndex}
* @see #remove(int)
*/
publicvoidremoveRange(intfromIndex, inttoIndex) {
if (fromIndex > toIndex) {
thrownewIllegalArgumentException("fromIndex must be <= toIndex");
}
for(inti = toIndex; i >= fromIndex; i--) {
delegate.removeElementAt(i);
}
fireIntervalRemoved(this, fromIndex, toIndex);
}
/**
* Adds all of the elements present in the collection to the list.
*
* @param c the collection which contains the elements to add
* @throws NullPointerException if {@code c} is null
*
* @since 11
*/
publicvoidaddAll(Collection<? extendsE> c) {
if (c.isEmpty()) {
return;
}
intstartIndex = getSize();
delegate.addAll(c);
fireIntervalAdded(this, startIndex, getSize() - 1);
}
/**
* Adds all of the elements present in the collection, starting
* from the specified index.
*
* @param index index at which to insert the first element from the
* specified collection
* @param c the collection which contains the elements to add
* @throws ArrayIndexOutOfBoundsException if {@code index} does not
* fall within the range of number of elements currently held
* @throws NullPointerException if {@code c} is null
*
* @since 11
*/
publicvoidaddAll(intindex, Collection<? extendsE> c) {
if (index < 0 || index > getSize()) {
thrownewArrayIndexOutOfBoundsException("index out of range: " +
index);
}
if (c.isEmpty()) {
return;
}
delegate.addAll(index, c);
fireIntervalAdded(this, index, index + c.size() - 1);
}
}