0001: /*
0002: * @(#)List.java 1.84 06/10/10
0003: *
0004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
0005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
0006: *
0007: * This program is free software; you can redistribute it and/or
0008: * modify it under the terms of the GNU General Public License version
0009: * 2 only, as published by the Free Software Foundation.
0010: *
0011: * This program is distributed in the hope that it will be useful, but
0012: * WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0014: * General Public License version 2 for more details (a copy is
0015: * included at /legal/license.txt).
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * version 2 along with this work; if not, write to the Free Software
0019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020: * 02110-1301 USA
0021: *
0022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
0023: * Clara, CA 95054 or visit www.sun.com if you need additional
0024: * information or have any questions.
0025: *
0026: */
0027: package java.awt;
0028:
0029: import java.util.Vector;
0030: import sun.awt.peer.ListPeer;
0031: import sun.awt.PeerBasedToolkit;
0032: import java.awt.event.*;
0033: import java.io.ObjectOutputStream;
0034: import java.io.ObjectInputStream;
0035: import java.io.IOException;
0036: import java.awt.AWTEventMulticaster;
0037: import java.util.EventListener;
0038:
0039: /**
0040: * The <code>List</code> component presents the user with a
0041: * scrolling list of text items. The list can be set up so that
0042: * the user can choose either one item or multiple items.
0043: * <p>
0044: * For example, the code . . .
0045: * <p>
0046: * <hr><blockquote><pre>
0047: * List lst = new List(4, false);
0048: * lst.add("Mercury");
0049: * lst.add("Venus");
0050: * lst.add("Earth");
0051: * lst.add("JavaSoft");
0052: * lst.add("Mars");
0053: * lst.add("Jupiter");
0054: * lst.add("Saturn");
0055: * lst.add("Uranus");
0056: * lst.add("Neptune");
0057: * lst.add("Pluto");
0058: * cnt.add(lst);
0059: * </pre></blockquote><hr>
0060: * <p>
0061: * where <code>cnt</code> is a container, produces the following
0062: * scrolling list:
0063: * <p>
0064: * <img src="doc-files/List-1.gif"
0065: * ALIGN=center HSPACE=10 VSPACE=7>
0066: * <p>
0067: * Clicking on an item that isn't selected selects it. Clicking on
0068: * an item that is already selected deselects it. In the preceding
0069: * example, only one item from the scrolling list can be selected
0070: * at a time, since the second argument when creating the new scrolling
0071: * list is <code>false</code>. Selecting an item causes any other
0072: * selected item to be automatically deselected.
0073: * <p>
0074: * Beginning with Java 1.1, the Abstract Window Toolkit
0075: * sends the <code>List</code> object all mouse, keyboard, and focus events
0076: * that occur over it. (The old AWT event model is being maintained
0077: * only for backwards compatibility, and its use is discouraged.)
0078: * <p>
0079: * When an item is selected or deselected, AWT sends an instance
0080: * of <code>ItemEvent</code> to the list.
0081: * When the user double-clicks on an item in a scrolling list,
0082: * AWT sends an instance of <code>ActionEvent</code> to the
0083: * list following the item event. AWT also generates an action event
0084: * when the user presses the return key while an item in the
0085: * list is selected.
0086: * <p>
0087: * If an application wants to perform some action based on an item
0088: * in this list being selected or activated, it should implement
0089: * <code>ItemListener</code> or <code>ActionListener</code>
0090: * as appropriate and register the new listener to receive
0091: * events from this list.
0092: * <p>
0093: * For multiple-selection scrolling lists, it is considered a better
0094: * user interface to use an external gesture (such as clicking on a
0095: * button) to trigger the action.
0096: * @version 1.77, %G
0097: * @author Sami Shaio
0098: * @see java.awt.event.ItemEvent
0099: * @see java.awt.event.ItemListener
0100: * @see java.awt.event.ActionEvent
0101: * @see java.awt.event.ActionListener
0102: * @since JDK1.0
0103: */
0104: public class List extends Component implements ItemSelectable {
0105: /**
0106: * A vector created to contain items which will become
0107: * part of the List Component.
0108: *
0109: * @serial
0110: * @see addItem()
0111: * @see getItem()
0112: */
0113: Vector items = new Vector();
0114: /**
0115: * This field will represent the number of rows in the
0116: * List Component. It is specified only once, and
0117: * that is when the list component is actually
0118: * created. It will never change.
0119: *
0120: * @serial
0121: * @see getRows()
0122: */
0123: int rows = 0;
0124: /**
0125: * <code>multipleMode</code> is a variable that will
0126: * be set to <code>true</code> if a list component is to be set to
0127: * multiple selection mode, that is where the user can
0128: * select more than one item in a list at one time.
0129: * <code>multipleMode</code> will be set to false if the
0130: * list component is set to single selection, that is where
0131: * the user can only select one item on the list at any
0132: * one time.
0133: *
0134: * @serial
0135: * @see isMultipleMode()
0136: * @see setMultipleMode()
0137: */
0138: boolean multipleMode = false;
0139: /**
0140: * <code>selected</code> is an array that will contain
0141: * the indices of items that have been selected.
0142: *
0143: * @serial
0144: * @see getSelectedIndexes()
0145: * @see getSelectedIndex()
0146: */
0147: int selected[] = new int[0];
0148: /**
0149: * This variable contains the value that will be used
0150: * when trying to make a particular list item visible.
0151: *
0152: * @serial
0153: * @see makeVisible()
0154: */
0155: int visibleIndex = -1;
0156: transient ActionListener actionListener;
0157: transient ItemListener itemListener;
0158: private static final String base = "list";
0159: private static int nameCounter = 0;
0160: /*
0161: * JDK 1.1 serialVersionUID
0162: */
0163: private static final long serialVersionUID = -3304312411574666869L;
0164:
0165: /**
0166: * Creates a new scrolling list.
0167: * By default, there are four visible lines and multiple selections are
0168: * not allowed.
0169: */
0170: public List() {
0171: this (0, false);
0172: }
0173:
0174: /**
0175: * Creates a new scrolling list initialized with the specified
0176: * number of visible lines. By default, multiple selections are
0177: * not allowed.
0178: * @param rows the number of items to show.
0179: * @since JDK1.1
0180: */
0181: public List(int rows) {
0182: this (rows, false);
0183: }
0184:
0185: /**
0186: * The default number of visible rows is 4. A list with
0187: * zero rows is unusable and unsightly.
0188: */
0189: final static int DEFAULT_VISIBLE_ROWS = 4;
0190:
0191: /**
0192: * Creates a new scrolling list initialized to display the specified
0193: * number of rows. If the value of <code>multipleMode</code> is
0194: * <code>true</code>, then the user can select multiple items from
0195: * the list. If it is <code>false</code>, only one item at a time
0196: * can be selected.
0197: * @param rows the number of items to show.
0198: * @param multipleMode if <code>true</code>,
0199: * then multiple selections are allowed;
0200: * otherwise, only one item can be selected at a time.
0201: */
0202: public List(int rows, boolean multipleMode) {
0203: this .rows = (rows != 0) ? rows : DEFAULT_VISIBLE_ROWS;
0204: this .multipleMode = multipleMode;
0205: }
0206:
0207: /**
0208: * Construct a name for this component. Called by getName() when the
0209: * name is null.
0210: */
0211: String constructComponentName() {
0212: return base + nameCounter++;
0213: }
0214:
0215: /**
0216: * Creates the peer for the list. The peer allows us to modify the
0217: * list's appearance without changing its functionality.
0218: */
0219: public void addNotify() {
0220: synchronized (getTreeLock()) {
0221: if (peer == null)
0222: peer = ((PeerBasedToolkit) getToolkit())
0223: .createList(this );
0224: super .addNotify();
0225: visibleIndex = -1;
0226: }
0227: }
0228:
0229: /**
0230: * Removes the peer for this list. The peer allows us to modify the
0231: * list's appearance without changing its functionality.
0232: */
0233: public void removeNotify() {
0234: synchronized (getTreeLock()) {
0235: ListPeer peer = (ListPeer) this .peer;
0236: if (peer != null) {
0237: selected = peer.getSelectedIndexes();
0238: }
0239: super .removeNotify();
0240: }
0241: }
0242:
0243: /**
0244: * Gets the number of items in the list.
0245: * @return the number of items in the list.
0246: * @see java.awt.List#getItem
0247: * @since JDK1.1
0248: */
0249: public int getItemCount() {
0250: return countItems();
0251: }
0252:
0253: /**
0254: * @deprecated As of JDK version 1.1,
0255: * replaced by <code>getItemCount()</code>.
0256: */
0257: public int countItems() {
0258: return items.size();
0259: }
0260:
0261: /**
0262: * Gets the item associated with the specified index.
0263: * @return an item that is associated with
0264: * the specified index.
0265: * @param index the position of the item.
0266: * @see java.awt.List#getItemCount
0267: */
0268: public String getItem(int index) {
0269: return (String) items.elementAt(index);
0270: }
0271:
0272: /**
0273: * Gets the items in the list.
0274: * @return a string array containing items of the list.
0275: * @see java.awt.List#select
0276: * @see java.awt.List#deselect
0277: * @see java.awt.List#isIndexSelected
0278: * @since JDK1.1
0279: */
0280: public synchronized String[] getItems() {
0281: String itemCopies[] = new String[items.size()];
0282: items.copyInto(itemCopies);
0283: return itemCopies;
0284: }
0285:
0286: /**
0287: * Adds the specified item to the end of scrolling list.
0288: * @param item the item to be added.
0289: * @since JDK1.1
0290: */
0291: public void add(String item) {
0292: addItem(item);
0293: }
0294:
0295: /**
0296: * @deprecated replaced by <code>add(String)</code>.
0297: */
0298: public void addItem(String item) {
0299: addItem(item, -1);
0300: }
0301:
0302: /**
0303: * Adds the specified item to the the scrolling list
0304: * at the position indicated by the index. The index is
0305: * zero-based. If the value of the index is less than zero,
0306: * or if the value of the index is greater than or equal to
0307: * the number of items in the list, then the item is added
0308: * to the end of the list.
0309: * @param item the item to be added.
0310: * If this parameter is null then the item is
0311: * treated as an empty string, <code>""</code>.
0312: * @param index the position at which to add the item.
0313: * @since JDK1.1
0314: */
0315: public void add(String item, int index) {
0316: addItem(item, index);
0317: }
0318:
0319: /**
0320: * @deprecated replaced by <code>add(String, int)</code>.
0321: */
0322: public synchronized void addItem(String item, int index) {
0323: if (index < -1 || index >= items.size()) {
0324: index = -1;
0325: }
0326: if (item == null)
0327: item = "";
0328: if (index == -1) {
0329: items.addElement(item);
0330: } else {
0331: items.insertElementAt(item, index);
0332: }
0333: ListPeer peer = (ListPeer) this .peer;
0334: if (peer != null) {
0335: peer.add(item, index);
0336: }
0337: }
0338:
0339: /**
0340: * Replaces the item at the specified index in the scrolling list
0341: * with the new string.
0342: * @param newValue a new string to replace an existing item.
0343: * @param index the position of the item to replace.
0344: */
0345: public synchronized void replaceItem(String newValue, int index) {
0346: remove(index);
0347: add(newValue, index);
0348: }
0349:
0350: /**
0351: * Removes all items from this list.
0352: * @see #remove
0353: * @see #delItems
0354: * @since JDK1.1
0355: */
0356: public void removeAll() {
0357: clear();
0358: }
0359:
0360: /**
0361: * @deprecated As of JDK version 1.1,
0362: * replaced by <code>removeAll()</code>.
0363: */
0364: public synchronized void clear() {
0365: ListPeer peer = (ListPeer) this .peer;
0366: if (peer != null) {
0367: peer.removeAll();
0368: }
0369: items = new Vector();
0370: selected = new int[0];
0371: }
0372:
0373: /**
0374: * Removes the first occurrence of an item from the list.
0375: * @param item the item to remove from the list.
0376: * @exception IllegalArgumentException
0377: * if the item doesn't exist in the list.
0378: * @since JDK1.1
0379: */
0380: public synchronized void remove(String item) {
0381: int index = items.indexOf(item);
0382: if (index < 0) {
0383: throw new IllegalArgumentException("item " + item
0384: + " not found in list");
0385: } else {
0386: remove(index);
0387: }
0388: }
0389:
0390: /**
0391: * Remove the item at the specified position
0392: * from this scrolling list.
0393: * @param position the index of the item to delete.
0394: * @see java.awt.List#add(String, int)
0395: * @since JDK1.1
0396: * @exception ArrayIndexOutOfBoundsException
0397: * if the <code>position</code> is less than 0 or
0398: * greater than <code>getItemCount()-1</code>
0399: */
0400: public void remove(int position) {
0401: delItem(position);
0402: }
0403:
0404: /**
0405: * @deprecated replaced by <code>remove(String)</code>
0406: * and <code>remove(int)</code>.
0407: */
0408: public void delItem(int position) {
0409: delItems(position, position);
0410: }
0411:
0412: /**
0413: * Gets the index of the selected item on the list,
0414: * @return the index of the selected item, or
0415: * <code>-1</code> if no item is selected,
0416: * or if more that one item is selected.
0417: * @see java.awt.List#select
0418: * @see java.awt.List#deselect
0419: * @see java.awt.List#isIndexSelected
0420: */
0421: public synchronized int getSelectedIndex() {
0422: int sel[] = getSelectedIndexes();
0423: return (sel.length == 1) ? sel[0] : -1;
0424: }
0425:
0426: /**
0427: * Gets the selected indexes on the list.
0428: * @return an array of the selected indexes
0429: * of this scrolling list. If no items are
0430: * selected, a zero-length array is returned.
0431: * @see java.awt.List#select
0432: * @see java.awt.List#deselect
0433: * @see java.awt.List#isIndexSelected
0434: */
0435: public synchronized int[] getSelectedIndexes() {
0436: ListPeer peer = (ListPeer) this .peer;
0437: if (peer != null) {
0438: selected = ((ListPeer) peer).getSelectedIndexes();
0439: }
0440: return selected;
0441: }
0442:
0443: /**
0444: * Get the selected item on this scrolling list.
0445: * @return the selected item on the list,
0446: * or null if no item is selected.
0447: * @see java.awt.List#select
0448: * @see java.awt.List#deselect
0449: * @see java.awt.List#isIndexSelected
0450: */
0451: public synchronized String getSelectedItem() {
0452: int index = getSelectedIndex();
0453: return (index < 0) ? null : getItem(index);
0454: }
0455:
0456: /**
0457: * Get the selected items on this scrolling list.
0458: * @return an array of the selected items
0459: * on this scrolling list.
0460: * @see java.awt.List#select
0461: * @see java.awt.List#deselect
0462: * @see java.awt.List#isIndexSelected
0463: */
0464: public synchronized String[] getSelectedItems() {
0465: int sel[] = getSelectedIndexes();
0466: String str[] = new String[sel.length];
0467: for (int i = 0; i < sel.length; i++) {
0468: str[i] = getItem(sel[i]);
0469: }
0470: return str;
0471: }
0472:
0473: /**
0474: * Returns the selected items on the list in an array of Objects.
0475: * @see ItemSelectable
0476: */
0477: public Object[] getSelectedObjects() {
0478: return getSelectedItems();
0479: }
0480:
0481: /**
0482: * Selects the item at the specified index in the scrolling list.
0483: * @param index the position of the item to select.
0484: * @see java.awt.List#getSelectedItem
0485: * @see java.awt.List#deselect
0486: * @see java.awt.List#isIndexSelected
0487: */
0488: public void select(int index) {
0489: // Bug #4059614: select can't be synchronized while calling the peer,
0490: // because it is called from the Window Thread. It is sufficient to
0491: // synchronize the code that manipulates 'selected' except for the
0492: // case where the peer changes. To handle this case, we simply
0493: // repeat the selection process.
0494:
0495: ListPeer peer;
0496: do {
0497: peer = (ListPeer) this .peer;
0498: if (peer != null) {
0499: peer.select(index);
0500: return;
0501: }
0502: synchronized (this ) {
0503: boolean alreadySelected = false;
0504: for (int i = 0; i < selected.length; i++) {
0505: if (selected[i] == index) {
0506: alreadySelected = true;
0507: break;
0508: }
0509: }
0510: if (!alreadySelected) {
0511: if (!multipleMode) {
0512: selected = new int[1];
0513: selected[0] = index;
0514: } else {
0515: int newsel[] = new int[selected.length + 1];
0516: System.arraycopy(selected, 0, newsel, 0,
0517: selected.length);
0518: newsel[selected.length] = index;
0519: selected = newsel;
0520: }
0521: }
0522: }
0523: } while (peer != this .peer);
0524: }
0525:
0526: /**
0527: * Deselects the item at the specified index.
0528: * <p>
0529: * If the item at the specified index is not selected, or if the
0530: * index is out of range, then the operation is ignored.
0531: * @param index the position of the item to deselect.
0532: * @see java.awt.List#select
0533: * @see java.awt.List#getSelectedItem
0534: * @see java.awt.List#isIndexSelected
0535: */
0536: public synchronized void deselect(int index) {
0537: ListPeer peer = (ListPeer) this .peer;
0538: if (peer != null) {
0539: peer.deselect(index);
0540: }
0541: for (int i = 0; i < selected.length; i++) {
0542: if (selected[i] == index) {
0543: int newsel[] = new int[selected.length - 1];
0544: System.arraycopy(selected, 0, newsel, 0, i);
0545: System.arraycopy(selected, i + 1, newsel, i,
0546: selected.length - (i + 1));
0547: selected = newsel;
0548: return;
0549: }
0550: }
0551: }
0552:
0553: /**
0554: * Determines if the specified item in this scrolling list is
0555: * selected.
0556: * @param index the item to be checked.
0557: * @return <code>true</code> if the specified item has been
0558: * selected; <code>false</code> otherwise.
0559: * @see java.awt.List#select
0560: * @see java.awt.List#deselect
0561: * @since JDK1.1
0562: */
0563: public boolean isIndexSelected(int index) {
0564: return isSelected(index);
0565: }
0566:
0567: /**
0568: * @deprecated As of JDK version 1.1,
0569: * replaced by <code>isIndexSelected(int)</code>.
0570: */
0571: public boolean isSelected(int index) {
0572: int sel[] = getSelectedIndexes();
0573: for (int i = 0; i < sel.length; i++) {
0574: if (sel[i] == index) {
0575: return true;
0576: }
0577: }
0578: return false;
0579: }
0580:
0581: /**
0582: * Get the number of visible lines in this list.
0583: * @return the number of visible lines in this scrolling list.
0584: */
0585: public int getRows() {
0586: return rows;
0587: }
0588:
0589: /**
0590: * Determines whether this list allows multiple selections.
0591: * @return <code>true</code> if this list allows multiple
0592: * selections; otherwise, <code>false</code>.
0593: * @see java.awt.List#setMultipleMode
0594: * @since JDK1.1
0595: */
0596: public boolean isMultipleMode() {
0597: return allowsMultipleSelections();
0598: }
0599:
0600: /**
0601: * @deprecated As of JDK version 1.1,
0602: * replaced by <code>isMultipleMode()</code>.
0603: */
0604: public boolean allowsMultipleSelections() {
0605: return multipleMode;
0606: }
0607:
0608: /**
0609: * Sets the flag that determines whether this list
0610: * allows multiple selections.
0611: * @param b if <code>true</code> then multiple selections
0612: * are allowed; otherwise, only one item from
0613: * the list can be selected at once.
0614: * @see java.awt.List#isMultipleMode
0615: * @since JDK1.1
0616: */
0617: public void setMultipleMode(boolean b) {
0618: setMultipleSelections(b);
0619: }
0620:
0621: /**
0622: * @deprecated As of JDK version 1.1,
0623: * replaced by <code>setMultipleMode(boolean)</code>.
0624: */
0625: public synchronized void setMultipleSelections(boolean b) {
0626: if (b != multipleMode) {
0627: multipleMode = b;
0628: ListPeer peer = (ListPeer) this .peer;
0629: if (peer != null) {
0630: peer.setMultipleMode(b);
0631: }
0632: }
0633: }
0634:
0635: /**
0636: * Gets the index of the item that was last made visible by
0637: * the method <code>makeVisible</code>.
0638: * @return the index of the item that was last made visible.
0639: * @see java.awt.List#makeVisible
0640: */
0641: public int getVisibleIndex() {
0642: return visibleIndex;
0643: }
0644:
0645: /**
0646: * Makes the item at the specified index visible.
0647: * @param index the position of the item.
0648: * @see java.awt.List#getVisibleIndex
0649: */
0650: public synchronized void makeVisible(int index) {
0651: visibleIndex = index;
0652: ListPeer peer = (ListPeer) this .peer;
0653: if (peer != null) {
0654: peer.makeVisible(index);
0655: }
0656: }
0657:
0658: /**
0659: * Gets the preferred dimensions for a list with the specified
0660: * number of rows.
0661: * @param rows number of rows in the list.
0662: * @return the preferred dimensions for displaying this scrolling list
0663: * given that the specified number of rows must be visible.
0664: * @see java.awt.Component#getPreferredSize
0665: * @since JDK1.1
0666: */
0667: public Dimension getPreferredSize(int rows) {
0668: return preferredSize(rows);
0669: }
0670:
0671: /**
0672: * @deprecated As of JDK version 1.1,
0673: * replaced by <code>getPreferredSize(int)</code>.
0674: */
0675: public Dimension preferredSize(int rows) {
0676: synchronized (getTreeLock()) {
0677: ListPeer peer = (ListPeer) this .peer;
0678: return (peer != null) ? peer.getPreferredSize(rows) : super
0679: .preferredSize();
0680: }
0681: }
0682:
0683: /**
0684: * Gets the preferred size of this scrolling list.
0685: * @return the preferred dimensions for displaying this scrolling list.
0686: * @see java.awt.Component#getPreferredSize
0687: * @since JDK1.1
0688: */
0689: public Dimension getPreferredSize() {
0690: return preferredSize();
0691: }
0692:
0693: /**
0694: * @deprecated As of JDK version 1.1,
0695: * replaced by <code>getPreferredSize()</code>.
0696: */
0697: public Dimension preferredSize() {
0698: synchronized (getTreeLock()) {
0699: return (rows > 0) ? preferredSize(rows) : super
0700: .preferredSize();
0701: }
0702: }
0703:
0704: /**
0705: * Gets the minumum dimensions for a list with the specified
0706: * number of rows.
0707: * @param rows number of rows in the list.
0708: * @return the minimum dimensions for displaying this scrolling list
0709: * given that the specified number of rows must be visible.
0710: * @see java.awt.Component#getMinimumSize
0711: * @since JDK1.1
0712: */
0713: public Dimension getMinimumSize(int rows) {
0714: return minimumSize(rows);
0715: }
0716:
0717: /**
0718: * @deprecated As of JDK version 1.1,
0719: * replaced by <code>getMinimumSize(int)</code>.
0720: */
0721: public Dimension minimumSize(int rows) {
0722: synchronized (getTreeLock()) {
0723: ListPeer peer = (ListPeer) this .peer;
0724: return (peer != null) ? peer.getMinimumSize(rows) : super
0725: .minimumSize();
0726: }
0727: }
0728:
0729: /**
0730: * Determines the minimum size of this scrolling list.
0731: * @return the minimum dimensions needed
0732: * to display this scrolling list.
0733: * @see java.awt.Component#getMinimumSize()
0734: * @since JDK1.1
0735: */
0736: public Dimension getMinimumSize() {
0737: return minimumSize();
0738: }
0739:
0740: /**
0741: * @deprecated As of JDK version 1.1,
0742: * replaced by <code>getMinimumSize()</code>.
0743: */
0744: public Dimension minimumSize() {
0745: synchronized (getTreeLock()) {
0746: return (rows > 0) ? minimumSize(rows) : super .minimumSize();
0747: }
0748: }
0749:
0750: /**
0751: * Adds the specified item listener to receive item events from
0752: * this list.
0753: * If l is null, no exception is thrown and no action is performed.
0754: *
0755: * @param l the item listener.
0756: * @see java.awt.event.ItemEvent
0757: * @see java.awt.event.ItemListener
0758: * @see java.awt.List#removeItemListener
0759: * @since JDK1.1
0760: */
0761: public synchronized void addItemListener(ItemListener l) {
0762: itemListener = AWTEventMulticaster.add(itemListener, l);
0763: newEventsOnly = true;
0764: }
0765:
0766: /**
0767: * Removes the specified item listener so that it no longer
0768: * receives item events from this list.
0769: * If l is null, no exception is thrown and no action is performed.
0770: *
0771: * @param l the item listener.
0772: * @see java.awt.event.ItemEvent
0773: * @see java.awt.event.ItemListener
0774: * @see java.awt.List#addItemListener
0775: * @since JDK1.1
0776: */
0777: public synchronized void removeItemListener(ItemListener l) {
0778: itemListener = AWTEventMulticaster.remove(itemListener, l);
0779: }
0780:
0781: /**
0782: * Returns an array of all the item listeners
0783: * registered on this list.
0784: *
0785: * @return all of this list's <code>ItemListener</code>s
0786: * or an empty array if no item
0787: * listeners are currently registered
0788: *
0789: * @see #addItemListener
0790: * @see #removeItemListener
0791: * @see java.awt.event.ItemEvent
0792: * @see java.awt.event.ItemListener
0793: * @since 1.4
0794: */
0795: public synchronized ItemListener[] getItemListeners() {
0796: return (ItemListener[]) AWTEventMulticaster.getListeners(
0797: (EventListener) itemListener, ItemListener.class);
0798: }
0799:
0800: /**
0801: * Adds the specified action listener to receive action events from
0802: * this list. Action events occur when a user double-clicks
0803: * on a list item.
0804: * If l is null, no exception is thrown and no action is performed.
0805: *
0806: * @param l the action listener.
0807: * @see java.awt.event.ActionEvent
0808: * @see java.awt.event.ActionListener
0809: * @see java.awt.List#removeActionListener
0810: * @since JDK1.1
0811: */
0812: public synchronized void addActionListener(ActionListener l) {
0813: actionListener = AWTEventMulticaster.add(actionListener, l);
0814: newEventsOnly = true;
0815: }
0816:
0817: /**
0818: * Removes the specified action listener so that it no longer
0819: * receives action events from this list. Action events
0820: * occur when a user double-clicks on a list item.
0821: * If l is null, no exception is thrown and no action is performed.
0822: *
0823: * @param l the action listener.
0824: * @see java.awt.event.ActionEvent
0825: * @see java.awt.event.ActionListener
0826: * @see java.awt.List#addActionListener
0827: * @since JDK1.1
0828: */
0829: public synchronized void removeActionListener(ActionListener l) {
0830: actionListener = AWTEventMulticaster.remove(actionListener, l);
0831: }
0832:
0833: /**
0834: * Returns an array of all the action listeners
0835: * registered on this list.
0836: *
0837: * @return all of this list's <code>ActionListener</code>s
0838: * or an empty array if no action
0839: * listeners are currently registered
0840: *
0841: * @see #addActionListener
0842: * @see #removeActionListener
0843: * @see java.awt.event.ActionEvent
0844: * @see java.awt.event.ActionListener
0845: * @since 1.4
0846: */
0847: public synchronized ActionListener[] getActionListeners() {
0848: return (ActionListener[]) AWTEventMulticaster.getListeners(
0849: (EventListener) actionListener, ActionListener.class);
0850: }
0851:
0852: // NOTE: remove when filtering is done at lower level
0853: boolean eventEnabled(AWTEvent e) {
0854: switch (e.id) {
0855: case ActionEvent.ACTION_PERFORMED:
0856: if ((eventMask & AWTEvent.ACTION_EVENT_MASK) != 0
0857: || actionListener != null) {
0858: return true;
0859: }
0860: return false;
0861:
0862: case ItemEvent.ITEM_STATE_CHANGED:
0863: if ((eventMask & AWTEvent.ITEM_EVENT_MASK) != 0
0864: || itemListener != null) {
0865: return true;
0866: }
0867: return false;
0868:
0869: default:
0870: break;
0871: }
0872: return super .eventEnabled(e);
0873: }
0874:
0875: /**
0876: * Processes events on this scrolling list. If an event is
0877: * an instance of <code>ItemEvent</code>, it invokes the
0878: * <code>processItemEvent</code> method. Else, if the
0879: * event is an instance of <code>ActionEvent</code>,
0880: * it invokes <code>processActionEvent</code>.
0881: * If the event is not an item event or an action event,
0882: * it invokes <code>processEvent</code> on the superclass.
0883: * @param e the event.
0884: * @see java.awt.event.ActionEvent
0885: * @see java.awt.event.ItemEvent
0886: * @see java.awt.List#processActionEvent
0887: * @see java.awt.List#processItemEvent
0888: * @since JDK1.1
0889: */
0890: protected void processEvent(AWTEvent e) {
0891: if (e instanceof ItemEvent) {
0892: processItemEvent((ItemEvent) e);
0893: return;
0894: } else if (e instanceof ActionEvent) {
0895: processActionEvent((ActionEvent) e);
0896: return;
0897: }
0898: super .processEvent(e);
0899: }
0900:
0901: /**
0902: * Processes item events occurring on this list by
0903: * dispatching them to any registered
0904: * <code>ItemListener</code> objects.
0905: * <p>
0906: * This method is not called unless item events are
0907: * enabled for this component. Item events are enabled
0908: * when one of the following occurs:
0909: * <p><ul>
0910: * <li>An <code>ItemListener</code> object is registered
0911: * via <code>addItemListener</code>.
0912: * <li>Item events are enabled via <code>enableEvents</code>.
0913: * </ul>
0914: * @param e the item event.
0915: * @see java.awt.event.ItemEvent
0916: * @see java.awt.event.ItemListener
0917: * @see java.awt.List#addItemListener
0918: * @see java.awt.Component#enableEvents
0919: * @since JDK1.1
0920: */
0921: protected void processItemEvent(ItemEvent e) {
0922: if (itemListener != null) {
0923: itemListener.itemStateChanged(e);
0924: }
0925: }
0926:
0927: /**
0928: * Processes action events occurring on this component
0929: * by dispatching them to any registered
0930: * <code>ActionListener</code> objects.
0931: * <p>
0932: * This method is not called unless action events are
0933: * enabled for this component. Action events are enabled
0934: * when one of the following occurs:
0935: * <p><ul>
0936: * <li>An <code>ActionListener</code> object is registered
0937: * via <code>addActionListener</code>.
0938: * <li>Action events are enabled via <code>enableEvents</code>.
0939: * </ul>
0940: * @param e the action event.
0941: * @see java.awt.event.ActionEvent
0942: * @see java.awt.event.ActionListener
0943: * @see java.awt.List#addActionListener
0944: * @see java.awt.Component#enableEvents
0945: * @since JDK1.1
0946: */
0947: protected void processActionEvent(ActionEvent e) {
0948: if (actionListener != null) {
0949: actionListener.actionPerformed(e);
0950: }
0951: }
0952:
0953: /**
0954: * Returns the parameter string representing the state of this
0955: * scrolling list. This string is useful for debugging.
0956: * @return the parameter string of this scrolling list.
0957: */
0958: protected String paramString() {
0959: return super .paramString() + ",selected=" + getSelectedItem();
0960: }
0961:
0962: /**
0963: * @deprecated As of JDK version 1.1,
0964: * Not for public use in the future.
0965: * This method is expected to be retained only as a package
0966: * private method.
0967: */
0968: public synchronized void delItems(int start, int end) {
0969: for (int i = end; i >= start; i--) {
0970: items.removeElementAt(i);
0971: }
0972: ListPeer peer = (ListPeer) this .peer;
0973: if (peer != null) {
0974: peer.delItems(start, end);
0975: }
0976: }
0977:
0978: /*
0979: * Serialization support. Since the value of the selected
0980: * field isn't neccessarily up to date we sync it up with the
0981: * peer before serializing.
0982: */
0983:
0984: private int listSerializedDataVersion = 1;
0985:
0986: /**
0987: * Writes default serializable fields to stream. Writes
0988: * a list of serializable ItemListener(s) as optional data.
0989: * The non-serializable ItemListner(s) are detected and
0990: * no attempt is made to serialize them.
0991: *
0992: * @serialData Null terminated sequence of 0 or more pairs.
0993: * The pair consists of a String and Object.
0994: * The String indicates the type of object and
0995: * is one of the following :
0996: * itemListenerK indicating and ItemListener object.
0997: *
0998: * @see AWTEventMulticaster.save(ObjectOutputStream, String, EventListener)
0999: * @see java.awt.Component.itemListenerK
1000: */
1001: private void writeObject(ObjectOutputStream s) throws IOException {
1002: synchronized (this ) {
1003: ListPeer peer = (ListPeer) this .peer;
1004: if (peer != null) {
1005: selected = peer.getSelectedIndexes();
1006: }
1007: }
1008: s.defaultWriteObject();
1009: AWTEventMulticaster.save(s, itemListenerK, itemListener);
1010: AWTEventMulticaster.save(s, actionListenerK, actionListener);
1011: s.writeObject(null);
1012: }
1013:
1014: private void readObject(ObjectInputStream s)
1015: throws ClassNotFoundException, IOException {
1016: s.defaultReadObject();
1017: Object keyOrNull;
1018: while (null != (keyOrNull = s.readObject())) {
1019: String key = ((String) keyOrNull).intern();
1020: if (itemListenerK == key)
1021: addItemListener((ItemListener) (s.readObject()));
1022: else if (actionListenerK == key)
1023: addActionListener((ActionListener) (s.readObject()));
1024: else
1025: // skip value for unrecognized key
1026: s.readObject();
1027: }
1028: }
1029: }
|