1. /*
  2. * @(#)List.java 1.97 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 java.awt;
  8. import java.util.Vector;
  9. import java.util.Locale;
  10. import java.util.EventListener;
  11. import java.awt.peer.ListPeer;
  12. import java.awt.event.*;
  13. import java.io.ObjectOutputStream;
  14. import java.io.ObjectInputStream;
  15. import java.io.IOException;
  16. import javax.accessibility.*;
  17. /**
  18. * The <code>List</code> component presents the user with a
  19. * scrolling list of text items. The list can be set up so that
  20. * the user can choose either one item or multiple items.
  21. * <p>
  22. * For example, the code . . .
  23. * <p>
  24. * <hr><blockquote><pre>
  25. * List lst = new List(4, false);
  26. * lst.add("Mercury");
  27. * lst.add("Venus");
  28. * lst.add("Earth");
  29. * lst.add("JavaSoft");
  30. * lst.add("Mars");
  31. * lst.add("Jupiter");
  32. * lst.add("Saturn");
  33. * lst.add("Uranus");
  34. * lst.add("Neptune");
  35. * lst.add("Pluto");
  36. * cnt.add(lst);
  37. * </pre></blockquote><hr>
  38. * <p>
  39. * where <code>cnt</code> is a container, produces the following
  40. * scrolling list:
  41. * <p>
  42. * <img src="doc-files/List-1.gif"
  43. * alt="Shows a list containing: Venus, Earth, JavaSoft, and Mars. Javasoft is selected." ALIGN=center HSPACE=10 VSPACE=7>
  44. * <p>
  45. * Clicking on an item that isn't selected selects it. Clicking on
  46. * an item that is already selected deselects it. In the preceding
  47. * example, only one item from the scrolling list can be selected
  48. * at a time, since the second argument when creating the new scrolling
  49. * list is <code>false</code>. Selecting an item causes any other
  50. * selected item to be automatically deselected.
  51. * <p>
  52. * Note that the list in the example shown was created with four visible
  53. * rows. Once the list has been created, the number of visible rows
  54. * cannot be changed. A default <code>List</code> is created with
  55. * four rows, so that <code>lst = new List()</code> is equivalent to
  56. * <code>list = new List(4, false)</code>.
  57. * <p>
  58. * Beginning with Java 1.1, the Abstract Window Toolkit
  59. * sends the <code>List</code> object all mouse, keyboard, and focus events
  60. * that occur over it. (The old AWT event model is being maintained
  61. * only for backwards compatibility, and its use is discouraged.)
  62. * <p>
  63. * When an item is selected or deselected by the user, AWT sends an instance
  64. * of <code>ItemEvent</code> to the list.
  65. * When the user double-clicks on an item in a scrolling list,
  66. * AWT sends an instance of <code>ActionEvent</code> to the
  67. * list following the item event. AWT also generates an action event
  68. * when the user presses the return key while an item in the
  69. * list is selected.
  70. * <p>
  71. * If an application wants to perform some action based on an item
  72. * in this list being selected or activated by the user, it should implement
  73. * <code>ItemListener</code> or <code>ActionListener</code>
  74. * as appropriate and register the new listener to receive
  75. * events from this list.
  76. * <p>
  77. * For multiple-selection scrolling lists, it is considered a better
  78. * user interface to use an external gesture (such as clicking on a
  79. * button) to trigger the action.
  80. * @version 1.97, 01/23/03
  81. * @author Sami Shaio
  82. * @see java.awt.event.ItemEvent
  83. * @see java.awt.event.ItemListener
  84. * @see java.awt.event.ActionEvent
  85. * @see java.awt.event.ActionListener
  86. * @since JDK1.0
  87. */
  88. public class List extends Component implements ItemSelectable, Accessible {
  89. /**
  90. * A vector created to contain items which will become
  91. * part of the List Component.
  92. *
  93. * @serial
  94. * @see #addItem(String)
  95. * @see #getItem(int)
  96. */
  97. Vector items = new Vector();
  98. /**
  99. * This field will represent the number of visible rows in the
  100. * <code>List</code> Component. It is specified only once, and
  101. * that is when the list component is actually
  102. * created. It will never change.
  103. *
  104. * @serial
  105. * @see #getRows()
  106. */
  107. int rows = 0;
  108. /**
  109. * <code>multipleMode</code> is a variable that will
  110. * be set to <code>true</code> if a list component is to be set to
  111. * multiple selection mode, that is where the user can
  112. * select more than one item in a list at one time.
  113. * <code>multipleMode</code> will be set to false if the
  114. * list component is set to single selection, that is where
  115. * the user can only select one item on the list at any
  116. * one time.
  117. *
  118. * @serial
  119. * @see #isMultipleMode()
  120. * @see #setMultipleMode(boolean)
  121. */
  122. boolean multipleMode = false;
  123. /**
  124. * <code>selected</code> is an array that will contain
  125. * the indices of items that have been selected.
  126. *
  127. * @serial
  128. * @see #getSelectedIndexes()
  129. * @see #getSelectedIndex()
  130. */
  131. int selected[] = new int[0];
  132. /**
  133. * This variable contains the value that will be used
  134. * when trying to make a particular list item visible.
  135. *
  136. * @serial
  137. * @see #makeVisible(int)
  138. */
  139. int visibleIndex = -1;
  140. transient ActionListener actionListener;
  141. transient ItemListener itemListener;
  142. private static final String base = "list";
  143. private static int nameCounter = 0;
  144. /*
  145. * JDK 1.1 serialVersionUID
  146. */
  147. private static final long serialVersionUID = -3304312411574666869L;
  148. /**
  149. * Creates a new scrolling list.
  150. * By default, there are four visible lines and multiple selections are
  151. * not allowed. Note that this is a convenience method for
  152. * <code>List(0, false)</code>. Also note that the number of visible
  153. * lines in the list cannot be changed after it has been created.
  154. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  155. * returns true.
  156. * @see java.awt.GraphicsEnvironment#isHeadless
  157. */
  158. public List() throws HeadlessException {
  159. this(0, false);
  160. }
  161. /**
  162. * Creates a new scrolling list initialized with the specified
  163. * number of visible lines. By default, multiple selections are
  164. * not allowed. Note that this is a convenience method for
  165. * <code>List(rows, false)</code>. Also note that the number
  166. * of visible rows in the list cannot be changed after it has
  167. * been created.
  168. * @param rows the number of items to show.
  169. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  170. * returns true.
  171. * @see java.awt.GraphicsEnvironment#isHeadless
  172. * @since JDK1.1
  173. */
  174. public List(int rows) throws HeadlessException {
  175. this(rows, false);
  176. }
  177. /**
  178. * The default number of visible rows is 4. A list with
  179. * zero rows is unusable and unsightly.
  180. */
  181. final static int DEFAULT_VISIBLE_ROWS = 4;
  182. /**
  183. * Creates a new scrolling list initialized to display the specified
  184. * number of rows. Note that if zero rows are specified, then
  185. * the list will be created with a default of four rows.
  186. * Also note that the number of visible rows in the list cannot
  187. * be changed after it has been created.
  188. * If the value of <code>multipleMode</code> is
  189. * <code>true</code>, then the user can select multiple items from
  190. * the list. If it is <code>false</code>, only one item at a time
  191. * can be selected.
  192. * @param rows the number of items to show.
  193. * @param multipleMode if <code>true</code>,
  194. * then multiple selections are allowed;
  195. * otherwise, only one item can be selected at a time.
  196. * @exception HeadlessException if GraphicsEnvironment.isHeadless()
  197. * returns true.
  198. * @see java.awt.GraphicsEnvironment#isHeadless
  199. */
  200. public List(int rows, boolean multipleMode) throws HeadlessException {
  201. GraphicsEnvironment.checkHeadless();
  202. this.rows = (rows != 0) ? rows : DEFAULT_VISIBLE_ROWS;
  203. this.multipleMode = multipleMode;
  204. }
  205. /**
  206. * Construct a name for this component. Called by
  207. * <code>getName</code> when the name is <code>null</code>.
  208. */
  209. String constructComponentName() {
  210. synchronized (getClass()) {
  211. return base + nameCounter++;
  212. }
  213. }
  214. /**
  215. * Creates the peer for the list. The peer allows us to modify the
  216. * list's appearance without changing its functionality.
  217. */
  218. public void addNotify() {
  219. synchronized (getTreeLock()) {
  220. if (peer == null)
  221. peer = getToolkit().createList(this);
  222. super.addNotify();
  223. }
  224. }
  225. /**
  226. * Removes the peer for this list. The peer allows us to modify the
  227. * list's appearance without changing its functionality.
  228. */
  229. public void removeNotify() {
  230. synchronized (getTreeLock()) {
  231. ListPeer peer = (ListPeer)this.peer;
  232. if (peer != null) {
  233. selected = peer.getSelectedIndexes();
  234. }
  235. super.removeNotify();
  236. }
  237. }
  238. /**
  239. * Gets the number of items in the list.
  240. * @return the number of items in the list
  241. * @see #getItem
  242. * @since JDK1.1
  243. */
  244. public int getItemCount() {
  245. return countItems();
  246. }
  247. /**
  248. * @deprecated As of JDK version 1.1,
  249. * replaced by <code>getItemCount()</code>.
  250. */
  251. public int countItems() {
  252. return items.size();
  253. }
  254. /**
  255. * Gets the item associated with the specified index.
  256. * @return an item that is associated with
  257. * the specified index
  258. * @param index the position of the item
  259. * @see #getItemCount
  260. */
  261. public String getItem(int index) {
  262. return getItemImpl(index);
  263. }
  264. // NOTE: This method may be called by privileged threads.
  265. // We implement this functionality in a package-private method
  266. // to insure that it cannot be overridden by client subclasses.
  267. // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
  268. final String getItemImpl(int index) {
  269. return (String)items.elementAt(index);
  270. }
  271. /**
  272. * Gets the items in the list.
  273. * @return a string array containing items of the list
  274. * @see #select
  275. * @see #deselect
  276. * @see #isIndexSelected
  277. * @since JDK1.1
  278. */
  279. public synchronized String[] getItems() {
  280. String itemCopies[] = new String[items.size()];
  281. items.copyInto(itemCopies);
  282. return itemCopies;
  283. }
  284. /**
  285. * Adds the specified item to the end of scrolling list.
  286. * @param item the item to be added
  287. * @since JDK1.1
  288. */
  289. public void add(String item) {
  290. addItem(item);
  291. }
  292. /**
  293. * @deprecated replaced by <code>add(String)</code>.
  294. */
  295. public void addItem(String item) {
  296. addItem(item, -1);
  297. }
  298. /**
  299. * Adds the specified item to the the scrolling list
  300. * at the position indicated by the index. The index is
  301. * zero-based. If the value of the index is less than zero,
  302. * or if the value of the index is greater than or equal to
  303. * the number of items in the list, then the item is added
  304. * to the end of the list.
  305. * @param item the item to be added;
  306. * if this parameter is <code>null</code> then the item is
  307. * treated as an empty string, <code>""</code>
  308. * @param index the position at which to add the item
  309. * @since JDK1.1
  310. */
  311. public void add(String item, int index) {
  312. addItem(item, index);
  313. }
  314. /**
  315. * @deprecated replaced by <code>add(String, int)</code>.
  316. */
  317. public synchronized void addItem(String item, int index) {
  318. if (index < -1 || index >= items.size()) {
  319. index = -1;
  320. }
  321. if (item == null) {
  322. item = "";
  323. }
  324. if (index == -1) {
  325. items.addElement(item);
  326. } else {
  327. items.insertElementAt(item, index);
  328. }
  329. ListPeer peer = (ListPeer)this.peer;
  330. if (peer != null) {
  331. peer.addItem(item, index);
  332. }
  333. }
  334. /**
  335. * Replaces the item at the specified index in the scrolling list
  336. * with the new string.
  337. * @param newValue a new string to replace an existing item
  338. * @param index the position of the item to replace
  339. * @exception ArrayIndexOutOfBoundsException if <code>index</code>
  340. * is out of range
  341. */
  342. public synchronized void replaceItem(String newValue, int index) {
  343. remove(index);
  344. add(newValue, index);
  345. }
  346. /**
  347. * Removes all items from this list.
  348. * @see #remove
  349. * @see #delItems
  350. * @since JDK1.1
  351. */
  352. public void removeAll() {
  353. clear();
  354. }
  355. /**
  356. * @deprecated As of JDK version 1.1,
  357. * replaced by <code>removeAll()</code>.
  358. */
  359. public synchronized void clear() {
  360. ListPeer peer = (ListPeer)this.peer;
  361. if (peer != null) {
  362. peer.clear();
  363. }
  364. items = new Vector();
  365. selected = new int[0];
  366. }
  367. /**
  368. * Removes the first occurrence of an item from the list.
  369. * @param item the item to remove from the list
  370. * @exception IllegalArgumentException
  371. * if the item doesn't exist in the list
  372. * @since JDK1.1
  373. */
  374. public synchronized void remove(String item) {
  375. int index = items.indexOf(item);
  376. if (index < 0) {
  377. throw new IllegalArgumentException("item " + item +
  378. " not found in list");
  379. } else {
  380. remove(index);
  381. }
  382. }
  383. /**
  384. * Remove the item at the specified position
  385. * from this scrolling list.
  386. * @param position the index of the item to delete
  387. * @see #add(String, int)
  388. * @since JDK1.1
  389. * @exception ArrayIndexOutOfBoundsException
  390. * if the <code>position</code> is less than 0 or
  391. * greater than <code>getItemCount()-1</code>
  392. */
  393. public void remove(int position) {
  394. delItem(position);
  395. }
  396. /**
  397. * @deprecated replaced by <code>remove(String)</code>
  398. * and <code>remove(int)</code>.
  399. */
  400. public void delItem(int position) {
  401. delItems(position, position);
  402. }
  403. /**
  404. * Gets the index of the selected item on the list,
  405. * @return the index of the selected item, or
  406. * <code>-1</code> if no item is selected,
  407. * or if more that one item is selected
  408. * @see #select
  409. * @see #deselect
  410. * @see #isIndexSelected
  411. */
  412. public synchronized int getSelectedIndex() {
  413. int sel[] = getSelectedIndexes();
  414. return (sel.length == 1) ? sel[0] : -1;
  415. }
  416. /**
  417. * Gets the selected indexes on the list.
  418. * @return an array of the selected indexes
  419. * of this scrolling list; if no items are
  420. * selected, a zero-length array is returned
  421. * @see #select
  422. * @see #deselect
  423. * @see #isIndexSelected
  424. */
  425. public synchronized int[] getSelectedIndexes() {
  426. ListPeer peer = (ListPeer)this.peer;
  427. if (peer != null) {
  428. selected = ((ListPeer)peer).getSelectedIndexes();
  429. }
  430. return (int[])selected.clone();
  431. }
  432. /**
  433. * Gets the selected item on this scrolling list.
  434. * @return the selected item on the list,
  435. * or <code>null</code> if no item is selected
  436. * @see #select
  437. * @see #deselect
  438. * @see #isIndexSelected
  439. */
  440. public synchronized String getSelectedItem() {
  441. int index = getSelectedIndex();
  442. return (index < 0) ? null : getItem(index);
  443. }
  444. /**
  445. * Gets the selected items on this scrolling list.
  446. * @return an array of the selected items
  447. * on this scrolling list
  448. * @see #select
  449. * @see #deselect
  450. * @see #isIndexSelected
  451. */
  452. public synchronized String[] getSelectedItems() {
  453. int sel[] = getSelectedIndexes();
  454. String str[] = new String[sel.length];
  455. for (int i = 0 ; i < sel.length ; i++) {
  456. str[i] = getItem(sel[i]);
  457. }
  458. return str;
  459. }
  460. /**
  461. * Returns the selected items on the list in an array of objects.
  462. * @see ItemSelectable
  463. */
  464. public Object[] getSelectedObjects() {
  465. return getSelectedItems();
  466. }
  467. /**
  468. * Selects the item at the specified index in the scrolling list.
  469. *
  470. * <p>Note that this method should be primarily used to
  471. * initially select an item in this component.
  472. * Programmatically calling this method will <i>not</i> trigger
  473. * an <code>ItemEvent</code>. The only way to trigger an
  474. * <code>ItemEvent</code> is by user interaction.
  475. *
  476. * @param index the position of the item to select
  477. * @see #getSelectedItem
  478. * @see #deselect
  479. * @see #isIndexSelected
  480. */
  481. public void select(int index) {
  482. // Bug #4059614: select can't be synchronized while calling the peer,
  483. // because it is called from the Window Thread. It is sufficient to
  484. // synchronize the code that manipulates 'selected' except for the
  485. // case where the peer changes. To handle this case, we simply
  486. // repeat the selection process.
  487. ListPeer peer;
  488. do {
  489. peer = (ListPeer)this.peer;
  490. if (peer != null) {
  491. peer.select(index);
  492. return;
  493. }
  494. synchronized(this)
  495. {
  496. boolean alreadySelected = false;
  497. for (int i = 0 ; i < selected.length ; i++) {
  498. if (selected[i] == index) {
  499. alreadySelected = true;
  500. break;
  501. }
  502. }
  503. if (!alreadySelected) {
  504. if (!multipleMode) {
  505. selected = new int[1];
  506. selected[0] = index;
  507. } else {
  508. int newsel[] = new int[selected.length + 1];
  509. System.arraycopy(selected, 0, newsel, 0,
  510. selected.length);
  511. newsel[selected.length] = index;
  512. selected = newsel;
  513. }
  514. }
  515. }
  516. } while (peer != this.peer);
  517. }
  518. /**
  519. * Deselects the item at the specified index.
  520. * <p>
  521. * If the item at the specified index is not selected, or if the
  522. * index is out of range, then the operation is ignored.
  523. * @param index the position of the item to deselect
  524. * @see #select
  525. * @see #getSelectedItem
  526. * @see #isIndexSelected
  527. */
  528. public synchronized void deselect(int index) {
  529. ListPeer peer = (ListPeer)this.peer;
  530. if (peer != null) {
  531. peer.deselect(index);
  532. }
  533. for (int i = 0 ; i < selected.length ; i++) {
  534. if (selected[i] == index) {
  535. int newsel[] = new int[selected.length - 1];
  536. System.arraycopy(selected, 0, newsel, 0, i);
  537. System.arraycopy(selected, i+1, newsel, i, selected.length - (i+1));
  538. selected = newsel;
  539. return;
  540. }
  541. }
  542. }
  543. /**
  544. * Determines if the specified item in this scrolling list is
  545. * selected.
  546. * @param index the item to be checked
  547. * @return <code>true</code> if the specified item has been
  548. * selected; <code>false</code> otherwise
  549. * @see #select
  550. * @see #deselect
  551. * @since JDK1.1
  552. */
  553. public boolean isIndexSelected(int index) {
  554. return isSelected(index);
  555. }
  556. /**
  557. * @deprecated As of JDK version 1.1,
  558. * replaced by <code>isIndexSelected(int)</code>.
  559. */
  560. public boolean isSelected(int index) {
  561. int sel[] = getSelectedIndexes();
  562. for (int i = 0 ; i < sel.length ; i++) {
  563. if (sel[i] == index) {
  564. return true;
  565. }
  566. }
  567. return false;
  568. }
  569. /**
  570. * Gets the number of visible lines in this list. Note that
  571. * once the <code>List</code> has been created, this number
  572. * will never change.
  573. * @return the number of visible lines in this scrolling list
  574. */
  575. public int getRows() {
  576. return rows;
  577. }
  578. /**
  579. * Determines whether this list allows multiple selections.
  580. * @return <code>true</code> if this list allows multiple
  581. * selections; otherwise, <code>false</code>
  582. * @see #setMultipleMode
  583. * @since JDK1.1
  584. */
  585. public boolean isMultipleMode() {
  586. return allowsMultipleSelections();
  587. }
  588. /**
  589. * @deprecated As of JDK version 1.1,
  590. * replaced by <code>isMultipleMode()</code>.
  591. */
  592. public boolean allowsMultipleSelections() {
  593. return multipleMode;
  594. }
  595. /**
  596. * Sets the flag that determines whether this list
  597. * allows multiple selections.
  598. * @param b if <code>true</code> then multiple selections
  599. * are allowed; otherwise, only one item from
  600. * the list can be selected at once
  601. * @see #isMultipleMode
  602. * @since JDK1.1
  603. */
  604. public void setMultipleMode(boolean b) {
  605. setMultipleSelections(b);
  606. }
  607. /**
  608. * @deprecated As of JDK version 1.1,
  609. * replaced by <code>setMultipleMode(boolean)</code>.
  610. */
  611. public synchronized void setMultipleSelections(boolean b) {
  612. if (b != multipleMode) {
  613. multipleMode = b;
  614. ListPeer peer = (ListPeer)this.peer;
  615. if (peer != null) {
  616. peer.setMultipleSelections(b);
  617. }
  618. }
  619. }
  620. /**
  621. * Gets the index of the item that was last made visible by
  622. * the method <code>makeVisible</code>.
  623. * @return the index of the item that was last made visible
  624. * @see #makeVisible
  625. */
  626. public int getVisibleIndex() {
  627. return visibleIndex;
  628. }
  629. /**
  630. * Makes the item at the specified index visible.
  631. * @param index the position of the item
  632. * @see #getVisibleIndex
  633. */
  634. public synchronized void makeVisible(int index) {
  635. visibleIndex = index;
  636. ListPeer peer = (ListPeer)this.peer;
  637. if (peer != null) {
  638. peer.makeVisible(index);
  639. }
  640. }
  641. /**
  642. * Gets the preferred dimensions for a list with the specified
  643. * number of rows.
  644. * @param rows number of rows in the list
  645. * @return the preferred dimensions for displaying this scrolling list
  646. * given that the specified number of rows must be visible
  647. * @see java.awt.Component#getPreferredSize
  648. * @since JDK1.1
  649. */
  650. public Dimension getPreferredSize(int rows) {
  651. return preferredSize(rows);
  652. }
  653. /**
  654. * @deprecated As of JDK version 1.1,
  655. * replaced by <code>getPreferredSize(int)</code>.
  656. */
  657. public Dimension preferredSize(int rows) {
  658. synchronized (getTreeLock()) {
  659. ListPeer peer = (ListPeer)this.peer;
  660. return (peer != null) ?
  661. peer.preferredSize(rows) :
  662. super.preferredSize();
  663. }
  664. }
  665. /**
  666. * Gets the preferred size of this scrolling list.
  667. * @return the preferred dimensions for displaying this scrolling list
  668. * @see java.awt.Component#getPreferredSize
  669. * @since JDK1.1
  670. */
  671. public Dimension getPreferredSize() {
  672. return preferredSize();
  673. }
  674. /**
  675. * @deprecated As of JDK version 1.1,
  676. * replaced by <code>getPreferredSize()</code>.
  677. */
  678. public Dimension preferredSize() {
  679. synchronized (getTreeLock()) {
  680. return (rows > 0) ?
  681. preferredSize(rows) :
  682. super.preferredSize();
  683. }
  684. }
  685. /**
  686. * Gets the minumum dimensions for a list with the specified
  687. * number of rows.
  688. * @param rows number of rows in the list
  689. * @return the minimum dimensions for displaying this scrolling list
  690. * given that the specified number of rows must be visible
  691. * @see java.awt.Component#getMinimumSize
  692. * @since JDK1.1
  693. */
  694. public Dimension getMinimumSize(int rows) {
  695. return minimumSize(rows);
  696. }
  697. /**
  698. * @deprecated As of JDK version 1.1,
  699. * replaced by <code>getMinimumSize(int)</code>.
  700. */
  701. public Dimension minimumSize(int rows) {
  702. synchronized (getTreeLock()) {
  703. ListPeer peer = (ListPeer)this.peer;
  704. return (peer != null) ?
  705. peer.minimumSize(rows) :
  706. super.minimumSize();
  707. }
  708. }
  709. /**
  710. * Determines the minimum size of this scrolling list.
  711. * @return the minimum dimensions needed
  712. * to display this scrolling list
  713. * @see java.awt.Component#getMinimumSize()
  714. * @since JDK1.1
  715. */
  716. public Dimension getMinimumSize() {
  717. return minimumSize();
  718. }
  719. /**
  720. * @deprecated As of JDK version 1.1,
  721. * replaced by <code>getMinimumSize()</code>.
  722. */
  723. public Dimension minimumSize() {
  724. synchronized (getTreeLock()) {
  725. return (rows > 0) ? minimumSize(rows) : super.minimumSize();
  726. }
  727. }
  728. /**
  729. * Adds the specified item listener to receive item events from
  730. * this list. Item events are sent in response to user input, but not
  731. * in response to calls to <code>select</code> or <code>deselect</code>.
  732. * If listener <code>l</code> is <code>null</code>,
  733. * no exception is thrown and no action is performed.
  734. *
  735. * @param l the item listener
  736. * @see #removeItemListener
  737. * @see #getItemListeners
  738. * @see #select
  739. * @see #deselect
  740. * @see java.awt.event.ItemEvent
  741. * @see java.awt.event.ItemListener
  742. * @since JDK1.1
  743. */
  744. public synchronized void addItemListener(ItemListener l) {
  745. if (l == null) {
  746. return;
  747. }
  748. itemListener = AWTEventMulticaster.add(itemListener, l);
  749. newEventsOnly = true;
  750. }
  751. /**
  752. * Removes the specified item listener so that it no longer
  753. * receives item events from this list.
  754. * If listener <code>l</code> is <code>null</code>,
  755. * no exception is thrown and no action is performed.
  756. *
  757. * @param l the item listener
  758. * @see #addItemListener
  759. * @see #getItemListeners
  760. * @see java.awt.event.ItemEvent
  761. * @see java.awt.event.ItemListener
  762. * @since JDK1.1
  763. */
  764. public synchronized void removeItemListener(ItemListener l) {
  765. if (l == null) {
  766. return;
  767. }
  768. itemListener = AWTEventMulticaster.remove(itemListener, l);
  769. }
  770. /**
  771. * Returns an array of all the item listeners
  772. * registered on this list.
  773. *
  774. * @return all of this list's <code>ItemListener</code>s
  775. * or an empty array if no item
  776. * listeners are currently registered
  777. *
  778. * @see #addItemListener
  779. * @see #removeItemListener
  780. * @see java.awt.event.ItemEvent
  781. * @see java.awt.event.ItemListener
  782. * @since 1.4
  783. */
  784. public synchronized ItemListener[] getItemListeners() {
  785. return (ItemListener[])(getListeners(ItemListener.class));
  786. }
  787. /**
  788. * Adds the specified action listener to receive action events from
  789. * this list. Action events occur when a user double-clicks
  790. * on a list item.
  791. * If listener <code>l</code> is <code>null</code>,
  792. * no exception is thrown and no action is performed.
  793. *
  794. * @param l the action listener
  795. * @see #removeActionListener
  796. * @see #getActionListeners
  797. * @see java.awt.event.ActionEvent
  798. * @see java.awt.event.ActionListener
  799. * @since JDK1.1
  800. */
  801. public synchronized void addActionListener(ActionListener l) {
  802. if (l == null) {
  803. return;
  804. }
  805. actionListener = AWTEventMulticaster.add(actionListener, l);
  806. newEventsOnly = true;
  807. }
  808. /**
  809. * Removes the specified action listener so that it no longer
  810. * receives action events from this list. Action events
  811. * occur when a user double-clicks on a list item.
  812. * If listener <code>l</code> is <code>null</code>,
  813. * no exception is thrown and no action is performed.
  814. *
  815. * @param l the action listener
  816. * @see #addActionListener
  817. * @see #getActionListeners
  818. * @see java.awt.event.ActionEvent
  819. * @see java.awt.event.ActionListener
  820. * @since JDK1.1
  821. */
  822. public synchronized void removeActionListener(ActionListener l) {
  823. if (l == null) {
  824. return;
  825. }
  826. actionListener = AWTEventMulticaster.remove(actionListener, l);
  827. }
  828. /**
  829. * Returns an array of all the action listeners
  830. * registered on this list.
  831. *
  832. * @return all of this list's <code>ActionListener</code>s
  833. * or an empty array if no action
  834. * listeners are currently registered
  835. *
  836. * @see #addActionListener
  837. * @see #removeActionListener
  838. * @see java.awt.event.ActionEvent
  839. * @see java.awt.event.ActionListener
  840. * @since 1.4
  841. */
  842. public synchronized ActionListener[] getActionListeners() {
  843. return (ActionListener[])(getListeners(ActionListener.class));
  844. }
  845. /**
  846. * Returns an array of all the objects currently registered
  847. * as <code><em>Foo</em>Listener</code>s
  848. * upon this <code>List</code>.
  849. * <code><em>Foo</em>Listener</code>s are registered using the
  850. * <code>add<em>Foo</em>Listener</code> method.
  851. *
  852. * <p>
  853. * You can specify the <code>listenerType</code> argument
  854. * with a class literal, such as
  855. * <code><em>Foo</em>Listener.class</code>.
  856. * For example, you can query a
  857. * <code>List</code> <code>l</code>
  858. * for its item listeners with the following code:
  859. *
  860. * <pre>ItemListener[] ils = (ItemListener[])(l.getListeners(ItemListener.class));</pre>
  861. *
  862. * If no such listeners exist, this method returns an empty array.
  863. *
  864. * @param listenerType the type of listeners requested; this parameter
  865. * should specify an interface that descends from
  866. * <code>java.util.EventListener</code>
  867. * @return an array of all objects registered as
  868. * <code><em>Foo</em>Listener</code>s on this list,
  869. * or an empty array if no such
  870. * listeners have been added
  871. * @exception ClassCastException if <code>listenerType</code>
  872. * doesn't specify a class or interface that implements
  873. * <code>java.util.EventListener</code>
  874. *
  875. * @see #getItemListeners
  876. * @since 1.3
  877. */
  878. public EventListener[] getListeners(Class listenerType) {
  879. EventListener l = null;
  880. if (listenerType == ActionListener.class) {
  881. l = actionListener;
  882. } else if (listenerType == ItemListener.class) {
  883. l = itemListener;
  884. } else {
  885. return super.getListeners(listenerType);
  886. }
  887. return AWTEventMulticaster.getListeners(l, listenerType);
  888. }
  889. // REMIND: remove when filtering is done at lower level
  890. boolean eventEnabled(AWTEvent e) {
  891. switch(e.id) {
  892. case ActionEvent.ACTION_PERFORMED:
  893. if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0 ||
  894. actionListener != null) {
  895. return true;
  896. }
  897. return false;
  898. case ItemEvent.ITEM_STATE_CHANGED:
  899. if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0 ||
  900. itemListener != null) {
  901. return true;
  902. }
  903. return false;
  904. default:
  905. break;
  906. }
  907. return super.eventEnabled(e);
  908. }
  909. /**
  910. * Processes events on this scrolling list. If an event is
  911. * an instance of <code>ItemEvent</code>, it invokes the
  912. * <code>processItemEvent</code> method. Else, if the
  913. * event is an instance of <code>ActionEvent</code>,
  914. * it invokes <code>processActionEvent</code>.
  915. * If the event is not an item event or an action event,
  916. * it invokes <code>processEvent</code> on the superclass.
  917. * <p>Note that if the event parameter is <code>null</code>
  918. * the behavior is unspecified and may result in an
  919. * exception.
  920. *
  921. * @param e the event
  922. * @see java.awt.event.ActionEvent
  923. * @see java.awt.event.ItemEvent
  924. * @see #processActionEvent
  925. * @see #processItemEvent
  926. * @since JDK1.1
  927. */
  928. protected void processEvent(AWTEvent e) {
  929. if (e instanceof ItemEvent) {
  930. processItemEvent((ItemEvent)e);
  931. return;
  932. } else if (e instanceof ActionEvent) {
  933. processActionEvent((ActionEvent)e);
  934. return;
  935. }
  936. super.processEvent(e);
  937. }
  938. /**
  939. * Processes item events occurring on this list by
  940. * dispatching them to any registered
  941. * <code>ItemListener</code> objects.
  942. * <p>
  943. * This method is not called unless item events are
  944. * enabled for this component. Item events are enabled
  945. * when one of the following occurs:
  946. * <p><ul>
  947. * <li>An <code>ItemListener</code> object is registered
  948. * via <code>addItemListener</code>.
  949. * <li>Item events are enabled via <code>enableEvents</code>.
  950. * </ul>
  951. * <p>Note that if the event parameter is <code>null</code>
  952. * the behavior is unspecified and may result in an
  953. * exception.
  954. *
  955. * @param e the item event
  956. * @see java.awt.event.ItemEvent
  957. * @see java.awt.event.ItemListener
  958. * @see #addItemListener
  959. * @see java.awt.Component#enableEvents
  960. * @since JDK1.1
  961. */
  962. protected void processItemEvent(ItemEvent e) {
  963. ItemListener listener = itemListener;
  964. if (listener != null) {
  965. listener.itemStateChanged(e);
  966. }
  967. }
  968. /**
  969. * Processes action events occurring on this component
  970. * by dispatching them to any registered
  971. * <code>ActionListener</code> objects.
  972. * <p>
  973. * This method is not called unless action events are
  974. * enabled for this component. Action events are enabled
  975. * when one of the following occurs:
  976. * <p><ul>
  977. * <li>An <code>ActionListener</code> object is registered
  978. * via <code>addActionListener</code>.
  979. * <li>Action events are enabled via <code>enableEvents</code>.
  980. * </ul>
  981. * <p>Note that if the event parameter is <code>null</code>
  982. * the behavior is unspecified and may result in an
  983. * exception.
  984. *
  985. * @param e the action event
  986. * @see java.awt.event.ActionEvent
  987. * @see java.awt.event.ActionListener
  988. * @see #addActionListener
  989. * @see java.awt.Component#enableEvents
  990. * @since JDK1.1
  991. */
  992. protected void processActionEvent(ActionEvent e) {
  993. ActionListener listener = actionListener;
  994. if (listener != null) {
  995. listener.actionPerformed(e);
  996. }
  997. }
  998. /**
  999. * Returns the parameter string representing the state of this
  1000. * scrolling list. This string is useful for debugging.
  1001. * @return the parameter string of this scrolling list
  1002. */
  1003. protected String paramString() {
  1004. return super.paramString() + ",selected=" + getSelectedItem();
  1005. }
  1006. /**
  1007. * @deprecated As of JDK version 1.1,
  1008. * Not for public use in the future.
  1009. * This method is expected to be retained only as a package
  1010. * private method.
  1011. */
  1012. public synchronized void delItems(int start, int end) {
  1013. for (int i = end; i >= start; i--) {
  1014. items.removeElementAt(i);
  1015. }
  1016. ListPeer peer = (ListPeer)this.peer;
  1017. if (peer != null) {
  1018. peer.delItems(start, end);
  1019. }
  1020. }
  1021. /*
  1022. * Serialization support. Since the value of the selected
  1023. * field isn't neccessarily up to date we sync it up with the
  1024. * peer before serializing.
  1025. */
  1026. /**
  1027. * The <code>List</code> component's
  1028. * Serialized Data Version.
  1029. *
  1030. * @serial
  1031. */
  1032. private int listSerializedDataVersion = 1;
  1033. /**
  1034. * Writes default serializable fields to stream. Writes
  1035. * a list of serializable <code>ItemListeners</code>
  1036. * and <code>ActionListeners</code> as optional data.
  1037. * The non-serializable listeners are detected and
  1038. * no attempt is made to serialize them.
  1039. *
  1040. * @serialData <code>null</code> terminated sequence of 0
  1041. * or more pairs; the pair consists of a <code>String</code>
  1042. * and an <code>Object</code> the <code>String</code>
  1043. * indicates the type of object and is one of the
  1044. * following:
  1045. * <code>itemListenerK</code> indicating an
  1046. * <code>ItemListener</code> object;
  1047. * <code>actionListenerK</code> indicating an
  1048. * <code>ActionListener</code> object
  1049. *
  1050. * @param s the <code>ObjectOutputStream</code> to write
  1051. * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
  1052. * @see java.awt.Component#itemListenerK
  1053. * @see java.awt.Component#actionListenerK
  1054. * @see #readObject(ObjectInputStream)
  1055. */
  1056. private void writeObject(ObjectOutputStream s)
  1057. throws IOException
  1058. {
  1059. synchronized (this) {
  1060. ListPeer peer = (ListPeer)this.peer;
  1061. if (peer != null) {
  1062. selected = peer.getSelectedIndexes();
  1063. }
  1064. }
  1065. s.defaultWriteObject();
  1066. AWTEventMulticaster.save(s, itemListenerK, itemListener);
  1067. AWTEventMulticaster.save(s, actionListenerK, actionListener);
  1068. s.writeObject(null);
  1069. }
  1070. /**
  1071. * Reads the <code>ObjectInputStream</code> and if it
  1072. * isn't <code>null</code> adds a listener to receive
  1073. * both item events and action events (as specified
  1074. * by the key stored in the stream) fired by the
  1075. * <code>List</code>.
  1076. * Unrecognized keys or values will be ignored.
  1077. *
  1078. * @param s the <code>ObjectInputStream</code> to write
  1079. * @exception HeadlessException if
  1080. * <code>GraphicsEnvironment.isHeadless</code> returns
  1081. * <code>true</code>
  1082. * @see #removeItemListener(ItemListener)
  1083. * @see #addItemListener(ItemListener)
  1084. * @see java.awt.GraphicsEnvironment#isHeadless
  1085. * @see #writeObject(ObjectOutputStream)
  1086. */
  1087. private void readObject(ObjectInputStream s)
  1088. throws ClassNotFoundException, IOException, HeadlessException
  1089. {
  1090. GraphicsEnvironment.checkHeadless();
  1091. s.defaultReadObject();
  1092. Object keyOrNull;
  1093. while(null != (keyOrNull = s.readObject())) {
  1094. String key = ((String)keyOrNull).intern();
  1095. if (itemListenerK == key)
  1096. addItemListener((ItemListener)(s.readObject()));
  1097. else if (actionListenerK == key)
  1098. addActionListener((ActionListener)(s.readObject()));
  1099. else // skip value for unrecognized key
  1100. s.readObject();
  1101. }
  1102. }
  1103. /////////////////
  1104. // Accessibility support
  1105. ////////////////
  1106. /**
  1107. * Gets the <code>AccessibleContext</code> associated with this
  1108. * <code>List</code>. For lists, the <code>AccessibleContext</code>
  1109. * takes the form of an <code>AccessibleAWTList</code>.
  1110. * A new <code>AccessibleAWTList</code> instance is created, if necessary.
  1111. *
  1112. * @return an <code>AccessibleAWTList</code> that serves as the
  1113. * <code>AccessibleContext</code> of this <code>List</code>
  1114. */
  1115. public AccessibleContext getAccessibleContext() {
  1116. if (accessibleContext == null) {
  1117. accessibleContext = new AccessibleAWTList();
  1118. }
  1119. return accessibleContext;
  1120. }
  1121. /**
  1122. * This class implements accessibility support for the
  1123. * <code>List</code> class. It provides an implementation of the
  1124. * Java Accessibility API appropriate to list user-interface elements.
  1125. */
  1126. protected class AccessibleAWTList extends AccessibleAWTComponent
  1127. implements AccessibleSelection, ItemListener, ActionListener {
  1128. public AccessibleAWTList() {
  1129. super();
  1130. List.this.addActionListener(this);
  1131. List.this.addItemListener(this);
  1132. }
  1133. public void actionPerformed(ActionEvent event) {
  1134. }
  1135. public void itemStateChanged(ItemEvent event) {
  1136. }
  1137. /**
  1138. * Get the state set of this object.
  1139. *
  1140. * @return an instance of AccessibleState containing the current state
  1141. * of the object
  1142. * @see AccessibleState
  1143. */
  1144. public AccessibleStateSet getAccessibleStateSet() {
  1145. AccessibleStateSet states = super.getAccessibleStateSet();
  1146. if (List.this.isMultipleMode()) {
  1147. states.add(AccessibleState.MULTISELECTABLE);
  1148. }
  1149. return states;
  1150. }
  1151. /**
  1152. * Get the role of this object.
  1153. *
  1154. * @return an instance of AccessibleRole describing the role of the
  1155. * object
  1156. * @see AccessibleRole
  1157. */
  1158. public AccessibleRole getAccessibleRole() {
  1159. return AccessibleRole.LIST;
  1160. }
  1161. /**
  1162. * Returns the Accessible child contained at the local coordinate
  1163. * Point, if one exists.
  1164. *
  1165. * @return the Accessible at the specified location, if it exists
  1166. */
  1167. public Accessible getAccessibleAt(Point p) {
  1168. return null; // fredxFIXME Not implemented yet
  1169. }
  1170. /**
  1171. * Returns the number of accessible children in the object. If all
  1172. * of the children of this object implement Accessible, than this
  1173. * method should return the number of children of this object.
  1174. *
  1175. * @return the number of accessible children in the object.
  1176. */
  1177. public int getAccessibleChildrenCount() {
  1178. return List.this.getItemCount();
  1179. }
  1180. /**
  1181. * Return the nth Accessible child of the object.
  1182. *
  1183. * @param i zero-based index of child
  1184. * @return the nth Accessible child of the object
  1185. */
  1186. public Accessible getAccessibleChild(int i) {
  1187. synchronized(List.this) {
  1188. if (i >= List.this.getItemCount()) {
  1189. return null;
  1190. } else {
  1191. return new AccessibleAWTListChild(List.this, i);
  1192. }
  1193. }
  1194. }
  1195. /**
  1196. * Get the AccessibleSelection associated with this object. In the
  1197. * implementation of the Java Accessibility API for this class,
  1198. * return this object, which is responsible for implementing the
  1199. * AccessibleSelection interface on behalf of itself.
  1200. *
  1201. * @return this object
  1202. */
  1203. public AccessibleSelection getAccessibleSelection() {
  1204. return this;
  1205. }
  1206. // AccessibleSelection methods
  1207. /**
  1208. * Returns the number of items currently selected.
  1209. * If no items are selected, the return value will be 0.
  1210. *
  1211. * @return the number of items currently selected.
  1212. */
  1213. public int getAccessibleSelectionCount() {
  1214. return List.this.getSelectedIndexes().length;
  1215. }
  1216. /**
  1217. * Returns an Accessible representing the specified selected item
  1218. * in the object. If there isn't a selection, or there are
  1219. * fewer items selected than the integer passed in, the return
  1220. * value will be null.
  1221. *
  1222. * @param i the zero-based index of selected items
  1223. * @return an Accessible containing the selected item
  1224. */
  1225. public Accessible getAccessibleSelection(int i) {
  1226. synchronized(List.this) {
  1227. int len = getAccessibleSelectionCount();
  1228. if (i <= 0 || i >= len) {
  1229. return null;
  1230. } else {
  1231. return getAccessibleChild(List.this.getSelectedIndexes()[i]);
  1232. }
  1233. }
  1234. }
  1235. /**
  1236. * Returns true if the current child of this object is selected.
  1237. *
  1238. * @param i the zero-based index of the child in this Accessible
  1239. * object.
  1240. * @see AccessibleContext#getAccessibleChild
  1241. */
  1242. public boolean isAccessibleChildSelected(int i) {
  1243. return List.this.isIndexSelected(i);
  1244. }
  1245. /**
  1246. * Adds the specified selected item in the object to the object's
  1247. * selection. If the object supports multiple selections,
  1248. * the specified item is added to any existing selection, otherwise
  1249. * it replaces any existing selection in the object. If the
  1250. * specified item is already selected, this method has no effect.
  1251. *
  1252. * @param i the zero-based index of selectable items
  1253. */
  1254. public void addAccessibleSelection(int i) {
  1255. List.this.select(i);
  1256. }
  1257. /**
  1258. * Removes the specified selected item in the object from the object's
  1259. * selection. If the specified item isn't currently selected, this
  1260. * method has no effect.
  1261. *
  1262. * @param i the zero-based index of selectable items
  1263. */
  1264. public void removeAccessibleSelection(int i) {
  1265. List.this.deselect(i);
  1266. }
  1267. /**
  1268. * Clears the selection in the object, so that nothing in the
  1269. * object is selected.
  1270. */
  1271. public void clearAccessibleSelection() {
  1272. synchronized(List.this) {
  1273. int selectedIndexes[] = List.this.getSelectedIndexes();
  1274. if (selectedIndexes == null)
  1275. return;
  1276. for (int i = selectedIndexes.length; i >= 0; i--) {
  1277. List.this.deselect(selectedIndexes[i]);
  1278. }
  1279. }
  1280. }
  1281. /**
  1282. * Causes every selected item in the object to be selected
  1283. * if the object supports multiple selections.
  1284. */
  1285. public void selectAllAccessibleSelection() {
  1286. synchronized(List.this) {
  1287. for (int i = List.this.getItemCount() - 1; i >= 0; i--) {
  1288. List.this.select(i);
  1289. }
  1290. }
  1291. }
  1292. /**
  1293. * This class implements accessibility support for
  1294. * List children. It provides an implementation of the
  1295. * Java Accessibility API appropriate to list children
  1296. * user-interface elements.
  1297. */
  1298. protected class AccessibleAWTListChild extends AccessibleAWTComponent
  1299. implements Accessible {
  1300. // [[[FIXME]]] need to finish implementing this!!!
  1301. private List parent;
  1302. private int indexInParent;
  1303. public AccessibleAWTListChild(List parent, int indexInParent) {
  1304. this.parent = parent;
  1305. this.setAccessibleParent(parent);
  1306. this.indexInParent = indexInParent;
  1307. }
  1308. //
  1309. // required Accessible methods
  1310. //
  1311. /**
  1312. * Gets the AccessibleContext for this object. In the
  1313. * implementation of the Java Accessibility API for this class,
  1314. * return this object, which acts as its own AccessibleContext.
  1315. *
  1316. * @return this object
  1317. */
  1318. public AccessibleContext getAccessibleContext() {
  1319. return this;
  1320. }
  1321. //
  1322. // required AccessibleContext methods
  1323. //
  1324. /**
  1325. * Get the role of this object.
  1326. *
  1327. * @return an instance of AccessibleRole describing the role of
  1328. * the object
  1329. * @see AccessibleRole
  1330. */
  1331. public AccessibleRole getAccessibleRole() {
  1332. return AccessibleRole.LIST_ITEM;
  1333. }
  1334. /**
  1335. * Get the state set of this object. The AccessibleStateSet of an
  1336. * object is composed of a set of unique AccessibleState's. A
  1337. * change in the AccessibleStateSet of an object will cause a
  1338. * PropertyChangeEvent to be fired for the
  1339. * ACCESSIBLE_STATE_PROPERTY property.
  1340. *
  1341. * @return an instance of AccessibleStateSet containing the
  1342. * current state set of the object
  1343. * @see AccessibleStateSet
  1344. * @see AccessibleState
  1345. * @see #addPropertyChangeListener
  1346. */
  1347. public AccessibleStateSet getAccessibleStateSet() {
  1348. AccessibleStateSet states = super.getAccessibleStateSet();
  1349. if (parent.isIndexSelected(indexInParent)) {
  1350. states.add(AccessibleState.SELECTED);
  1351. }
  1352. return states;
  1353. }
  1354. /**
  1355. * Gets the locale of the component. If the component does not
  1356. * have a locale, then the locale of its parent is returned.
  1357. *
  1358. * @return This component's locale. If this component does not have
  1359. * a locale, the locale of its parent is returned.
  1360. *
  1361. * @exception IllegalComponentStateException
  1362. * If the Component does not have its own locale and has not yet
  1363. * been added to a containment hierarchy such that the locale can
  1364. * be determined from the containing parent.
  1365. */
  1366. public Locale getLocale() {
  1367. return parent.getLocale();
  1368. }
  1369. /**
  1370. * Get the 0-based index of this object in its accessible parent.
  1371. *
  1372. * @return the 0-based index of this object in its parent; -1 if
  1373. * this object does not have an accessible parent.
  1374. *
  1375. * @see #getAccessibleParent
  1376. * @see #getAccessibleChildrenCount
  1377. * @see #getAccessibleChild
  1378. */
  1379. public int getAccessibleIndexInParent() {
  1380. return indexInParent;
  1381. }
  1382. /**
  1383. * Returns the number of accessible children of the object.
  1384. *
  1385. * @return the number of accessible children of the object.
  1386. */
  1387. public int getAccessibleChildrenCount() {
  1388. return 0; // list elements can't have children
  1389. }
  1390. /**
  1391. * Return the specified Accessible child of the object. The
  1392. * Accessible children of an Accessible object are zero-based,
  1393. * so the first child of an Accessible child is at index 0, the
  1394. * second child is at index 1, and so on.
  1395. *
  1396. * @param i zero-based index of child
  1397. * @return the Accessible child of the object
  1398. * @see #getAccessibleChildrenCount
  1399. */
  1400. public Accessible getAccessibleChild(int i) {
  1401. return null; // list elements can't have children
  1402. }
  1403. //
  1404. // AccessibleComponent delegatation to parent List
  1405. //
  1406. /**
  1407. * Get the background color of this object.
  1408. *
  1409. * @return the background color, if supported, of the object;
  1410. * otherwise, null
  1411. * @see #setBackground
  1412. */
  1413. public Color getBackground() {
  1414. return parent.getBackground();
  1415. }
  1416. /**
  1417. * Set the background color of this object.
  1418. *
  1419. * @param c the new Color for the background
  1420. * @see #setBackground
  1421. */
  1422. public void setBackground(Color c) {
  1423. parent.setBackground(c);
  1424. }
  1425. /**
  1426. * Get the foreground color of this object.
  1427. *
  1428. * @return the foreground color, if supported, of the object;
  1429. * otherwise, null
  1430. * @see #setForeground
  1431. */
  1432. public Color getForeground() {
  1433. return parent.getForeground();
  1434. }
  1435. /**
  1436. * Set the foreground color of this object.
  1437. *
  1438. * @param c the new Color for the foreground
  1439. * @see #getForeground
  1440. */
  1441. public void setForeground(Color c) {
  1442. parent.setForeground(c);
  1443. }
  1444. /**
  1445. * Get the Cursor of this object.
  1446. *
  1447. * @return the Cursor, if supported, of the object; otherwise, null
  1448. * @see #setCursor
  1449. */
  1450. public Cursor getCursor() {
  1451. return parent.getCursor();
  1452. }
  1453. /**
  1454. * Set the Cursor of this object.
  1455. *
  1456. * @param cursor the new Cursor for the object
  1457. * @see #getCursor
  1458. */
  1459. public void setCursor(Cursor cursor) {
  1460. parent.setCursor(cursor);
  1461. }
  1462. /**
  1463. * Get the Font of this object.
  1464. *
  1465. * @return the Font,if supported, for the object; otherwise, null
  1466. * @see #setFont
  1467. */
  1468. public Font getFont() {
  1469. return parent.getFont();
  1470. }
  1471. /**
  1472. * Set the Font of this object.
  1473. *
  1474. * @param f the new Font for the object
  1475. * @see #getFont
  1476. */
  1477. public void setFont(Font f) {
  1478. parent.setFont(f);
  1479. }
  1480. /**
  1481. * Get the FontMetrics of this object.
  1482. *
  1483. * @param f the Font
  1484. * @return the FontMetrics, if supported, the object; otherwise, null
  1485. * @see #getFont
  1486. */
  1487. public FontMetrics getFontMetrics(Font f) {
  1488. return parent.getFontMetrics(f);
  1489. }
  1490. /**
  1491. * Determine if the object is enabled. Objects that are enabled
  1492. * will also have the AccessibleState.ENABLED state set in their
  1493. * AccessibleStateSet.
  1494. *
  1495. * @return true if object is enabled; otherwise, false
  1496. * @see #setEnabled
  1497. * @see AccessibleContext#getAccessibleStateSet
  1498. * @see AccessibleState#ENABLED
  1499. * @see AccessibleStateSet
  1500. */
  1501. public boolean isEnabled() {
  1502. return parent.isEnabled();
  1503. }
  1504. /**
  1505. * Set the enabled state of the object.
  1506. *
  1507. * @param b if true, enables this object; otherwise, disables it
  1508. * @see #isEnabled
  1509. */
  1510. public void setEnabled(boolean b) {
  1511. parent.setEnabled(b);
  1512. }
  1513. /**
  1514. * Determine if the object is visible. Note: this means that the
  1515. * object intends to be visible; however, it may not be
  1516. * showing on the screen because one of the objects that this object
  1517. * is contained by is currently not visible. To determine if an
  1518. * object is showing on the screen, use isShowing().
  1519. * <p>Objects that are visible will also have the
  1520. * AccessibleState.VISIBLE state set in their AccessibleStateSet.
  1521. *
  1522. * @return true if object is visible; otherwise, false
  1523. * @see #setVisible
  1524. * @see AccessibleContext#getAccessibleStateSet
  1525. * @see AccessibleState#VISIBLE
  1526. * @see AccessibleStateSet
  1527. */
  1528. public boolean isVisible() {
  1529. // [[[FIXME]]] needs to work like isShowing() below
  1530. return false;
  1531. // return parent.isVisible();
  1532. }
  1533. /**
  1534. * Set the visible state of the object.
  1535. *
  1536. * @param b if true, shows this object; otherwise, hides it
  1537. * @see #isVisible
  1538. */
  1539. public void setVisible(boolean b) {
  1540. // [[[FIXME]]] should scroll to item to make it show!
  1541. parent.setVisible(b);
  1542. }
  1543. /**
  1544. * Determine if the object is showing. This is determined by
  1545. * checking the visibility of the object and visibility of the
  1546. * object ancestors.
  1547. * Note: this will return true even if the object is obscured
  1548. * by another (for example, it to object is underneath a menu
  1549. * that was pulled down).
  1550. *
  1551. * @return true if object is showing; otherwise, false
  1552. */
  1553. public boolean isShowing() {
  1554. // [[[FIXME]]] only if it's showing!!!
  1555. return false;
  1556. // return parent.isShowing();
  1557. }
  1558. /**
  1559. * Checks whether the specified point is within this object's
  1560. * bounds, where the point's x and y coordinates are defined to
  1561. * be relative to the coordinate system of the object.
  1562. *
  1563. * @param p the Point relative to the coordinate system of the
  1564. * object
  1565. * @return true if object contains Point; otherwise false
  1566. * @see #getBounds
  1567. */
  1568. public boolean contains(Point p) {
  1569. // [[[FIXME]]] - only if p is within the list element!!!
  1570. return false;
  1571. // return parent.contains(p);
  1572. }
  1573. /**
  1574. * Returns the location of the object on the screen.
  1575. *
  1576. * @return location of object on screen; null if this object
  1577. * is not on the screen
  1578. * @see #getBounds
  1579. * @see #getLocation
  1580. */
  1581. public Point getLocationOnScreen() {
  1582. // [[[FIXME]]] sigh
  1583. return null;
  1584. }
  1585. /**
  1586. * Gets the location of the object relative to the parent in the
  1587. * form of a point specifying the object's top-left corner in the
  1588. * screen's coordinate space.
  1589. *
  1590. * @return An instance of Point representing the top-left corner of
  1591. * the objects's bounds in the coordinate space of the screen; null
  1592. * if this object or its parent are not on the screen
  1593. * @see #getBounds
  1594. * @see #getLocationOnScreen
  1595. */
  1596. public Point getLocation() {
  1597. // [[[FIXME]]]
  1598. return null;
  1599. }
  1600. /**
  1601. * Sets the location of the object relative to the parent.
  1602. * @param p the new position for the top-left corner
  1603. * @see #getLocation
  1604. */
  1605. public void setLocation(Point p) {
  1606. // [[[FIXME]]] maybe - can simply return as no-op
  1607. }
  1608. /**
  1609. * Gets the bounds of this object in the form of a Rectangle object.
  1610. * The bounds specify this object's width, height, and location
  1611. * relative to its parent.
  1612. *
  1613. * @return A rectangle indicating this component's bounds; null if
  1614. * this object is not on the screen.
  1615. * @see #contains
  1616. */
  1617. public Rectangle getBounds() {
  1618. // [[[FIXME]]]
  1619. return null;
  1620. }
  1621. /**
  1622. * Sets the bounds of this object in the form of a Rectangle
  1623. * object. The bounds specify this object's width, height, and
  1624. * location relative to its parent.
  1625. *
  1626. * @param r rectangle indicating this component's bounds
  1627. * @see #getBounds
  1628. */
  1629. public void setBounds(Rectangle r) {
  1630. // no-op; not supported
  1631. }
  1632. /**
  1633. * Returns the size of this object in the form of a Dimension
  1634. * object. The height field of the Dimension object contains this
  1635. * objects's height, and the width field of the Dimension object
  1636. * contains this object's width.
  1637. *
  1638. * @return A Dimension object that indicates the size of this
  1639. * component; null if this object is not on the screen
  1640. * @see #setSize
  1641. */
  1642. public Dimension getSize() {
  1643. // [[[FIXME]]]
  1644. return null;
  1645. }
  1646. /**
  1647. * Resizes this object so that it has width and height.
  1648. *
  1649. * @param d - The dimension specifying the new size of the object.
  1650. * @see #getSize
  1651. */
  1652. public void setSize(Dimension d) {
  1653. // not supported; no-op
  1654. }
  1655. /**
  1656. * Returns the <code>Accessible</code> child, if one exists,
  1657. * contained at the local coordinate <code>Point</code>.
  1658. *
  1659. * @param p the point relative to the coordinate system of this
  1660. * object
  1661. * @return the <code>Accessible</code>, if it exists,
  1662. * at the specified location; otherwise <code>null</code>
  1663. */
  1664. public Accessible getAccessibleAt(Point p) {
  1665. return null; // object cannot have children!
  1666. }
  1667. /**
  1668. * Returns whether this object can accept focus or not. Objects
  1669. * that can accept focus will also have the
  1670. * <code>AccessibleState.FOCUSABLE</code> state set in their
  1671. * <code>AccessibleStateSet</code>.
  1672. *
  1673. * @return true if object can accept focus; otherwise false
  1674. * @see AccessibleContext#getAccessibleStateSet
  1675. * @see AccessibleState#FOCUSABLE
  1676. * @see AccessibleState#FOCUSED
  1677. * @see AccessibleStateSet
  1678. */
  1679. public boolean isFocusTraversable() {
  1680. return false; // list element cannot receive focus!
  1681. }
  1682. /**
  1683. * Requests focus for this object. If this object cannot accept
  1684. * focus, nothing will happen. Otherwise, the object will attempt
  1685. * to take focus.
  1686. * @see #isFocusTraversable
  1687. */
  1688. public void requestFocus() {
  1689. // nothing to do; a no-op
  1690. }
  1691. /**
  1692. * Adds the specified focus listener to receive focus events from
  1693. * this component.
  1694. *
  1695. * @param l the focus listener
  1696. * @see #removeFocusListener
  1697. */
  1698. public void addFocusListener(FocusListener l) {
  1699. // nothing to do; a no-op
  1700. }
  1701. /**
  1702. * Removes the specified focus listener so it no longer receives
  1703. * focus events from this component.
  1704. *
  1705. * @param l the focus listener
  1706. * @see #addFocusListener
  1707. */
  1708. public void removeFocusListener(FocusListener l) {
  1709. // nothing to do; a no-op
  1710. }
  1711. } // inner class AccessibleAWTListChild
  1712. } // inner class AccessibleAWTList
  1713. }