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