1. /*
  2. * @(#)DefaultListModel.java 1.32 03/01/23
  3. *
  4. * Copyright 2003 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 loosely implements the <code>java.util.Vector</code>
  13. * API, in that it implements the 1.1.x version of
  14. * <code>java.util.Vector</code>, has no collection class support,
  15. * and notifies the <code>ListDataListener</code>s when changes occur.
  16. * Presently it delegates to a <code>Vector</code>,
  17. * in a future release it will be a real Collection implementation.
  18. * <p>
  19. * <strong>Warning:</strong>
  20. * Serialized objects of this class will not be compatible with
  21. * future Swing releases. The current serialization support is
  22. * appropriate for short term storage or RMI between applications running
  23. * the same version of Swing. As of 1.4, support for long term storage
  24. * of all JavaBeans<sup><font size="-2">TM</font></sup>
  25. * has been added to the <code>java.beans</code> package.
  26. * Please see {@link java.beans.XMLEncoder}.
  27. *
  28. * @version 1.32 01/23/03
  29. * @author Hans Muller
  30. */
  31. public class DefaultListModel extends AbstractListModel
  32. {
  33. private Vector delegate = new Vector();
  34. /**
  35. * Returns the number of components in this list.
  36. * <p>
  37. * This method is identical to <code>size</code>, which implements the
  38. * <code>List</code> interface defined in the 1.2 Collections framework.
  39. * This method exists in conjunction with <code>setSize</code> so that
  40. * <code>size</code> is identifiable as a JavaBean property.
  41. *
  42. * @return the number of components in this list
  43. * @see #size()
  44. */
  45. public int getSize() {
  46. return delegate.size();
  47. }
  48. /**
  49. * Returns the component at the specified index.
  50. * <blockquote>
  51. * <b>Note:</b> Although this method is not deprecated, the preferred
  52. * method to use is <code>get(int)</code>, which implements the
  53. * <code>List</code> interface defined in the 1.2 Collections framework.
  54. * </blockquote>
  55. * @param index an index into this list
  56. * @return the component at the specified index
  57. * @exception ArrayIndexOutOfBoundsException if the <code>index</code>
  58. * is negative or greater than the current size of this
  59. * list
  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 <code>IndexOutOfBoundsException</code> 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 whether this list has any 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 whether 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 occurrence of <code>elem</code>.
  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 occurrence of <code>elem</code>, beginning
  173. * the search at <code>index</code>.
  174. *
  175. * @param elem an desired component
  176. * @param index the index from which to begin searching
  177. * @return the index where the first occurrence of <code>elem</code>
  178. * is found after <code>index</code> returns <code>-1</code>
  179. * if the <code>elem</code> is not found in the list
  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 <code>elem</code>.
  187. *
  188. * @param elem the desired component
  189. * @return the index of the last occurrence of <code>elem</code>
  190. * in the list; returns <code>-1</code> if the object is not found
  191. * @see Vector#lastIndexOf(Object)
  192. */
  193. public int lastIndexOf(Object elem) {
  194. return delegate.lastIndexOf(elem);
  195. }
  196. /**
  197. * Searches backwards for <code>elem</code>, starting from the
  198. * specified index, and returns an index to it.
  199. *
  200. * @param elem the desired component
  201. * @param index the index to start searching from
  202. * @return the index of the last occurrence of the <code>elem</code>
  203. * in this list at position less than <code>index</code>
  204. * returns <code>-1</code> if the object is not found
  205. * @see Vector#lastIndexOf(Object,int)
  206. */
  207. public int lastIndexOf(Object elem, int index) {
  208. return delegate.lastIndexOf(elem, index);
  209. }
  210. /**
  211. * Returns the component at the specified index.
  212. * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  213. * is negative or not less than the size of the list.
  214. * <blockquote>
  215. * <b>Note:</b> Although this method is not deprecated, the preferred
  216. * method to use is <code>get(int)</code>, which implements the
  217. * <code>List</code> interface defined in the 1.2 Collections framework.
  218. * </blockquote>
  219. *
  220. * @param index an index into this list
  221. * @return the component at the specified index
  222. * @see #get(int)
  223. * @see Vector#elementAt(int)
  224. */
  225. public Object elementAt(int index) {
  226. return delegate.elementAt(index);
  227. }
  228. /**
  229. * Returns the first component of this list.
  230. * Throws a <code>NoSuchElementException</code> if this
  231. * 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 <code>NoSuchElementException</code> if this vector
  241. * has no components.
  242. *
  243. * @return the last component of the list
  244. * @see Vector#lastElement()
  245. */
  246. public Object lastElement() {
  247. return delegate.lastElement();
  248. }
  249. /**
  250. * Sets the component at the specified <code>index</code> of this
  251. * list to be the specified object. The previous component at that
  252. * position is discarded.
  253. * <p>
  254. * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  255. * is invalid.
  256. * <blockquote>
  257. * <b>Note:</b> Although this method is not deprecated, the preferred
  258. * method to use is <code>set(int,Object)</code>, which implements the
  259. * <code>List</code> interface defined in the 1.2 Collections framework.
  260. * </blockquote>
  261. *
  262. * @param obj what the component is to be set to
  263. * @param index the specified index
  264. * @see #set(int,Object)
  265. * @see Vector#setElementAt(Object,int)
  266. */
  267. public void setElementAt(Object obj, int index) {
  268. delegate.setElementAt(obj, index);
  269. fireContentsChanged(this, index, index);
  270. }
  271. /**
  272. * Deletes the component at the specified index.
  273. * <p>
  274. * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  275. * is invalid.
  276. * <blockquote>
  277. * <b>Note:</b> Although this method is not deprecated, the preferred
  278. * method to use is <code>remove(int)</code>, which implements the
  279. * <code>List</code> interface defined in the 1.2 Collections framework.
  280. * </blockquote>
  281. *
  282. * @param index the index of the object to remove
  283. * @see #remove(int)
  284. * @see Vector#removeElementAt(int)
  285. */
  286. public void removeElementAt(int index) {
  287. delegate.removeElementAt(index);
  288. fireIntervalRemoved(this, index, index);
  289. }
  290. /**
  291. * Inserts the specified object as a component in this list at the
  292. * specified <code>index</code>.
  293. * <p>
  294. * Throws an <code>ArrayIndexOutOfBoundsException</code> if the index
  295. * is invalid.
  296. * <blockquote>
  297. * <b>Note:</b> Although this method is not deprecated, the preferred
  298. * method to use is <code>add(int,Object)</code>, which implements the
  299. * <code>List</code> interface defined in the 1.2 Collections framework.
  300. * </blockquote>
  301. *
  302. * @param obj the component to insert
  303. * @param index where to insert the new component
  304. * @exception ArrayIndexOutOfBoundsException if the index was invalid
  305. * @see #add(int,Object)
  306. * @see Vector#insertElementAt(Object,int)
  307. */
  308. public void insertElementAt(Object obj, int index) {
  309. delegate.insertElementAt(obj, index);
  310. fireIntervalAdded(this, index, index);
  311. }
  312. /**
  313. * Adds the specified component to the end of this list.
  314. *
  315. * @param obj the component to be added
  316. * @see Vector#addElement(Object)
  317. */
  318. public void addElement(Object obj) {
  319. int index = delegate.size();
  320. delegate.addElement(obj);
  321. fireIntervalAdded(this, index, index);
  322. }
  323. /**
  324. * Removes the first (lowest-indexed) occurrence of the argument
  325. * from this list.
  326. *
  327. * @param obj the component to be removed
  328. * @return <code>true</code> if the argument was a component of this
  329. * list; <code>false</code> otherwise
  330. * @see Vector#removeElement(Object)
  331. */
  332. public boolean removeElement(Object obj) {
  333. int index = indexOf(obj);
  334. boolean rv = delegate.removeElement(obj);
  335. if (index >= 0) {
  336. fireIntervalRemoved(this, index, index);
  337. }
  338. return rv;
  339. }
  340. /**
  341. * Removes all components from this list and sets its size to zero.
  342. * <blockquote>
  343. * <b>Note:</b> Although this method is not deprecated, the preferred
  344. * method to use is <code>clear</code>, which implements the
  345. * <code>List</code> interface defined in the 1.2 Collections framework.
  346. * </blockquote>
  347. *
  348. * @see #clear()
  349. * @see Vector#removeAllElements()
  350. */
  351. public void removeAllElements() {
  352. int index1 = delegate.size()-1;
  353. delegate.removeAllElements();
  354. if (index1 >= 0) {
  355. fireIntervalRemoved(this, 0, index1);
  356. }
  357. }
  358. /**
  359. * Returns a string that displays and identifies this
  360. * object's properties.
  361. *
  362. * @return a String representation of this object
  363. */
  364. public String toString() {
  365. return delegate.toString();
  366. }
  367. /* The remaining methods are included for compatibility with the
  368. * Java 2 platform Vector class.
  369. */
  370. /**
  371. * Returns an array containing all of the elements in this list in the
  372. * correct order.
  373. *
  374. * @return an array containing the elements of the list
  375. * @see Vector#toArray()
  376. */
  377. public Object[] toArray() {
  378. Object[] rv = new Object[delegate.size()];
  379. delegate.copyInto(rv);
  380. return rv;
  381. }
  382. /**
  383. * Returns the element at the specified position in this list.
  384. * <p>
  385. * Throws an <code>ArrayIndexOutOfBoundsException</code>
  386. * if the index is out of range
  387. * (<code>index < 0 || index >= size()</code>).
  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 <code>ArrayIndexOutOfBoundsException</code>
  399. * if the index is out of range
  400. * (<code>index < 0 || index >= size()</code>).
  401. *
  402. * @param index index of element to replace
  403. * @param element element to be stored at the specified position
  404. * @return the element previously at the specified position
  405. */
  406. public Object set(int index, Object element) {
  407. Object rv = delegate.elementAt(index);
  408. delegate.setElementAt(element, index);
  409. fireContentsChanged(this, index, index);
  410. return rv;
  411. }
  412. /**
  413. * Inserts the specified element at the specified position in this list.
  414. * <p>
  415. * Throws an <code>ArrayIndexOutOfBoundsException</code> if the
  416. * index is out of range
  417. * (<code>index < 0 || index > size()</code>).
  418. *
  419. * @param index index at which the specified element is to be inserted
  420. * @param element element to be inserted
  421. */
  422. public void add(int index, Object element) {
  423. delegate.insertElementAt(element, index);
  424. fireIntervalAdded(this, index, index);
  425. }
  426. /**
  427. * Removes the element at the specified position in this list.
  428. * Returns the element that was removed from the list.
  429. * <p>
  430. * Throws an <code>ArrayIndexOutOfBoundsException</code>
  431. * if the index is out of range
  432. * (<code>index < 0 || index >= size()</code>).
  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 <code>ArrayIndexOutOfBoundsException</code>
  460. * if the index was invalid.
  461. * Throws an <code>IllegalArgumentException</code> if
  462. * <code>fromIndex > toIndex</code>.
  463. *
  464. * @param fromIndex the index of the lower end of the range
  465. * @param toIndex the index of the upper end of the range
  466. * @see #remove(int)
  467. */
  468. public void removeRange(int fromIndex, int toIndex) {
  469. for(int i = toIndex; i >= fromIndex; i--) {
  470. delegate.removeElementAt(i);
  471. }
  472. fireIntervalRemoved(this, fromIndex, toIndex);
  473. }
  474. /*
  475. public void addAll(Collection c) {
  476. }
  477. public void addAll(int index, Collection c) {
  478. }
  479. */
  480. }