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