source: trunk/gcc/libjava/java/awt/List.java@ 3204

Last change on this file since 3204 was 1392, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 25.1 KB
Line 
1/* List.java -- A listbox widget
2 Copyright (C) 1999, 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38
39package java.awt;
40
41import java.io.Serializable;
42import java.awt.event.ActionEvent;
43import java.awt.event.ActionListener;
44import java.awt.event.ItemEvent;
45import java.awt.event.ItemListener;
46import java.awt.peer.ListPeer;
47import java.awt.peer.ComponentPeer;
48import java.util.Vector;
49import javax.accessibility.Accessible;
50
51/**
52 * Class that implements a listbox widget
53 *
54 * @author Aaron M. Renn ([email protected])
55 */
56public class List extends Component
57 implements ItemSelectable, Serializable, Accessible
58{
59
60/*
61 * Static Variables
62 */
63
64// Serialization constant
65private static final long serialVersionUID = -3304312411574666869L;
66
67/*************************************************************************/
68
69/*
70 * Instance Variables
71 */
72
73// FIXME: Need read/writeObject
74
75/**
76 * @serial The items in the list.
77 */
78private Vector items = new Vector();
79
80/**
81 * @serial Indicates whether or not multiple items can be selected
82 * simultaneously.
83 */
84private boolean multipleMode;
85
86/**
87 * @serial The number of rows in the list. This is set on creation
88 * only and cannot be modified.
89 */
90private int rows;
91
92/**
93 * @serial An array of the item indices that are selected.
94 */
95private int[] selected;
96
97/**
98 * @serial An index value used by <code>makeVisible()</code> and
99 * <code>getVisibleIndex</code>.
100 */
101private int visibleIndex;
102
103// The list of ItemListeners for this object.
104private ItemListener item_listeners;
105
106// The list of ActionListeners for this object.
107private ActionListener action_listeners;
108
109/*************************************************************************/
110
111/*
112 * Constructors
113 */
114
115/**
116 * Initializes a new instance of <code>List</code> with no visible lines
117 * and multi-select disabled.
118 *
119 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
120 */
121public
122List()
123{
124 this(4, false);
125}
126
127/*************************************************************************/
128
129/**
130 * Initializes a new instance of <code>List</code> with the specified
131 * number of visible lines and multi-select disabled.
132 *
133 * @param lines The number of visible lines in the list.
134 *
135 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
136 */
137public
138List(int rows)
139{
140 this(rows, false);
141}
142
143/*************************************************************************/
144
145/**
146 * Initializes a new instance of <code>List</code> with the specified
147 * number of lines and the specified multi-select setting.
148 *
149 * @param lines The number of visible lines in the list.
150 * @param multipleMode <code>true</code> if multiple lines can be selected
151 * simultaneously, <code>false</code> otherwise.
152 *
153 * @exception HeadlessException If GraphicsEnvironment.isHeadless() is true.
154 */
155public
156List(int rows, boolean multipleMode)
157{
158 this.rows = rows;
159 this.multipleMode = multipleMode;
160
161 if (GraphicsEnvironment.isHeadless())
162 throw new HeadlessException ();
163}
164
165/*************************************************************************/
166
167/*
168 * Instance Variables
169 */
170
171/**
172 * Returns the number of items in this list.
173 *
174 * @return The number of items in this list.
175 */
176public int
177getItemCount()
178{
179 return(items.size());
180}
181
182/*************************************************************************/
183
184/**
185 * Returns the number of items in this list.
186 *
187 * @return The number of items in this list.
188 *
189 * @deprecated This method is deprecated in favor of
190 * <code>getItemCount()</code>
191 */
192public int
193countItems()
194{
195 return(getItemCount());
196}
197
198/*************************************************************************/
199
200/**
201 * Returns the complete list of items.
202 *
203 * @return The complete list of items in the list.
204 */
205public synchronized String[]
206getItems()
207{
208 String[] l_items = new String[getItemCount()];
209
210 items.copyInto(l_items);
211 return(l_items);
212}
213
214/*************************************************************************/
215
216/**
217 * Returns the item at the specified index.
218 *
219 * @param index The index of the item to retrieve.
220 *
221 * @exception IndexOutOfBoundsException If the index value is not valid.
222 */
223public String
224getItem(int index)
225{
226 return((String)items.elementAt(index));
227}
228
229/*************************************************************************/
230
231/**
232 * Returns the number of visible rows in the list.
233 *
234 * @return The number of visible rows in the list.
235 */
236public int
237getRows()
238{
239 return(rows);
240}
241
242/*************************************************************************/
243
244/**
245 * Tests whether or not multi-select mode is enabled.
246 *
247 * @return <code>true</code> if multi-select mode is enabled,
248 * <code>false</code> otherwise.
249 */
250public boolean
251isMultipleMode()
252{
253 return(multipleMode);
254}
255
256/*************************************************************************/
257
258/**
259 * Tests whether or not multi-select mode is enabled.
260 *
261 * @return <code>true</code> if multi-select mode is enabled,
262 * <code>false</code> otherwise.
263 *
264 * @deprecated This method is deprecated in favor of
265 * <code>isMultipleMode()</code>.
266 */
267public boolean
268allowsMultipleSelections()
269{
270 return(multipleMode);
271}
272
273/*************************************************************************/
274
275/**
276 * This method enables or disables multiple selection mode for this
277 * list.
278 *
279 * @param multipleMode <code>true</code> to enable multiple mode,
280 * <code>false</code> otherwise.
281 */
282public void
283setMultipleMode(boolean multipleMode)
284{
285 this.multipleMode = multipleMode;
286 if (peer != null)
287 {
288 ListPeer l = (ListPeer) peer;
289 l.setMultipleMode (multipleMode);
290 }
291}
292
293/*************************************************************************/
294
295/**
296 * This method enables or disables multiple selection mode for this
297 * list.
298 *
299 * @param multipleMode <code>true</code> to enable multiple mode,
300 * <code>false</code> otherwise.
301 */
302public void
303setMultipleSelections(boolean multipleMode)
304{
305 setMultipleMode(multipleMode);
306}
307
308/*************************************************************************/
309
310/**
311 * Returns the minimum size of this component.
312 *
313 * @return The minimum size of this component.
314 */
315public Dimension
316getMinimumSize()
317{
318 return(getMinimumSize(rows));
319}
320
321/*************************************************************************/
322
323/**
324 * Returns the minimum size of this component.
325 *
326 * @return The minimum size of this component.
327 *
328 * @deprecated This method is deprecated in favor of
329 * <code>getMinimumSize</code>.
330 */
331public Dimension
332minimumSize()
333{
334 return(getMinimumSize(rows));
335}
336
337/*************************************************************************/
338
339/**
340 * Returns the minimum size of this component assuming it had the specified
341 * number of rows.
342 *
343 * @param rows The number of rows to size for.
344 *
345 * @return The minimum size of this component.
346 */
347public Dimension
348getMinimumSize(int rows)
349{
350 ListPeer lp = (ListPeer)getPeer();
351 if (lp != null)
352 return(lp.minimumSize(rows));
353 else
354 return(new Dimension(0,0));
355}
356
357/*************************************************************************/
358
359/**
360 * Returns the minimum size of this component assuming it had the specified
361 * number of rows.
362 *
363 * @param rows The number of rows to size for.
364 *
365 * @return The minimum size of this component.
366 *
367 * @deprecated This method is deprecated in favor of
368 * <code>getMinimumSize(int)</code>>
369 */
370public Dimension
371minimumSize(int rows)
372{
373 return(getMinimumSize(rows));
374}
375
376/*************************************************************************/
377
378/**
379 * Returns the preferred size of this component.
380 *
381 * @return The preferred size of this component.
382 */
383public Dimension
384getPreferredSize()
385{
386 return(getPreferredSize(rows));
387}
388
389/*************************************************************************/
390
391/**
392 * Returns the preferred size of this component.
393 *
394 * @return The preferred size of this component.
395 *
396 * @deprecated This method is deprecated in favor of
397 * <code>getPreferredSize</code>.
398 */
399public Dimension
400preferredSize()
401{
402 return(getPreferredSize(rows));
403}
404
405/*************************************************************************/
406
407/**
408 * Returns the preferred size of this component assuming it had the specified
409 * number of rows.
410 *
411 * @param rows The number of rows to size for.
412 *
413 * @return The preferred size of this component.
414 */
415public Dimension
416getPreferredSize(int rows)
417{
418 ListPeer lp = (ListPeer)getPeer();
419 if (lp != null)
420 return(lp.preferredSize(rows));
421 else
422 return(new Dimension(0,0));
423}
424
425/*************************************************************************/
426
427/**
428 * Returns the preferred size of this component assuming it had the specified
429 * number of rows.
430 *
431 * @param rows The number of rows to size for.
432 *
433 * @return The preferred size of this component.
434 *
435 * @deprecated This method is deprecated in favor of
436 * <code>getPreferredSize(int)</code>>
437 */
438public Dimension
439preferredSize(int rows)
440{
441 return(getPreferredSize(rows));
442}
443
444/*************************************************************************/
445
446/**
447 * This method adds the specified item to the end of the list.
448 *
449 * @param item The item to add to the list.
450 */
451public void
452add(String item)
453{
454 add(item, -1);
455}
456
457/*************************************************************************/
458
459/**
460 * This method adds the specified item to the end of the list.
461 *
462 * @param item The item to add to the list.
463 *
464 * @deprecated Use add() instead.
465 */
466public void
467addItem(String item)
468{
469 addItem(item, -1);
470}
471
472/*************************************************************************/
473
474/**
475 * Adds the specified item to the specified location in the list.
476 * If the desired index is -1 or greater than the number of rows
477 * in the list, then the item is added to the end.
478 *
479 * @param item The item to add to the list.
480 * @param index The location in the list to add the item, or -1 to add
481 * to the end.
482 */
483public void
484add(String item, int index)
485{
486 if ((index == -1) || (index >= items.size()))
487 items.addElement(item);
488 else
489 items.insertElementAt(item, index);
490
491 if (peer != null)
492 {
493 ListPeer l = (ListPeer) peer;
494 l.add (item, index);
495 }
496}
497
498/*************************************************************************/
499
500/**
501 * Adds the specified item to the specified location in the list.
502 * If the desired index is -1 or greater than the number of rows
503 * in the list, then the item is added to the end.
504 *
505 * @param item The item to add to the list.
506 * @param index The location in the list to add the item, or -1 to add
507 * to the end.
508 *
509 * @deprecated Use add() instead.
510 */
511public void
512addItem(String item, int index)
513{
514 add(item, index);
515}
516
517/*************************************************************************/
518
519/**
520 * Deletes the item at the specified index.
521 *
522 * @param index The index of the item to delete.
523 *
524 * @exception IllegalArgumentException If the index is not valid
525 */
526public void
527delItem(int index) throws IllegalArgumentException
528{
529 remove(index);
530}
531
532/*************************************************************************/
533
534/**
535 * Deletes the item at the specified index.
536 *
537 * @param index The index of the item to delete.
538 *
539 * @exception IllegalArgumentException If the index is not valid
540 */
541public void
542remove(int index) throws IllegalArgumentException
543{
544 items.removeElementAt (index);
545 if (peer != null)
546 {
547 ListPeer l = (ListPeer) peer;
548 l.delItems (index, index);
549 }
550}
551
552/*************************************************************************/
553
554/**
555 * Deletes all items in the specified index range.
556 *
557 * @param start The beginning index of the range to delete.
558 * @param end The ending index of the range to delete.
559 *
560 * @exception IllegalArgumentException If the indexes are not valid
561 *
562 * @deprecated This method is deprecated for some unknown reason.
563 */
564public synchronized void
565delItems(int start, int end) throws IllegalArgumentException
566{
567 if ((start < 0) || (start >= items.size()))
568 throw new IllegalArgumentException("Bad list start index value: " + start);
569
570 if ((start < 0) || (start >= items.size()))
571 throw new IllegalArgumentException("Bad list start index value: " + start);
572
573 if (start > end)
574 throw new IllegalArgumentException("Start is greater than end!");
575
576 // We must run the loop in reverse direction.
577 for (int i = end; i >= start; --i)
578 items.removeElementAt (i);
579 if (peer != null)
580 {
581 ListPeer l = (ListPeer) peer;
582 l.delItems (start, end);
583 }
584}
585
586/*************************************************************************/
587
588/**
589 * Deletes the first occurrence of the specified item from the list.
590 *
591 * @param item The item to delete.
592 *
593 * @exception IllegalArgumentException If the specified item does not exist.
594 */
595public synchronized void
596remove(String item) throws IllegalArgumentException
597{
598 int index = items.indexOf(item);
599 if (index == -1)
600 throw new IllegalArgumentException("List element to delete not found");
601
602 remove(index);
603}
604
605/*************************************************************************/
606
607/**
608 * Deletes all of the items from the list.
609 */
610public synchronized void
611removeAll()
612{
613 items.clear();
614 if (peer != null)
615 {
616 ListPeer l = (ListPeer) peer;
617 l.removeAll ();
618 }
619}
620
621/*************************************************************************/
622
623/**
624 * Deletes all of the items from the list.
625 *
626 * @deprecated This method is deprecated in favor of <code>removeAll()</code>.
627 */
628public void
629clear()
630{
631 removeAll();
632}
633
634/*************************************************************************/
635
636/**
637 * Replaces the item at the specified index with the specified item.
638 *
639 * @param item The new item value.
640 * @param index The index of the item to replace.
641 *
642 * @exception IllegalArgumentException If the index is not valid.
643 */
644public synchronized void
645replaceItem(String item, int index) throws IllegalArgumentException
646{
647 remove(index);
648 addItem(item, index);
649}
650
651/*************************************************************************/
652
653/**
654 * Returns the index of the currently selected item. -1 will be returned
655 * if there are no selected rows or if there are multiple selected rows.
656 *
657 * @return The index of the selected row.
658 */
659public synchronized int
660getSelectedIndex()
661{
662 if (peer != null)
663 {
664 ListPeer l = (ListPeer) peer;
665 selected = l.getSelectedIndexes ();
666 }
667
668 if (selected == null || selected.length > 1)
669 return -1;
670 return selected[0];
671}
672
673/*************************************************************************/
674
675/**
676 * Returns an array containing the indexes of the rows that are
677 * currently selected.
678 *
679 * @return A list of indexes of selected rows.
680 */
681public synchronized int[]
682getSelectedIndexes()
683{
684 if (peer != null)
685 {
686 ListPeer l = (ListPeer) peer;
687 selected = l.getSelectedIndexes ();
688 }
689 return selected;
690}
691
692/*************************************************************************/
693
694/**
695 * Returns the item that is currently selected, or <code>null</code> if there
696 * is no item selected. FIXME: What happens if multiple items selected?
697 *
698 * @return The selected item, or <code>null</code> if there is no
699 * selected item.
700 */
701public synchronized String
702getSelectedItem()
703{
704 int index = getSelectedIndex();
705 if (index == -1)
706 return(null);
707
708 return((String)items.elementAt(index));
709}
710
711/*************************************************************************/
712
713/**
714 * Returns the list of items that are currently selected in this list.
715 *
716 * @return The list of currently selected items.
717 */
718public synchronized String[]
719getSelectedItems()
720{
721 int[] indexes = getSelectedIndexes();
722 if (indexes == null)
723 return(new String[0]);
724
725 String[] retvals = new String[indexes.length];
726 if (retvals.length > 0)
727 for (int i = 0 ; i < retvals.length; i++)
728 retvals[i] = (String)items.elementAt(indexes[i]);
729
730 return(retvals);
731}
732
733/*************************************************************************/
734
735/**
736 * Returns the list of items that are currently selected in this list as
737 * an array of type <code>Object[]</code> instead of <code>String[]</code>.
738 *
739 * @return The list of currently selected items.
740 */
741public synchronized Object[]
742getSelectedObjects()
743{
744 int[] indexes = getSelectedIndexes();
745 if (indexes == null)
746 return(new Object[0]);
747
748 Object[] retvals = new Object[indexes.length];
749 if (retvals.length > 0)
750 for (int i = 0 ; i < retvals.length; i++)
751 retvals[i] = items.elementAt(indexes[i]);
752
753 return(retvals);
754}
755
756/*************************************************************************/
757
758/**
759 * Tests whether or not the specified index is selected.
760 *
761 * @param index The index to test.
762 *
763 * @return <code>true</code> if the index is selected, <code>false</code>
764 * otherwise.
765 */
766public boolean
767isIndexSelected(int index)
768{
769 int[] indexes = getSelectedIndexes();
770
771 for (int i = 0; i < indexes.length; i++)
772 if (indexes[i] == index)
773 return(true);
774
775 return(false);
776}
777
778/*************************************************************************/
779
780/**
781 * Tests whether or not the specified index is selected.
782 *
783 * @param index The index to test.
784 *
785 * @return <code>true</code> if the index is selected, <code>false</code>
786 * otherwise.
787 *
788 * @deprecated This method is deprecated in favor of
789 * <code>isIndexSelected(int)</code>.
790 */
791public boolean
792isSelected(int index)
793{
794 return(isIndexSelected(index));
795}
796
797/*************************************************************************/
798
799/**
800 * This method ensures that the item at the specified index is visible.
801 *
802 * @exception IllegalArgumentException If the specified index is out of
803 * range.
804 */
805public synchronized void
806makeVisible(int index) throws IllegalArgumentException
807{
808 if ((index < 0) || (index >= items.size()))
809 throw new IllegalArgumentException("Bad list index: " + index);
810
811 visibleIndex = index;
812 if (peer != null)
813 {
814 ListPeer l = (ListPeer) peer;
815 l.makeVisible (index);
816 }
817}
818
819/*************************************************************************/
820
821/**
822 * Returns the index of the last item that was made visible via the
823 * <code>makeVisible()</code> method.
824 *
825 * @return The index of the last item made visible via the
826 * <code>makeVisible()</code> method.
827 */
828public int
829getVisibleIndex()
830{
831 return(visibleIndex);
832}
833
834/*************************************************************************/
835
836/**
837 * Makes the item at the specified index selected.
838 *
839 * @param index The index of the item to select.
840 */
841public synchronized void
842select(int index)
843{
844 ListPeer lp = (ListPeer)getPeer();
845 if (lp != null)
846 lp.select(index);
847}
848
849/*************************************************************************/
850
851/**
852 * Makes the item at the specified index not selected.
853 *
854 * @param index The index of the item to unselect.
855 */
856public synchronized void
857deselect(int index)
858{
859 ListPeer lp = (ListPeer)getPeer();
860 if (lp != null)
861 lp.deselect(index);
862}
863
864/*************************************************************************/
865
866/**
867 * Notifies this object to create its native peer.
868 */
869public void
870addNotify()
871{
872 if (peer == null)
873 peer = getToolkit ().createList (this);
874 super.addNotify ();
875}
876
877/*************************************************************************/
878
879/**
880 * Notifies this object to destroy its native peer.
881 */
882public void
883removeNotify()
884{
885 super.removeNotify();
886}
887
888/*************************************************************************/
889
890/**
891 * Adds the specified <code>ActionListener</code> to the list of
892 * registered listeners for this object.
893 *
894 * @param listener The listener to add.
895 */
896public synchronized void
897addActionListener(ActionListener listener)
898{
899 action_listeners = AWTEventMulticaster.add(action_listeners, listener);
900}
901
902/*************************************************************************/
903
904/**
905 * Removes the specified <code>ActionListener</code> from the list of
906 * registers listeners for this object.
907 *
908 * @param listener The listener to remove.
909 */
910public synchronized void
911removeActionListener(ActionListener listener)
912{
913 action_listeners = AWTEventMulticaster.remove(action_listeners, listener);
914}
915
916/*************************************************************************/
917
918/**
919 * Adds the specified <code>ItemListener</code> to the list of
920 * registered listeners for this object.
921 *
922 * @param listener The listener to add.
923 */
924public synchronized void
925addItemListener(ItemListener listener)
926{
927 item_listeners = AWTEventMulticaster.add(item_listeners, listener);
928}
929
930/*************************************************************************/
931
932/**
933 * Removes the specified <code>ItemListener</code> from the list of
934 * registers listeners for this object.
935 *
936 * @param listener The listener to remove.
937 */
938public synchronized void
939removeItemListener(ItemListener listener)
940{
941 item_listeners = AWTEventMulticaster.remove(item_listeners, listener);
942}
943
944/*************************************************************************/
945
946/**
947 * Processes the specified event for this object. If the event is an
948 * instance of <code>ActionEvent</code> then the
949 * <code>processActionEvent()</code> method is called. Similarly, if the
950 * even is an instance of <code>ItemEvent</code> then the
951 * <code>processItemEvent()</code> method is called. Otherwise the
952 * superclass method is called to process this event.
953 *
954 * @param event The event to process.
955 */
956protected void
957processEvent(AWTEvent event)
958{
959 if (event instanceof ActionEvent)
960 processActionEvent((ActionEvent)event);
961 else if (event instanceof ItemEvent)
962 processItemEvent((ItemEvent)event);
963 else
964 super.processEvent(event);
965}
966
967/*************************************************************************/
968
969/**
970 * This method processes the specified event by dispatching it to any
971 * registered listeners. Note that this method will only get called if
972 * action events are enabled. This will happen automatically if any
973 * listeners are added, or it can be done "manually" by calling
974 * the <code>enableEvents()</code> method.
975 *
976 * @param event The event to process.
977 */
978protected void
979processActionEvent(ActionEvent event)
980{
981 if (action_listeners != null)
982 action_listeners.actionPerformed(event);
983}
984
985/*************************************************************************/
986
987/**
988 * This method processes the specified event by dispatching it to any
989 * registered listeners. Note that this method will only get called if
990 * item events are enabled. This will happen automatically if any
991 * listeners are added, or it can be done "manually" by calling
992 * the <code>enableEvents()</code> method.
993 *
994 * @param event The event to process.
995 */
996protected void
997processItemEvent(ItemEvent event)
998{
999 if (item_listeners != null)
1000 item_listeners.itemStateChanged(event);
1001}
1002
1003void
1004dispatchEventImpl(AWTEvent e)
1005{
1006 if (e.id <= ItemEvent.ITEM_LAST
1007 && e.id >= ItemEvent.ITEM_FIRST
1008 && (item_listeners != null
1009 || (eventMask & AWTEvent.ITEM_EVENT_MASK) != 0))
1010 processEvent(e);
1011 else if (e.id <= ActionEvent.ACTION_LAST
1012 && e.id >= ActionEvent.ACTION_FIRST
1013 && (action_listeners != null
1014 || (eventMask & AWTEvent.ACTION_EVENT_MASK) != 0))
1015 processEvent(e);
1016 else
1017 super.dispatchEventImpl(e);
1018}
1019
1020/*************************************************************************/
1021
1022/**
1023 * Returns a debugging string for this object.
1024 *
1025 * @return A debugging string for this object.
1026 */
1027protected String
1028paramString()
1029{
1030 return "multiple=" + multipleMode + ",rows=" + rows + super.paramString();
1031}
1032
1033} // class List
Note: See TracBrowser for help on using the repository browser.