1. /*
  2. * @(#)DefaultListModel.java 1.26 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing;
  11. import java.util.Vector;
  12. import java.util.Enumeration;
  13. import javax.swing.event.*;
  14. /**
  15. * This class implements the java.util.Vector API and notifies
  16. * the ListDataListeners when changes occur. Presently
  17. * it delegates to a Vector, in a future release it will be
  18. * a real Collection implementation.
  19. * <p>
  20. * <strong>Warning:</strong>
  21. * Serialized objects of this class will not be compatible with
  22. * future Swing releases. The current serialization support is appropriate
  23. * for short term storage or RMI between applications running the same
  24. * version of Swing. A future release of Swing will provide support for
  25. * long term persistence.
  26. *
  27. * @version 1.26 02/02/00
  28. * @author Hans Muller
  29. */
  30. public class DefaultListModel extends AbstractListModel
  31. {
  32. private Vector delegate = new Vector();
  33. /**
  34. * Returns the number of components in this list.
  35. * <p>
  36. * This method is identical to <tt>size()</tt>, which implements the
  37. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  38. * This method exists in conjunction with <tt>setSize()</tt> so that
  39. * "size" is identifiable as a JavaBean property.
  40. *
  41. * @return the number of components in this list.
  42. * @see #size()
  43. */
  44. public int getSize() {
  45. return delegate.size();
  46. }
  47. /**
  48. * Returns the component at the specified index.
  49. * <blockquote>
  50. * <b>Note:</b> Although this method is not deprecated, the preferred
  51. * method to use is <tt>get(int)</tt>, which implements the
  52. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  53. * </blockquote>
  54. * @param index an index into this list.
  55. * @return the component at the specified index.
  56. * @exception ArrayIndexOutOfBoundsException if the <tt>index</tt>
  57. * is negative or not less than the current size of this
  58. * list.
  59. * given.
  60. * @see #get(int)
  61. */
  62. public Object getElementAt(int index) {
  63. return delegate.elementAt(index);
  64. }
  65. /**
  66. * Copies the components of this list into the specified array.
  67. * The array must be big enough to hold all the objects in this list,
  68. * else an <tt>IndexOutOfBoundsException</tt> is thrown.
  69. *
  70. * @param anArray the array into which the components get copied.
  71. * @see Vector#copyInto(Object[])
  72. */
  73. public void copyInto(Object anArray[]) {
  74. delegate.copyInto(anArray);
  75. }
  76. /**
  77. * Trims the capacity of this list to be the list's current size.
  78. *
  79. * @see Vector#trimToSize()
  80. */
  81. public void trimToSize() {
  82. delegate.trimToSize();
  83. }
  84. /**
  85. * Increases the capacity of this list, if necessary, to ensure
  86. * that it can hold at least the number of components specified by
  87. * the minimum capacity argument.
  88. *
  89. * @param minCapacity the desired minimum capacity.
  90. * @see Vector#ensureCapacity(int)
  91. */
  92. public void ensureCapacity(int minCapacity) {
  93. delegate.ensureCapacity(minCapacity);
  94. }
  95. /**
  96. * Sets the size of this list.
  97. *
  98. * @param newSize the new size of this list.
  99. * @see Vector#setSize(int)
  100. */
  101. public void setSize(int newSize) {
  102. int oldSize = delegate.size();
  103. delegate.setSize(newSize);
  104. if (oldSize > newSize) {
  105. fireIntervalRemoved(this, newSize, oldSize-1);
  106. }
  107. else if (oldSize < newSize) {
  108. fireIntervalAdded(this, oldSize, newSize-1);
  109. }
  110. }
  111. /**
  112. * Returns the current capacity of this list.
  113. *
  114. * @return the current capacity
  115. * @see Vector#capacity()
  116. */
  117. public int capacity() {
  118. return delegate.capacity();
  119. }
  120. /**
  121. * Returns the number of components in this list.
  122. *
  123. * @return the number of components in this list.
  124. * @see Vector#size()
  125. */
  126. public int size() {
  127. return delegate.size();
  128. }
  129. /**
  130. * Tests if this list has no components.
  131. *
  132. * @return <code>true</code> if and only if this list has
  133. * no components, that is, its size is zero;
  134. * <code>false</code> otherwise.
  135. * @see Vector#isEmpty()
  136. */
  137. public boolean isEmpty() {
  138. return delegate.isEmpty();
  139. }
  140. /**
  141. * Returns an enumeration of the components of this list.
  142. *
  143. * @return an enumeration of the components of this list.
  144. * @see Vector#elements()
  145. */
  146. public Enumeration elements() {
  147. return delegate.elements();
  148. }
  149. /**
  150. * Tests if the specified object is a component in this list.
  151. *
  152. * @param elem an object.
  153. * @return <code>true</code> if the specified object
  154. * is the same as a component in this list
  155. * @see Vector#contains(Object)
  156. */
  157. public boolean contains(Object elem) {
  158. return delegate.contains(elem);
  159. }
  160. /**
  161. * Searches for the first occurence of the given argument.
  162. *
  163. * @param elem an object.
  164. * @return the index of the first occurrence of the argument in this
  165. * list; returns <code>-1</code> if the object is not found.
  166. * @see Vector#indexOf(Object)
  167. */
  168. public int indexOf(Object elem) {
  169. return delegate.indexOf(elem);
  170. }
  171. /**
  172. * Searches for the first occurence of the given argument, beginning
  173. * the search at <code>index</code>.
  174. *
  175. * @param elem an object.
  176. * @param index the index to start searching from.
  177. * @return the index of the first occurrence of the object argument in
  178. * this list at position <code>index</code> or later in the
  179. * list; returns <code>-1</code> if the object is not found.
  180. * @see Vector#indexOf(Object,int)
  181. */
  182. public int indexOf(Object elem, int index) {
  183. return delegate.indexOf(elem, index);
  184. }
  185. /**
  186. * Returns the index of the last occurrence of the specified object in
  187. * this list.
  188. *
  189. * @param elem the desired component.
  190. * @return the index of the last occurrence of the specified object in
  191. * this list; returns <code>-1</code> if the object is not found.
  192. * @see Vector#lastIndexOf(Object)
  193. */
  194. public int lastIndexOf(Object elem) {
  195. return delegate.lastIndexOf(elem);
  196. }
  197. /**
  198. * Searches backwards for the specified object, starting from the
  199. * specified index, and returns an index to it.
  200. *
  201. * @param elem the desired component.
  202. * @param index the index to start searching from.
  203. * @return the index of the last occurrence of the specified object in this
  204. * list at position less than <code>index</code> in the
  205. * list; <code>-1</code> if the object is not found.
  206. * @see Vector#lastIndexOf(Object,int)
  207. */
  208. public int lastIndexOf(Object elem, int index) {
  209. return delegate.lastIndexOf(elem, index);
  210. }
  211. /**
  212. * Returns the component at the specified index.
  213. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index
  214. * is negative or not less than the size of the list.
  215. * <blockquote>
  216. * <b>Note:</b> Although this method is not deprecated, the preferred
  217. * method to use is <tt>get(int)</tt>, which implements the
  218. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  219. * </blockquote>
  220. *
  221. * @param index an index into this list.
  222. * @return the component at the specified index.
  223. * @see #get(int)
  224. * @see Vector#elementAt(int)
  225. */
  226. public Object elementAt(int index) {
  227. return delegate.elementAt(index);
  228. }
  229. /**
  230. * Returns the first component of this list.
  231. * Throws a <tt>NoSuchElementException</tt> if this vector has no components *
  232. * @return the first component of this list
  233. * @see Vector#firstElement()
  234. */
  235. public Object firstElement() {
  236. return delegate.firstElement();
  237. }
  238. /**
  239. * Returns the last component of the list.
  240. * Throws a <tt>NoSuchElementException</tt> if this vector has no components *
  241. *
  242. * @return the last component of the list
  243. * @see Vector#lastElement()
  244. */
  245. public Object lastElement() {
  246. return delegate.lastElement();
  247. }
  248. /**
  249. * Sets the component at the specified <code>index</code> of this
  250. * list to be the specified object. The previous component at that
  251. * position is discarded.
  252. * <p>
  253. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index
  254. * is invalid.
  255. * <blockquote>
  256. * <b>Note:</b> Although this method is not deprecated, the preferred
  257. * method to use is <tt>set(int,Object)</tt>, which implements the
  258. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  259. * </blockquote>
  260. *
  261. * @param obj what the component is to be set to.
  262. * @param index the specified index.
  263. * @see #set(int,Object)
  264. * @see Vector#setElementAt(Object,int)
  265. */
  266. public void setElementAt(Object obj, int index) {
  267. delegate.setElementAt(obj, index);
  268. fireContentsChanged(this, index, index);
  269. }
  270. /**
  271. * Deletes the component at the specified index.
  272. * <p>
  273. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index
  274. * is invalid.
  275. * <blockquote>
  276. * <b>Note:</b> Although this method is not deprecated, the preferred
  277. * method to use is <tt>remove(int)</tt>, which implements the
  278. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  279. * </blockquote>
  280. *
  281. * @param index the index of the object to remove.
  282. * @see #remove(int)
  283. * @see Vector#removeElementAt(int)
  284. */
  285. public void removeElementAt(int index) {
  286. delegate.removeElementAt(index);
  287. fireIntervalRemoved(this, index, index);
  288. }
  289. /**
  290. * Inserts the specified object as a component in this list at the
  291. * specified <code>index</code>.
  292. * <p>
  293. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index
  294. * is invalid.
  295. * <blockquote>
  296. * <b>Note:</b> Although this method is not deprecated, the preferred
  297. * method to use is <tt>add(int,Object)</tt>, which implements the
  298. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  299. * </blockquote>
  300. *
  301. * @param obj the component to insert.
  302. * @param index where to insert the new component.
  303. * @exception ArrayIndexOutOfBoundsException if the index was invalid.
  304. * @see #add(int,Object)
  305. * @see Vector#insertElementAt(Object,int)
  306. */
  307. public void insertElementAt(Object obj, int index) {
  308. delegate.insertElementAt(obj, index);
  309. fireIntervalAdded(this, index, index);
  310. }
  311. /**
  312. * Adds the specified component to the end of this list.
  313. *
  314. * @param obj the component to be added.
  315. * @see Vector#addElement(Object)
  316. */
  317. public void addElement(Object obj) {
  318. int index = delegate.size();
  319. delegate.addElement(obj);
  320. fireIntervalAdded(this, index, index);
  321. }
  322. /**
  323. * Removes the first (lowest-indexed) occurrence of the argument
  324. * from this list.
  325. *
  326. * @param obj the component to be removed.
  327. * @return <code>true</code> if the argument was a component of this
  328. * list; <code>false</code> otherwise.
  329. * @see Vector#removeElement(Object)
  330. */
  331. public boolean removeElement(Object obj) {
  332. int index = indexOf(obj);
  333. boolean rv = delegate.removeElement(obj);
  334. if (index >= 0) {
  335. fireIntervalRemoved(this, index, index);
  336. }
  337. return rv;
  338. }
  339. /**
  340. * Removes all components from this list and sets its size to zero.
  341. * <blockquote>
  342. * <b>Note:</b> Although this method is not deprecated, the preferred
  343. * method to use is <tt>clear()</tt>, which implements the
  344. * <tt>List</tt> interface defined in the 1.2 Collections framework.
  345. * </blockquote>
  346. *
  347. * @see #clear()
  348. * @see Vector#removeAllElements()
  349. */
  350. public void removeAllElements() {
  351. int index1 = delegate.size()-1;
  352. delegate.removeAllElements();
  353. if (index1 >= 0) {
  354. fireIntervalRemoved(this, 0, index1);
  355. }
  356. }
  357. /**
  358. * Returns a string that displays and identifies this
  359. * object's properties.
  360. *
  361. * @return a String representation of this object
  362. */ public String toString() {
  363. return delegate.toString();
  364. }
  365. /* The remaining methods are included for compatibility with the
  366. * Java 2 platform Vector class.
  367. */
  368. /**
  369. * Returns an array containing all of the elements in this list in the
  370. * correct order.
  371. * <p>
  372. * Throws an <tt>ArrayStoreException</tt> if the runtime type of the array
  373. * a is not a supertype of the runtime type of every element in this list.
  374. *
  375. * @param a the array into which the elements of the list are to
  376. * be stored, if it is big enough; otherwise, a new array of the
  377. * same runtime type is allocated for this purpose.
  378. * @return an array containing the elements of the list.
  379. * @see Vector#toArray()
  380. */
  381. public Object[] toArray() {
  382. Object[] rv = new Object[delegate.size()];
  383. delegate.copyInto(rv);
  384. return rv;
  385. }
  386. /**
  387. * Returns the element at the specified position in this list.
  388. * <p>
  389. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index is out of range
  390. * (index < 0 || index >= size()).
  391. *
  392. * @param index index of element to return.
  393. */
  394. public Object get(int index) {
  395. return delegate.elementAt(index);
  396. }
  397. /**
  398. * Replaces the element at the specified position in this list with the
  399. * specified element.
  400. * <p>
  401. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index is out of range
  402. * (index < 0 || index >= size()).
  403. *
  404. * @param index index of element to replace.
  405. * @param element element to be stored at the specified position.
  406. * @return the element previously at the specified position.
  407. */
  408. public Object set(int index, Object element) {
  409. Object rv = delegate.elementAt(index);
  410. delegate.setElementAt(element, index);
  411. fireContentsChanged(this, index, index);
  412. return rv;
  413. }
  414. /**
  415. * Inserts the specified element at the specified position in this list.
  416. * <p>
  417. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index is out of range
  418. * (index < 0 || index >= size()).
  419. *
  420. * @param index index at which the specified element is to be inserted.
  421. * @param element element to be inserted.
  422. */
  423. public void add(int index, Object element) {
  424. delegate.insertElementAt(element, index);
  425. fireIntervalAdded(this, index, index);
  426. }
  427. /**
  428. * Removes the element at the specified position in this list.
  429. * Returns the element that was removed from the list.
  430. * <p>
  431. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index is out of range
  432. * (index < 0 || index >= size()).
  433. *
  434. * @param index the index of the element to removed.
  435. */
  436. public Object remove(int index) {
  437. Object rv = delegate.elementAt(index);
  438. delegate.removeElementAt(index);
  439. fireIntervalRemoved(this, index, index);
  440. return rv;
  441. }
  442. /**
  443. * Removes all of the elements from this list. The list will
  444. * be empty after this call returns (unless it throws an exception).
  445. */
  446. public void clear() {
  447. int index1 = delegate.size()-1;
  448. delegate.removeAllElements();
  449. if (index1 >= 0) {
  450. fireIntervalRemoved(this, 0, index1);
  451. }
  452. }
  453. /**
  454. * Deletes the components at the specified range of indexes.
  455. * The removal is inclusive, so specifying a range of (1,5)
  456. * removes the component at index 1 and the component at index 5,
  457. * as well as all components in between.
  458. * <p>
  459. * Throws an <tt>ArrayIndexOutOfBoundsException</tt> if the index was invalid.
  460. * Throws an <tt>IllegalArgumentException</tt> if <tt>fromIndex</tt>
  461. * > <tt>toIndex</tt>.
  462. *
  463. * @param fromIndex the index of the lower end of the range
  464. * @param toIndex the index of the upper end of the range
  465. * @see #remove(int)
  466. */
  467. public void removeRange(int fromIndex, int toIndex) {
  468. for(int i = toIndex; i >= fromIndex; i--) {
  469. delegate.removeElementAt(i);
  470. }
  471. fireIntervalRemoved(this, fromIndex, toIndex);
  472. }
  473. /*
  474. public void addAll(Collection c) {
  475. }
  476. public void addAll(int index, Collection c) {
  477. }
  478. */
  479. }