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