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 * Returns the number of components in this list. 62 * <p> 63 * This method is identical to {@code size}, which implements the 64 * {@code List} interface defined in the 1.2 Collections framework. 65 * This method exists in conjunction with {@code setSize} so that 66 * {@code size} 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)}, which implements the {@code List} 80 * 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 * @throws ArrayIndexOutOfBoundsException if the {@code index} 85 * is negative or greater than the current size of this 86 * list 87 * @see #get(int) 88 */ 89 public E getElementAt(int index) { 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} 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} if and only if this list has 167 * no components, that is, its size is zero; 168 * {@code false} 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 */ 181 public Enumeration<E> elements() { 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} 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}. 199 * 200 * @param elem an object 201 * @return the index of the first occurrence of the argument in this 202 * list; returns {@code -1} 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}, beginning 211 * the search at {@code index}. 212 * 213 * @param elem the desired component 214 * @param index the index from which to begin searching 215 * @return the index where the first occurrence of {@code elem} 216 * is found after {@code index}; returns {@code -1} 217 * if the {@code elem} 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}. 226 * 227 * @param elem the desired component 228 * @return the index of the last occurrence of {@code elem} 229 * in the list; returns {@code elem} 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}, 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} 243 * in this list at position less than {@code index}; 244 * returns {@code -1} 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 * <blockquote> 254 * <b>Note:</b> Although this method is not deprecated, the preferred 255 * method to use is {@code get(int)}, which implements the 256 * {@code List} interface defined in the 1.2 Collections framework. 257 * </blockquote> 258 * 259 * @param index an index into this list 260 * @return the component at the specified index 261 * @throws ArrayIndexOutOfBoundsException if the index 262 * is negative or not less than the size of the list 263 * @see #get(int) 264 * @see Vector#elementAt(int) 265 */ 266 public E elementAt(int index) { 267 return delegate.elementAt(index); 268 } 269 270 /** 271 * Returns the first component of this list. 272 * @return the first component of this list 273 * @see Vector#firstElement() 274 * @throws NoSuchElementException if this 275 * vector has no components 276 */ 277 public E firstElement() { 278 return delegate.firstElement(); 279 } 280 281 /** 282 * Returns the last component of the list. 283 * 284 * @return the last component of the list 285 * @see Vector#lastElement() 286 * @throws NoSuchElementException if this vector 287 * has no components 288 */ 289 public E lastElement() { 290 return delegate.lastElement(); 291 } 292 293 /** 294 * Sets the component at the specified {@code index} of this 295 * list to be the specified element. The previous component at that 296 * position is discarded. 297 * <blockquote> 298 * <b>Note:</b> Although this method is not deprecated, the preferred 299 * method to use is {@code set(int,Object)}, which implements the 300 * {@code List} interface defined in the 1.2 Collections framework. 301 * </blockquote> 302 * 303 * @param element what the component is to be set to 304 * @param index the specified index 305 * @throws ArrayIndexOutOfBoundsException if the index is invalid 306 * @see #set(int,Object) 307 * @see Vector#setElementAt(Object,int) 308 */ 309 public void setElementAt(E element, int index) { 310 delegate.setElementAt(element, index); 311 fireContentsChanged(this, index, index); 312 } 313 314 /** 315 * Deletes the component at the specified index. 316 * <blockquote> 317 * <b>Note:</b> Although this method is not deprecated, the preferred 318 * method to use is {@code remove(int)}, which implements the 319 * {@code List} interface defined in the 1.2 Collections framework. 320 * </blockquote> 321 * 322 * @param index the index of the object to remove 323 * @see #remove(int) 324 * @see Vector#removeElementAt(int) 325 * @throws ArrayIndexOutOfBoundsException if the index is invalid 326 */ 327 public void removeElementAt(int index) { 328 delegate.removeElementAt(index); 329 fireIntervalRemoved(this, index, index); 330 } 331 332 /** 333 * Inserts the specified element as a component in this list at the 334 * specified <code>index</code>. 335 * <blockquote> 336 * <b>Note:</b> Although this method is not deprecated, the preferred 337 * method to use is {@code add(int,Object)}, which implements the 338 * {@code List} interface defined in the 1.2 Collections framework. 339 * </blockquote> 340 * 341 * @param element the component to insert 342 * @param index where to insert the new component 343 * @exception ArrayIndexOutOfBoundsException if the index was invalid 344 * @see #add(int,Object) 345 * @see Vector#insertElementAt(Object,int) 346 */ 347 public void insertElementAt(E element, int index) { 348 delegate.insertElementAt(element, index); 349 fireIntervalAdded(this, index, index); 350 } 351 352 /** 353 * Adds the specified component to the end of this list. 354 * 355 * @param element the component to be added 356 * @see Vector#addElement(Object) 357 */ 358 public void addElement(E element) { 359 int index = delegate.size(); 360 delegate.addElement(element); 361 fireIntervalAdded(this, index, index); 362 } 363 364 /** 365 * Removes the first (lowest-indexed) occurrence of the argument 366 * from this list. 367 * 368 * @param obj the component to be removed 369 * @return {@code true} if the argument was a component of this 370 * list; {@code false} otherwise 371 * @see Vector#removeElement(Object) 372 */ 373 public boolean removeElement(Object obj) { 374 int index = indexOf(obj); 375 boolean rv = delegate.removeElement(obj); 376 if (index >= 0) { 377 fireIntervalRemoved(this, index, index); 378 } 379 return rv; 380 } 381 382 383 /** 384 * Removes all components from this list and sets its size to zero. 385 * <blockquote> 386 * <b>Note:</b> Although this method is not deprecated, the preferred 387 * method to use is {@code clear}, which implements the 388 * {@code List} interface defined in the 1.2 Collections framework. 389 * </blockquote> 390 * 391 * @see #clear() 392 * @see Vector#removeAllElements() 393 */ 394 public void removeAllElements() { 395 int index1 = delegate.size()-1; 396 delegate.removeAllElements(); 397 if (index1 >= 0) { 398 fireIntervalRemoved(this, 0, index1); 399 } 400 } 401 402 403 /** 404 * Returns a string that displays and identifies this 405 * object's properties. 406 * 407 * @return a String representation of this object 408 */ 409 public String toString() { 410 return delegate.toString(); 411 } 412 413 414 /* The remaining methods are included for compatibility with the 415 * Java 2 platform Vector class. 416 */ 417 418 /** 419 * Returns an array containing all of the elements in this list in the 420 * correct order. 421 * 422 * @return an array containing the elements of the list 423 * @see Vector#toArray() 424 */ 425 public Object[] toArray() { 426 Object[] rv = new Object[delegate.size()]; 427 delegate.copyInto(rv); 428 return rv; 429 } 430 431 /** 432 * Returns the element at the specified position in this list. 433 * 434 * @param index index of element to return 435 * @return the element at the specified position in this list 436 * @throws ArrayIndexOutOfBoundsException if the index is out of range 437 * ({@code index < 0 || index >= size()}) 438 */ 439 public E get(int index) { 440 return delegate.elementAt(index); 441 } 442 443 /** 444 * Replaces the element at the specified position in this list with the 445 * specified element. 446 * 447 * @param index index of element to replace 448 * @param element element to be stored at the specified position 449 * @return the element previously at the specified position 450 * @throws ArrayIndexOutOfBoundsException if the index is out of range 451 * ({@code index < 0 || index >= size()}) 452 */ 453 public E set(int index, E element) { 454 E rv = delegate.elementAt(index); 455 delegate.setElementAt(element, index); 456 fireContentsChanged(this, index, index); 457 return rv; 458 } 459 460 /** 461 * Inserts the specified element at the specified position in this list. 462 * 463 * @param index index at which the specified element is to be inserted 464 * @param element element to be inserted 465 * @throws ArrayIndexOutOfBoundsException if the index is out of range 466 * ({@code index < 0 || index > size()}) 467 */ 468 public void add(int index, E element) { 469 delegate.insertElementAt(element, index); 470 fireIntervalAdded(this, index, index); 471 } 472 473 /** 474 * Removes the element at the specified position in this list. 475 * Returns the element that was removed from the list 476 * 477 * @param index the index of the element to removed 478 * @return the element previously at the specified position 479 * @throws ArrayIndexOutOfBoundsException if the index is out of range 480 * ({@code index < 0 || index >= size()}) 481 */ 482 public E remove(int index) { 483 E rv = delegate.elementAt(index); 484 delegate.removeElementAt(index); 485 fireIntervalRemoved(this, index, index); 486 return rv; 487 } 488 489 /** 490 * Removes all of the elements from this list. The list will 491 * be empty after this call returns (unless it throws an exception). 492 */ 493 public void clear() { 494 int index1 = delegate.size()-1; 495 delegate.removeAllElements(); 496 if (index1 >= 0) { 497 fireIntervalRemoved(this, 0, index1); 498 } 499 } 500 501 /** 502 * Deletes the components at the specified range of indexes. 503 * The removal is inclusive, so specifying a range of (1,5) 504 * removes the component at index 1 and the component at index 5, 505 * as well as all components in between. 506 * 507 * @param fromIndex the index of the lower end of the range 508 * @param toIndex the index of the upper end of the range 509 * @throws ArrayIndexOutOfBoundsException if the index was invalid 510 * @throws IllegalArgumentException if {@code fromIndex > toIndex} 511 * @see #remove(int) 512 */ 513 public void removeRange(int fromIndex, int toIndex) { 514 if (fromIndex > toIndex) { 515 throw new IllegalArgumentException("fromIndex must be <= toIndex"); 516 } 517 for(int i = toIndex; i >= fromIndex; i--) { 518 delegate.removeElementAt(i); 519 } 520 fireIntervalRemoved(this, fromIndex, toIndex); 521 } 522 523 /** 524 * Adds all of the elements present in the collection to the list. 525 * 526 * @param c the collection which contains the elements to add 527 * @throws NullPointerException if {@code c} is null 528 */ 529 public void addAll(Collection<? extends E> c) { 530 if (c.isEmpty()) { 531 return; 532 } 533 534 int startIndex = getSize(); 535 536 delegate.addAll(c); 537 fireIntervalAdded(this, startIndex, getSize() - 1); 538 } 539 540 /** 541 * Adds all of the elements present in the collection, starting 542 * from the specified index. 543 * 544 * @param index index at which to insert the first element from the 545 * specified collection 546 * @param c the collection which contains the elements to add 547 * @throws ArrayIndexOutOfBoundsException if {@code index} does not 548 * fall within the range of number of elements currently held 549 * @throws NullPointerException if {@code c} is null 550 */ 551 public void addAll(int index, Collection<? extends E> c) { 552 if (index < 0 || index > getSize()) { 553 throw new ArrayIndexOutOfBoundsException("index out of range: " + 554 index); 555 } 556 557 if (c.isEmpty()) { 558 return; 559 } 560 561 delegate.addAll(index, c); 562 fireIntervalAdded(this, index, index + c.size() - 1); 563 } 564 }