Merge from mainline
[official-gcc.git] / libjava / classpath / javax / swing / JList.java
blobd0e1090b8c900597b3832ef5f73b74b02eb7d9cf
1 /* JList.java --
2 Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import java.awt.Color;
42 import java.awt.Component;
43 import java.awt.ComponentOrientation;
44 import java.awt.Cursor;
45 import java.awt.Dimension;
46 import java.awt.Font;
47 import java.awt.FontMetrics;
48 import java.awt.Point;
49 import java.awt.Rectangle;
50 import java.awt.event.FocusListener;
51 import java.beans.PropertyChangeEvent;
52 import java.beans.PropertyChangeListener;
53 import java.util.Locale;
54 import java.util.Vector;
56 import javax.accessibility.Accessible;
57 import javax.accessibility.AccessibleComponent;
58 import javax.accessibility.AccessibleContext;
59 import javax.accessibility.AccessibleRole;
60 import javax.accessibility.AccessibleSelection;
61 import javax.accessibility.AccessibleState;
62 import javax.accessibility.AccessibleStateSet;
63 import javax.swing.event.ListDataEvent;
64 import javax.swing.event.ListDataListener;
65 import javax.swing.event.ListSelectionEvent;
66 import javax.swing.event.ListSelectionListener;
67 import javax.swing.plaf.ListUI;
68 import javax.swing.text.Position;
70 /**
71 * <p>This class is a facade over three separate objects: {@link
72 * javax.swing.ListModel}, {@link javax.swing.ListSelectionModel} and
73 * {@link javax.swing.plaf.ListUI}. The facade represents a unified "list"
74 * concept, with independently replacable (possibly client-provided) models
75 * for its contents and its current selection. In addition, each element in
76 * the list is rendered via a strategy class {@link
77 * javax.swing.ListCellRenderer}.</p>
79 * <p>Lists have many properties, some of which are stored in this class
80 * while others are delegated to the list's model or selection. The
81 * following properties are available:</p>
83 * <table>
84 * <tr><th>Property </th><th>Stored in</th><th>Bound?</th></tr>
85 * <tr><td>accessibleContext </td><td>list </td><td>no </td></tr>
86 * <tr><td>anchorSelectionIndex </td><td>selection</td><td>no </td></tr>
87 * <tr><td>cellRenderer </td><td>list </td><td>yes </td></tr>
88 * <tr><td>dragEnabled </td><td>list </td><td>no </td></tr>
89 * <tr><td>firstVisibleIndex </td><td>list </td><td>no </td></tr>
90 * <tr><td>fixedCellHeight </td><td>list </td><td>yes </td></tr>
91 * <tr><td>fixedCellWidth </td><td>list </td><td>yes </td></tr>
92 * <tr><td>lastVisibleIndex </td><td>list </td><td>no </td></tr>
93 * <tr><td>layoutOrientation </td><td>list </td><td>yes </td></tr>
94 * <tr><td>leadSelectionIndex </td><td>selection</td><td>no </td></tr>
95 * <tr><td>maxSelectionIndex </td><td>selection</td><td>no </td></tr>
96 * <tr><td>minSelectionIndex </td><td>selection</td><td>no </td></tr>
97 * <tr><td>model </td><td>list </td><td>yes </td></tr>
98 * <tr><td>opaque </td><td>list </td><td>no </td></tr>
99 * <tr><td>preferredScrollableViewportSize</td><td>list </td><td>no </td></tr>
100 * <tr><td>prototypeCellValue </td><td>list </td><td>yes </td></tr>
101 * <tr><td>scrollableTracksViewportHeight </td><td>list </td><td>no </td></tr>
102 * <tr><td>scrollableTracksViewportWidth </td><td>list </td><td>no </td></tr>
103 * <tr><td>selectedIndex </td><td>selection</td><td>no </td></tr>
104 * <tr><td>selectedIndices </td><td>selection</td><td>no </td></tr>
105 * <tr><td>selectedValue </td><td>model </td><td>no </td></tr>
106 * <tr><td>selectedValues </td><td>model </td><td>no </td></tr>
107 * <tr><td>selectionBackground </td><td>list </td><td>yes </td></tr>
108 * <tr><td>selectionEmpty </td><td>selection</td><td>no </td></tr>
109 * <tr><td>selectionForeground </td><td>list </td><td>yes </td></tr>
110 * <tr><td>selectionMode </td><td>selection</td><td>no </td></tr>
111 * <tr><td>selectionModel </td><td>list </td><td>yes </td></tr>
112 * <tr><td>UI </td><td>list </td><td>yes </td></tr>
113 * <tr><td>UIClassID </td><td>list </td><td>no </td></tr>
114 * <tr><td>valueIsAdjusting </td><td>list </td><td>no </td></tr>
115 * <tr><td>visibleRowCount </td><td>list </td><td>no </td></tr>
116 * </table>
118 * @author Graydon Hoare (graydon@redhat.com)
121 public class JList extends JComponent implements Accessible, Scrollable
125 * Provides accessibility support for <code>JList</code>.
127 protected class AccessibleJList extends AccessibleJComponent
128 implements AccessibleSelection, PropertyChangeListener,
129 ListSelectionListener, ListDataListener
133 * Provides accessibility support for list elements in <code>JList</code>s.
135 protected class AccessibleJListChild extends AccessibleContext
136 implements Accessible, AccessibleComponent
140 * The parent list.
142 JList parent;
145 * The index in the list for that child.
147 int listIndex;
150 * The cursor for this list child.
152 // TODO: Testcases show that this class somehow stores state about the
153 // cursor. I cannot make up though how that could affect
154 // the actual list.
155 Cursor cursor = Cursor.getDefaultCursor();
158 * Creates a new instance of <code>AccessibleJListChild</code>.
160 * @param list the list of which this is an accessible child
161 * @param index the list index for this child
163 public AccessibleJListChild(JList list, int index)
165 parent = list;
166 listIndex = index;
170 * Returns the accessible context of this object. Returns
171 * <code>this</code> since <code>AccessibleJListChild</code>s are their
172 * own accessible contexts.
174 * @return the accessible context of this object, <code>this</code>
176 public AccessibleContext getAccessibleContext()
178 return this;
182 * Returns the background color for this list child. This returns the
183 * background of the <code>JList</code> itself since the background
184 * cannot be set on list children individually
186 * @return the background color for this list child
188 public Color getBackground()
190 return parent.getBackground();
194 * Calling this method has no effect, since the background color cannot be
195 * set on list children individually.
197 * @param color not used here.
199 public void setBackground(Color color)
201 // Calling this method has no effect, since the background color cannot
202 // be set on list children individually.
206 * Returns the foreground color for this list child. This returns the
207 * background of the <code>JList</code> itself since the foreground
208 * cannot be set on list children individually.
210 * @return the background color for this list child
212 public Color getForeground()
214 return parent.getForeground();
218 * Calling this method has no effect, since the foreground color cannot be
219 * set on list children individually.
221 * @param color not used here.
223 public void setForeground(Color color)
225 // Calling this method has no effect, since the foreground color cannot
226 // be set on list children individually.
230 * Returns the cursor for this list child.
232 * @return the cursor for this list child
234 public Cursor getCursor()
236 // TODO: Testcases show that this method returns the cursor that has
237 // been set by setCursor. I cannot make up though how that could affect
238 // the actual list.
239 return cursor;
243 * Sets the cursor for this list child.
245 public void setCursor(Cursor cursor)
247 this.cursor = cursor;
248 // TODO: Testcases show that this method returns the cursor that has
249 // been set by setCursor. I cannot make up though how that could affect
250 // the actual list.
254 * Returns the font of the <code>JList</code> since it is not possible to
255 * set fonts for list children individually.
257 * @return the font of the <code>JList</code>
259 public Font getFont()
261 return parent.getFont();
265 * Does nothing since it is not possible to set the font on list children
266 * individually.
268 * @param font not used here
270 public void setFont(Font font)
272 // Does nothing since it is not possible to set the font on list
273 // children individually.
277 * Returns the font metrics for the specified font. This method forwards
278 * to the parent <code>JList</code>.
280 * @param font the font for which the font metrics is queried
282 * @return the font metrics for the specified font
284 public FontMetrics getFontMetrics(Font font)
286 return parent.getFontMetrics(font);
290 * Returns <code>true</code> if the parent <code>JList</code> is enabled,
291 * <code>false</code> otherwise. The list children cannot have an enabled
292 * flag set individually.
294 * @return <code>true</code> if the parent <code>JList</code> is enabled,
295 * <code>false</code> otherwise
297 public boolean isEnabled()
299 return parent.isEnabled();
303 * Does nothing since the enabled flag cannot be set for list children
304 * individually.
306 * @param b not used here
308 public void setEnabled(boolean b)
310 // Does nothing since the enabled flag cannot be set for list children
311 // individually.
315 * Returns <code>true</code> if this list child is visible,
316 * <code>false</code> otherwise. The value of this property depends
317 * on {@link JList#getFirstVisibleIndex()} and
318 * {@link JList#getLastVisibleIndex()}.
320 * @return <code>true</code> if this list child is visible,
321 * <code>false</code> otherwise
323 public boolean isVisible()
325 return listIndex >= parent.getFirstVisibleIndex()
326 && listIndex <= parent.getLastVisibleIndex();
330 * The value of the visible property cannot be modified, so this method
331 * does nothing.
333 * @param b not used here
335 public void setVisible(boolean b)
337 // The value of the visible property cannot be modified, so this method
338 // does nothing.
342 * Returns <code>true</code> if this list child is currently showing on
343 * screen and <code>false</code> otherwise. The list child is showing if
344 * it is visible and if it's parent JList is currently showing.
346 * @return <code>true</code> if this list child is currently showing on
347 * screen and <code>false</code> otherwise
349 public boolean isShowing()
351 return isVisible() && parent.isShowing();
355 * Returns <code>true</code> if this list child covers the screen location
356 * <code>point</code> (relative to the <code>JList</code> coordinate
357 * system, <code>false</code> otherwise.
359 * @return <code>true</code> if this list child covers the screen location
360 * <code>point</code> , <code>false</code> otherwise
362 public boolean contains(Point point)
364 return getBounds().contains(point);
368 * Returns the absolute screen location of this list child.
370 * @return the absolute screen location of this list child
372 public Point getLocationOnScreen()
374 Point loc = getLocation();
375 SwingUtilities.convertPointToScreen(loc, parent);
376 return loc;
380 * Returns the screen location of this list child relative to it's parent.
382 * @return the location of this list child relative to it's parent
384 * @see JList#indexToLocation(int)
386 public Point getLocation()
388 return parent.indexToLocation(listIndex);
392 * Does nothing since the screen location cannot be set on list children
393 * explictitly.
395 * @param point not used here
397 public void setLocation(Point point)
399 // Does nothing since the screen location cannot be set on list children
400 // explictitly.
404 * Returns the bounds of this list child.
406 * @return the bounds of this list child
408 * @see JList#getCellBounds(int, int)
410 public Rectangle getBounds()
412 return parent.getCellBounds(listIndex, listIndex);
416 * Does nothing since the bounds cannot be set on list children
417 * individually.
419 * @param rectangle not used here
421 public void setBounds(Rectangle rectangle)
423 // Does nothing since the bounds cannot be set on list children
424 // individually.
428 * Returns the size of this list child.
430 * @return the size of this list child
432 public Dimension getSize()
434 Rectangle b = getBounds();
435 return b.getSize();
439 * Does nothing since the size cannot be set on list children
440 * individually.
442 * @param dimension not used here
444 public void setSize(Dimension dimension)
446 // Does nothing since the size cannot be set on list children
447 // individually.
451 * Returns <code>null</code> because list children do not have children
452 * themselves
454 * @return <code>null</code>
456 public Accessible getAccessibleAt(Point point)
458 return null;
462 * Returns <code>true</code> since list children are focus traversable.
464 * @return true
466 public boolean isFocusTraversable()
468 // TODO: Is this 100% ok?
469 return true;
473 * Requests focus on the parent list. List children cannot request focus
474 * individually.
476 public void requestFocus()
478 // TODO: Is this 100% ok?
479 parent.requestFocus();
483 * Adds a focus listener to the parent list. List children do not have
484 * their own focus management.
486 * @param listener the focus listener to add
488 public void addFocusListener(FocusListener listener)
490 // TODO: Is this 100% ok?
491 parent.addFocusListener(listener);
495 * Removes a focus listener from the parent list. List children do not
496 * have their own focus management.
498 * @param listener the focus listener to remove
500 public void removeFocusListener(FocusListener listener)
502 // TODO: Is this 100%
503 parent.removeFocusListener(listener);
507 * Returns the accessible role of this list item, which is
508 * {@link AccessibleRole#LABEL}.
510 * @return {@link AccessibleRole#LABEL}
512 public AccessibleRole getAccessibleRole()
514 return AccessibleRole.LABEL;
518 * Returns the accessible state set of this list item.
520 * @return the accessible state set of this list item
522 public AccessibleStateSet getAccessibleStateSet()
524 AccessibleStateSet states = new AccessibleStateSet();
525 if (isVisible())
526 states.add(AccessibleState.VISIBLE);
527 if (isShowing())
528 states.add(AccessibleState.SHOWING);
529 if (isFocusTraversable())
530 states.add(AccessibleState.FOCUSABLE);
531 // TODO: How should the active state be handled? The API docs
532 // suggest that this state is set on the activated list child,
533 // that is the one that is drawn with a box. However, I don't know how
534 // to implement this.
536 // TODO: We set the selectable state here because list children are
537 // selectable. Is there a way to disable single children?
538 if (parent.isEnabled())
539 states.add(AccessibleState.SELECTABLE);
541 if (parent.isSelectedIndex(listIndex))
542 states.add(AccessibleState.SELECTED);
544 // TODO: Handle more states here?
545 return states;
549 * Returns the index of this list child within it's parent list.
551 * @return the index of this list child within it's parent list
553 public int getAccessibleIndexInParent()
555 return listIndex;
559 * Returns <code>0</code> since list children don't have children
560 * themselves.
562 * @return <code>0</code>
564 public int getAccessibleChildrenCount()
566 return 0;
570 * Returns <code>null</code> since list children don't have children
571 * themselves.
573 * @return <code>null</code>
575 public Accessible getAccessibleChild(int i)
577 return null;
581 * Returns the locale of this component. This call is forwarded to the
582 * parent list since list children don't have a separate locale setting.
584 * @return the locale of this component
586 public Locale getLocale()
588 return parent.getLocale();
592 * This method does
593 * nothing, list children are transient accessible objects which means
594 * that they don't fire property change events.
596 * @param l not used here
598 public void addPropertyChangeListener(PropertyChangeListener l)
600 // Do nothing here.
604 * This method does
605 * nothing, list children are transient accessible objects which means
606 * that they don't fire property change events.
608 * @param l not used here
610 public void removePropertyChangeListener(PropertyChangeListener l)
612 // Do nothing here.
615 // TODO: Implement the remaining methods of this class.
619 * Create a new AccessibleJList.
621 public AccessibleJList()
623 // Nothing to do here.
627 * Returns the number of selected accessible children.
629 * @return the number of selected accessible children
631 public int getAccessibleSelectionCount()
633 return getSelectedIndices().length;
637 * Returns the n-th selected accessible child.
639 * @param n the index of the selected child to return
641 * @return the n-th selected accessible child
643 public Accessible getAccessibleSelection(int n)
645 return new AccessibleJListChild(JList.this, getSelectedIndices()[n]);
649 * Returns <code>true</code> if the n-th child is selected,
650 * <code>false</code> otherwise.
652 * @param n the index of the child of which the selected state is queried
654 * @return <code>true</code> if the n-th child is selected,
655 * <code>false</code> otherwise
657 public boolean isAccessibleChildSelected(int n)
659 return isSelectedIndex(n);
663 * Adds the accessible item with the specified index to the selected items.
664 * If multiple selections are supported, the item is added to the selection,
665 * otherwise the item replaces the current selection.
667 * @param i the index of the item to add to the selection
669 public void addAccessibleSelection(int i)
671 addSelectionInterval(i, i);
675 * Removes the accessible item with the specified index to the selection.
677 * @param i the index of the item to be removed from the selection
679 public void removeAccessibleSelection(int i)
681 removeSelectionInterval(i, i);
685 * Remove all selection items from the selection.
687 public void clearAccessibleSelection()
689 clearSelection();
693 * Selects all items if multiple selections are supported.
694 * Otherwise do nothing.
696 public void selectAllAccessibleSelection()
698 addSelectionInterval(0, getModel().getSize());
702 * Receices notification when the list selection is changed. This method
703 * fires two property change events, the first with
704 * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY} and the second
705 * with {@link AccessibleContext#ACCESSIBLE_SELECTION_PROPERTY}.
707 * @param event the list selection event
709 public void valueChanged(ListSelectionEvent event)
711 firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
712 Boolean.TRUE);
713 firePropertyChange(ACCESSIBLE_SELECTION_PROPERTY, Boolean.FALSE,
714 Boolean.TRUE);
718 * Receives notification when items have changed in the
719 * <code>JList</code>. This method fires a property change event with
720 * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
722 * @param event the list data event
724 public void contentsChanged(ListDataEvent event)
726 firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
727 Boolean.TRUE);
731 * Receives notification when items are inserted into the
732 * <code>JList</code>. This method fires a property change event with
733 * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
735 * @param event the list data event
737 public void intervalAdded(ListDataEvent event)
739 firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
740 Boolean.TRUE);
744 * Receives notification when items are removed from the
745 * <code>JList</code>. This method fires a property change event with
746 * {@link AccessibleContext#ACCESSIBLE_VISIBLE_DATA_PROPERTY}.
748 * @param event the list data event
750 public void intervalRemoved(ListDataEvent event)
752 firePropertyChange(ACCESSIBLE_VISIBLE_DATA_PROPERTY, Boolean.FALSE,
753 Boolean.TRUE);
758 * Receives notification about changes of the <code>JList</code>'s
759 * properties. This is used to re-register this object as listener to
760 * the data model and selection model when the data model or selection model
761 * changes.
763 * @param e the property change event
765 public void propertyChange(PropertyChangeEvent e)
767 String propertyName = e.getPropertyName();
768 if (propertyName.equals("model"))
770 ListModel oldModel = (ListModel) e.getOldValue();
771 oldModel.removeListDataListener(this);
772 ListModel newModel = (ListModel) e.getNewValue();
773 newModel.addListDataListener(this);
775 else if (propertyName.equals("selectionModel"))
777 ListSelectionModel oldModel = (ListSelectionModel) e.getOldValue();
778 oldModel.removeListSelectionListener(this);
779 ListSelectionModel newModel = (ListSelectionModel) e.getNewValue();
780 oldModel.addListSelectionListener(this);
785 * Return the state set of the <code>JList</code>.
787 * @return the state set of the <code>JList</code>
789 public AccessibleStateSet getAccessibleStateSet()
791 // TODO: Figure out if there is possibly more state that must be
792 // handled here.
793 AccessibleStateSet s = super.getAccessibleStateSet();
794 if (getSelectionMode() != ListSelectionModel.SINGLE_SELECTION)
795 s.add(AccessibleState.MULTISELECTABLE);
796 return s;
800 * Returns the accessible role for <code>JList</code>,
801 * {@link AccessibleRole#LIST}.
803 * @return the accessible role for <code>JList</code>
805 public AccessibleRole getAccessibleRole()
807 return AccessibleRole.LIST;
811 * Returns the accessible child at the visual location <code>p</code>
812 * (relative to the upper left corner of the <code>JList</code>). If there
813 * is no child at that location, this returns <code>null</code>.
815 * @param p the screen location for which to return the accessible child
817 * @return the accessible child at the specified location, or
818 * <code>null</code> if there is no child at that location
820 public Accessible getAccessibleAt(Point p)
822 int childIndex = locationToIndex(p);
823 return getAccessibleChild(childIndex);
827 * Returns the number of accessible children in the <code>JList</code>.
829 * @return the number of accessible children in the <code>JList</code>
831 public int getAccessibleChildrenCount()
833 return getModel().getSize();
837 * Returns the n-th accessible child of this <code>JList</code>. This will
838 * be an instance of {@link AccessibleJListChild}. If there is no child
839 * at that index, <code>null</code> is returned.
841 * @param n the index of the child to return
843 * @return the n-th accessible child of this <code>JList</code>
845 public Accessible getAccessibleChild(int n)
847 if (getModel().getSize() <= n)
848 return null;
849 return new AccessibleJListChild(JList.this, n);
853 private static final long serialVersionUID = 4406629526391098046L;
855 /**
856 * Constant value used in "layoutOrientation" property. This value means
857 * that cells are laid out in a single vertical column. This is the default.
859 public static final int VERTICAL = 0;
861 /**
862 * Constant value used in "layoutOrientation" property. This value means
863 * that cells are laid out in multiple columns "newspaper style", filling
864 * vertically first, then horizontally.
866 public static final int VERTICAL_WRAP = 1;
868 /**
869 * Constant value used in "layoutOrientation" property. This value means
870 * that cells are laid out in multiple columns "newspaper style",
871 * filling horizontally first, then vertically.
873 public static final int HORIZONTAL_WRAP = 2;
876 * This property indicates whether "drag and drop" functions are enabled
877 * on the list.
879 boolean dragEnabled;
881 /** This property provides a strategy for rendering cells in the list. */
882 ListCellRenderer cellRenderer;
885 * This property indicates an fixed width to assign to all cells in the
886 * list. If its value is <code>-1</code>, no width has been
887 * assigned. This value can be set explicitly, or implicitly by setting
888 * the {@link #prototypeCellValue} property.
890 int fixedCellWidth;
893 * This property indicates an fixed height to assign to all cells in the
894 * list. If its value is <code>-1</code>, no height has been
895 * assigned. This value can be set explicitly, or implicitly by setting
896 * the {@link #prototypeCellValue} property.
898 int fixedCellHeight;
900 /**
901 * This property holds the current layout orientation of the list, which
902 * is one of the integer constants {@link #VERTICAL}, {@link
903 * #VERTICAL_WRAP}, or {@link #HORIZONTAL_WRAP}.
905 int layoutOrientation;
907 /** This property holds the data elements displayed by the list. */
908 ListModel model;
911 * <p>This property holds a reference to a "prototype" data value --
912 * typically a String -- which is used to calculate the {@link
913 * #fixedCellWidth} and {@link #fixedCellHeight} properties, using the
914 * {@link #cellRenderer} property to acquire a component to render the
915 * prototype.</p>
917 * <p>It is important that you <em>not</em> set this value to a
918 * component. It has to be a <em>data value</em> such as the objects you
919 * would find in the list's model. Setting it to a component will have
920 * undefined (and undesirable) affects. </p>
922 Object prototypeCellValue;
924 /**
925 * This property specifies a foreground color for the selected cells in
926 * the list. When {@link ListCellRenderer#getListCellRendererComponent}
927 * is called with a selected cell object, the component returned will
928 * have its "foreground" set to this color.
930 Color selectionBackground;
932 /**
933 * This property specifies a background color for the selected cells in
934 * the list. When {@link ListCellRenderer#getListCellRendererComponent}
935 * is called with a selected cell object, the component returned will
936 * have its "background" property set to this color.
938 Color selectionForeground;
940 /**
941 * This property holds a description of which data elements in the {@link
942 * #model} property should be considered "selected", when displaying and
943 * interacting with the list.
945 ListSelectionModel selectionModel;
949 * This property indicates that the list's selection is currently
950 * "adjusting" -- perhaps due to a user actively dragging the mouse over
951 * multiple list elements -- and is therefore likely to change again in
952 * the near future. A {@link ListSelectionListener} might choose to delay
953 * updating its view of the list's selection until this property is
954 * false, meaning that the adjustment has completed.
956 boolean valueIsAdjusting;
958 /**
959 * This property indicates a <em>preference</em> for the number of rows
960 * displayed in the list, and will scale the
961 * {@link #getPreferredScrollableViewportSize} property accordingly. The actual
962 * number of displayed rows, when the list is placed in a real {@link
963 * JViewport} or other component, may be greater or less than this number.
965 int visibleRowCount;
968 * Fire a {@link ListSelectionEvent} to all the registered ListSelectionListeners.
970 protected void fireSelectionValueChanged(int firstIndex, int lastIndex, boolean isAdjusting)
972 ListSelectionEvent evt = new ListSelectionEvent(this, firstIndex, lastIndex, isAdjusting);
973 ListSelectionListener listeners[] = getListSelectionListeners();
974 for (int i = 0; i < listeners.length; ++i)
976 listeners[i].valueChanged(evt);
981 * This private listener propagates {@link ListSelectionEvent} events
982 * from the list's "selectionModel" property to the list's {@link
983 * ListSelectionListener} listeners. It also listens to {@link
984 * ListDataEvent} events from the list's {@link #model} property. If this
985 * class receives either type of event, it triggers repainting of the
986 * list.
988 private class ListListener
989 implements ListSelectionListener, ListDataListener
991 // ListDataListener events
992 public void contentsChanged(ListDataEvent event)
994 JList.this.revalidate();
995 JList.this.repaint();
997 public void intervalAdded(ListDataEvent event)
999 JList.this.revalidate();
1000 JList.this.repaint();
1002 public void intervalRemoved(ListDataEvent event)
1004 JList.this.revalidate();
1005 JList.this.repaint();
1007 // ListSelectionListener events
1008 public void valueChanged(ListSelectionEvent event)
1010 JList.this.fireSelectionValueChanged(event.getFirstIndex(),
1011 event.getLastIndex(),
1012 event.getValueIsAdjusting());
1013 JList.this.repaint();
1017 /**
1018 * Shared ListListener instance, subscribed to both the current {@link
1019 * #model} and {@link #selectionModel} properties of the list.
1021 ListListener listListener;
1025 * Creates a new JList object.
1027 public JList()
1029 init();
1033 * Creates a new JList object.
1035 * @param listData Initial data to populate the list with
1037 public JList(Object[] listData)
1039 init();
1040 setListData(listData);
1044 * Creates a new JList object.
1046 * @param listData Initial data to populate the list with
1048 public JList(Vector listData)
1050 init();
1051 setListData(listData);
1055 * Creates a new JList object.
1057 * @param listData Initial data to populate the list with
1059 public JList(ListModel listData)
1061 init();
1062 setModel(listData);
1065 void init()
1067 dragEnabled = false;
1068 fixedCellHeight = -1;
1069 fixedCellWidth = -1;
1070 layoutOrientation = VERTICAL;
1071 opaque = true;
1072 valueIsAdjusting = false;
1073 visibleRowCount = 7;
1075 cellRenderer = new DefaultListCellRenderer();
1076 listListener = new ListListener();
1078 setModel(new DefaultListModel());
1079 setSelectionModel(createSelectionModel());
1080 setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
1081 setLayout(null);
1083 updateUI();
1087 * Creates the default <code>ListSelectionModel</code>.
1089 * @return the <code>ListSelectionModel</code>
1091 protected ListSelectionModel createSelectionModel()
1093 return new DefaultListSelectionModel();
1097 * Gets the value of the {@link #fixedCellHeight} property. This property
1098 * may be <code>-1</code> to indicate that no cell height has been
1099 * set. This property is also set implicitly when the
1100 * {@link #prototypeCellValue} property is set.
1102 * @return The current value of the property
1104 * @see #fixedCellHeight
1105 * @see #setFixedCellHeight
1106 * @see #setPrototypeCellValue
1108 public int getFixedCellHeight()
1110 return fixedCellHeight;
1114 * Sets the value of the {@link #fixedCellHeight} property. This property
1115 * may be <code>-1</code> to indicate that no cell height has been
1116 * set. This property is also set implicitly when the {@link
1117 * #prototypeCellValue} property is set, but setting it explicitly
1118 * overrides the height computed from {@link #prototypeCellValue}.
1120 * @see #getFixedCellHeight
1121 * @see #getPrototypeCellValue
1123 public void setFixedCellHeight(int h)
1125 if (fixedCellHeight == h)
1126 return;
1128 int old = fixedCellHeight;
1129 fixedCellHeight = h;
1130 firePropertyChange("fixedCellWidth", old, h);
1135 * Gets the value of the {@link #fixedCellWidth} property. This property
1136 * may be <code>-1</code> to indicate that no cell width has been
1137 * set. This property is also set implicitly when the {@link
1138 * #prototypeCellValue} property is set.
1140 * @return The current value of the property
1142 * @see #setFixedCellWidth
1143 * @see #setPrototypeCellValue
1145 public int getFixedCellWidth()
1147 return fixedCellWidth;
1151 * Sets the value of the {@link #fixedCellWidth} property. This property
1152 * may be <code>-1</code> to indicate that no cell width has been
1153 * set. This property is also set implicitly when the {@link
1154 * #prototypeCellValue} property is set, but setting it explicitly
1155 * overrides the width computed from {@link #prototypeCellValue}.
1157 * @see #getFixedCellHeight
1158 * @see #getPrototypeCellValue
1160 public void setFixedCellWidth(int w)
1162 if (fixedCellWidth == w)
1163 return;
1165 int old = fixedCellWidth;
1166 fixedCellWidth = w;
1167 firePropertyChange("fixedCellWidth", old, w);
1170 /**
1171 * Gets the value of the {@link #visibleRowCount} property.
1173 * @return the current value of the property.
1176 public int getVisibleRowCount()
1178 return visibleRowCount;
1182 * Sets the value of the {@link #visibleRowCount} property.
1184 * @param vc The new property value
1186 public void setVisibleRowCount(int vc)
1188 visibleRowCount = vc;
1189 revalidate();
1190 repaint();
1194 * Adds a {@link ListSelectionListener} to the listener list for this
1195 * list. The listener will be called back with a {@link
1196 * ListSelectionEvent} any time the list's {@link #selectionModel}
1197 * property changes. The source of such events will be the JList,
1198 * not the selection model.
1200 * @param listener The new listener to add
1202 public void addListSelectionListener(ListSelectionListener listener)
1204 listenerList.add (ListSelectionListener.class, listener);
1208 * Removes a {@link ListSelectionListener} from the listener list for
1209 * this list. The listener will no longer be called when the list's
1210 * {@link #selectionModel} changes.
1212 * @param listener The listener to remove
1214 public void removeListSelectionListener(ListSelectionListener listener)
1216 listenerList.remove(ListSelectionListener.class, listener);
1220 * Returns an array of all ListSelectionListeners subscribed to this
1221 * list.
1223 * @return The current subscribed listeners
1225 * @since 1.4
1227 public ListSelectionListener[] getListSelectionListeners()
1229 return (ListSelectionListener[]) getListeners(ListSelectionListener.class);
1232 public int getSelectionMode()
1234 return selectionModel.getSelectionMode();
1238 * Sets the list's "selectionMode" property, which simply mirrors the
1239 * same property on the list's {@link #selectionModel} property. This
1240 * property should be one of the integer constants
1241 * <code>SINGLE_SELECTION</code>, <code>SINGLE_INTERVAL_SELECTION</code>,
1242 * or <code>MULTIPLE_INTERVAL_SELECTION</code> from the {@link
1243 * ListSelectionModel} interface.
1245 * @param a The new selection mode
1247 public void setSelectionMode(int a)
1249 selectionModel.setSelectionMode(a);
1253 * Adds the interval <code>[a,a]</code> to the set of selections managed
1254 * by this list's {@link #selectionModel} property. Depending on the
1255 * selection mode, this may cause existing selections to become invalid,
1256 * or may simply expand the set of selections.
1258 * @param a A number in the half-open range <code>[0, x)</code> where
1259 * <code>x = getModel.getSize()</code>, indicating the index of an
1260 * element in the list to select. When &lt; 0 the selection is cleared.
1262 * @see #setSelectionMode
1263 * @see #selectionModel
1265 public void setSelectedIndex(int a)
1267 if (a < 0)
1268 selectionModel.clearSelection();
1269 else
1270 selectionModel.setSelectionInterval(a, a);
1274 * For each element <code>a[i]</code> of the provided array
1275 * <code>a</code>, calls {@link #setSelectedIndex} on <code>a[i]</code>.
1277 * @see #setSelectionMode
1278 * @see #selectionModel
1280 public void setSelectedIndices(int [] a)
1282 for (int i = 0; i < a.length; ++i)
1283 setSelectedIndex(a[i]);
1287 * Returns the minimum index of an element in the list which is currently
1288 * selected.
1290 * @return A number in the half-open range <code>[0, x)</code> where
1291 * <code>x = getModel.getSize()</code>, indicating the minimum index of
1292 * an element in the list for which the element is selected, or
1293 * <code>-1</code> if no elements are selected
1295 public int getSelectedIndex()
1297 return selectionModel.getMinSelectionIndex();
1301 * Returns <code>true</code> if the model's selection is empty, otherwise
1302 * <code>false</code>.
1304 * @return The return value of {@link ListSelectionModel#isSelectionEmpty}
1306 public boolean isSelectionEmpty()
1308 return selectionModel.isSelectionEmpty();
1312 * Returns the list index of the upper left or upper right corner of the
1313 * visible rectangle of this list, depending on the {@link
1314 * Component#getComponentOrientation} property.
1316 * @return The index of the first visible list cell, or <code>-1</code>
1317 * if none is visible.
1319 public int getFirstVisibleIndex()
1321 ComponentOrientation or = getComponentOrientation();
1322 Rectangle r = getVisibleRect();
1323 if (or == ComponentOrientation.RIGHT_TO_LEFT)
1324 r.translate((int) r.getWidth() - 1, 0);
1325 return getUI().locationToIndex(this, r.getLocation());
1330 * Returns index of the cell to which specified location is closest to. If
1331 * the location is outside the bounds of the list, then the greatest index
1332 * in the list model is returned. If the list model is empty, then
1333 * <code>-1</code> is returned.
1335 * @param location for which to look for in the list
1337 * @return index of the cell to which specified location is closest to.
1339 public int locationToIndex(Point location)
1341 return getUI().locationToIndex(this, location);
1345 * Returns location of the cell located at the specified index in the list.
1346 * @param index of the cell for which location will be determined
1348 * @return location of the cell located at the specified index in the list.
1350 public Point indexToLocation(int index)
1352 return getUI().indexToLocation(this, index);
1356 * Returns the list index of the lower right or lower left corner of the
1357 * visible rectangle of this list, depending on the {@link
1358 * Component#getComponentOrientation} property.
1360 * @return The index of the last visible list cell, or <code>-1</code>
1361 * if none is visible.
1363 public int getLastVisibleIndex()
1365 ComponentOrientation or = getComponentOrientation();
1366 Rectangle r = getVisibleRect();
1367 r.translate(0, (int) r.getHeight() - 1);
1368 if (or == ComponentOrientation.LEFT_TO_RIGHT)
1369 r.translate((int) r.getWidth() - 1, 0);
1370 if (getUI().locationToIndex(this, r.getLocation()) == -1
1371 && indexToLocation(getModel().getSize() - 1).y < r.y)
1372 return getModel().getSize() - 1;
1373 return getUI().locationToIndex(this, r.getLocation());
1377 * Returns the indices of values in the {@link #model} property which are
1378 * selected.
1380 * @return An array of model indices, each of which is selected according
1381 * to the {@link #getSelectedValues} property
1383 public int[] getSelectedIndices()
1385 int lo, hi, n, i, j;
1386 if (selectionModel.isSelectionEmpty())
1387 return new int[0];
1388 lo = selectionModel.getMinSelectionIndex();
1389 hi = selectionModel.getMaxSelectionIndex();
1390 n = 0;
1391 for (i = lo; i <= hi; ++i)
1392 if (selectionModel.isSelectedIndex(i))
1393 n++;
1394 int [] v = new int[n];
1395 j = 0;
1396 for (i = lo; i <= hi; ++i)
1397 if (selectionModel.isSelectedIndex(i))
1398 v[j++] = i;
1399 return v;
1403 * Indicates whether the list element at a given index value is
1404 * currently selected.
1406 * @param a The index to check
1407 * @return <code>true</code> if <code>a</code> is the index of a selected
1408 * list element
1410 public boolean isSelectedIndex(int a)
1412 return selectionModel.isSelectedIndex(a);
1416 * Returns the first value in the list's {@link #model} property which is
1417 * selected, according to the list's {@link #selectionModel} property.
1418 * This is equivalent to calling
1419 * <code>getModel()getElementAt(getSelectedIndex())</code>, with a check
1420 * for the special index value of <code>-1</code> which returns null
1421 * <code>null</code>.
1423 * @return The first selected element, or <code>null</code> if no element
1424 * is selected.
1426 * @see #getSelectedValues
1428 public Object getSelectedValue()
1430 int index = getSelectedIndex();
1431 if (index == -1)
1432 return null;
1433 return getModel().getElementAt(index);
1437 * Returns all the values in the list's {@link #model} property which
1438 * are selected, according to the list's {@link #selectionModel} property.
1440 * @return An array containing all the selected values
1442 * @see #setSelectedValue
1444 public Object[] getSelectedValues()
1446 int [] idx = getSelectedIndices();
1447 Object [] v = new Object[idx.length];
1448 for (int i = 0; i < idx.length; ++i)
1449 v[i] = getModel().getElementAt(i);
1450 return v;
1454 * Gets the value of the {@link #selectionBackground} property.
1456 * @return The current value of the property
1458 public Color getSelectionBackground()
1460 return selectionBackground;
1464 * Sets the value of the {@link #selectionBackground} property.
1466 * @param c The new value of the property
1468 public void setSelectionBackground(Color c)
1470 if (selectionBackground == c)
1471 return;
1473 Color old = selectionBackground;
1474 selectionBackground = c;
1475 firePropertyChange("selectionBackground", old, c);
1476 repaint();
1480 * Gets the value of the {@link #selectionForeground} property.
1482 * @return The current value of the property
1484 public Color getSelectionForeground()
1486 return selectionForeground;
1490 * Sets the value of the {@link #selectionForeground} property.
1492 * @param c The new value of the property
1494 public void setSelectionForeground(Color c)
1496 if (selectionForeground == c)
1497 return;
1499 Color old = selectionForeground;
1500 selectionForeground = c;
1501 firePropertyChange("selectionForeground", old, c);
1505 * Sets the selection to cover only the specified value, if it
1506 * exists in the model.
1508 * @param obj The object to select
1509 * @param scroll Whether to scroll the list to make the newly selected
1510 * value visible
1512 * @see #ensureIndexIsVisible
1515 public void setSelectedValue(Object obj, boolean scroll)
1517 for (int i = 0; i < model.getSize(); ++i)
1519 if (model.getElementAt(i).equals(obj))
1521 setSelectedIndex(i);
1522 if (scroll)
1523 ensureIndexIsVisible(i);
1524 break;
1530 * Scrolls this list to make the specified cell visible. This
1531 * only works if the list is contained within a viewport.
1533 * @param i The list index to make visible
1535 * @see JComponent#scrollRectToVisible
1537 public void ensureIndexIsVisible(int i)
1539 scrollRectToVisible(getUI().getCellBounds(this, i, i));
1543 * Sets the {@link #model} property of the list to a new anonymous
1544 * {@link AbstractListModel} subclass which accesses the provided Object
1545 * array directly.
1547 * @param listData The object array to build a new list model on
1548 * @see #setModel
1550 public void setListData(final Object[] listData)
1552 setModel(new AbstractListModel()
1554 public int getSize()
1556 return listData.length;
1559 public Object getElementAt(int i)
1561 return listData[i];
1567 * Sets the {@link #model} property of the list to a new anonymous {@link
1568 * AbstractListModel} subclass which accesses the provided vector
1569 * directly.
1571 * @param listData The object array to build a new list model on
1572 * @see #setModel
1574 public void setListData(final Vector listData)
1576 setModel(new AbstractListModel()
1578 public int getSize()
1580 return listData.size();
1583 public Object getElementAt(int i)
1585 return listData.elementAt(i);
1591 * Gets the value of the {@link #cellRenderer} property.
1593 * @return The current value of the property
1595 public ListCellRenderer getCellRenderer()
1597 return cellRenderer;
1601 * Sets the value of the {@link #getCellRenderer} property.
1603 * @param renderer The new property value
1605 public void setCellRenderer(ListCellRenderer renderer)
1607 if (cellRenderer == renderer)
1608 return;
1610 ListCellRenderer old = cellRenderer;
1611 cellRenderer = renderer;
1612 firePropertyChange("cellRenderer", old, renderer);
1613 revalidate();
1614 repaint();
1618 * Gets the value of the {@link #model} property.
1620 * @return The current value of the property
1622 public ListModel getModel()
1624 return model;
1628 * Sets the value of the {@link #model} property. The list's {@link
1629 * #listListener} is unsubscribed from the existing model, if it exists,
1630 * and re-subscribed to the new model.
1632 * @param model the new model (<code>null</code> not permitted).
1634 * @throws IllegalArgumentException if <code>model</code> is
1635 * <code>null</code>.
1637 public void setModel(ListModel model)
1639 if (model == null)
1640 throw new IllegalArgumentException("Null 'model' argument.");
1641 if (this.model == model)
1642 return;
1644 if (this.model != null)
1645 this.model.removeListDataListener(listListener);
1647 ListModel old = this.model;
1648 this.model = model;
1650 if (this.model != null)
1651 this.model.addListDataListener(listListener);
1653 firePropertyChange("model", old, model);
1654 revalidate();
1655 repaint();
1659 public ListSelectionModel getSelectionModel()
1661 return selectionModel;
1665 * Sets the value of the {@link #selectionModel} property. The list's
1666 * {@link #listListener} is unsubscribed from the existing selection
1667 * model, if it exists, and re-subscribed to the new selection model.
1669 * @param model The new property value
1671 public void setSelectionModel(ListSelectionModel model)
1673 if (selectionModel == model)
1674 return;
1676 if (selectionModel != null)
1677 selectionModel.removeListSelectionListener(listListener);
1679 ListSelectionModel old = selectionModel;
1680 selectionModel = model;
1682 if (selectionModel != null)
1683 selectionModel.addListSelectionListener(listListener);
1685 firePropertyChange("selectionModel", old, model);
1686 revalidate();
1687 repaint();
1691 * Gets the value of the UI property.
1693 * @return The current property value
1695 public ListUI getUI()
1697 return (ListUI) ui;
1701 * Sets the value of the UI property.
1703 * @param ui The new property value
1705 public void setUI(ListUI ui)
1707 super.setUI(ui);
1711 * Calls {@link #setUI} with the {@link ListUI} subclass
1712 * returned from calling {@link UIManager#getUI}.
1714 public void updateUI()
1716 setUI((ListUI) UIManager.getUI(this));
1720 * Return the class identifier for the list's UI property. This should
1721 * be the constant string <code>"ListUI"</code>, and map to an
1722 * appropriate UI class in the {@link UIManager}.
1724 * @return The class identifier
1726 public String getUIClassID()
1728 return "ListUI";
1733 * Returns the current value of the {@link #prototypeCellValue}
1734 * property. This property holds a reference to a "prototype" data value
1735 * -- typically a String -- which is used to calculate the {@link
1736 * #fixedCellWidth} and {@link #fixedCellHeight} properties, using the
1737 * {@link #cellRenderer} property to acquire a component to render the
1738 * prototype.
1740 * @return The current prototype cell value
1741 * @see #setPrototypeCellValue
1743 public Object getPrototypeCellValue()
1745 return prototypeCellValue;
1749 * <p>Set the {@link #prototypeCellValue} property. This property holds a
1750 * reference to a "prototype" data value -- typically a String -- which
1751 * is used to calculate the {@link #fixedCellWidth} and {@link
1752 * #fixedCellHeight} properties, using the {@link #cellRenderer} property
1753 * to acquire a component to render the prototype.</p>
1755 * <p>It is important that you <em>not</em> set this value to a
1756 * component. It has to be a <em>data value</em> such as the objects you
1757 * would find in the list's model. Setting it to a component will have
1758 * undefined (and undesirable) affects. </p>
1760 * @param obj The new prototype cell value
1761 * @see #getPrototypeCellValue
1763 public void setPrototypeCellValue(Object obj)
1765 if (prototypeCellValue == obj)
1766 return;
1768 Object old = prototypeCellValue;
1769 Component comp = getCellRenderer()
1770 .getListCellRendererComponent(this, obj, 0, false, false);
1771 Dimension d = comp.getPreferredSize();
1772 fixedCellWidth = d.width;
1773 fixedCellHeight = d.height;
1774 prototypeCellValue = obj;
1775 firePropertyChange("prototypeCellValue", old, obj);
1778 public AccessibleContext getAccessibleContext()
1780 return new AccessibleJList();
1784 * Returns a size indicating how much space this list would like to
1785 * consume, when contained in a scrollable viewport. This is part of the
1786 * {@link Scrollable} interface, which interacts with {@link
1787 * ScrollPaneLayout} and {@link JViewport} to define scrollable objects.
1789 * @return The preferred size
1791 public Dimension getPreferredScrollableViewportSize()
1793 //If the layout orientation is not VERTICAL, then this will
1794 //return the value from getPreferredSize. The current ListUI is
1795 //expected to override getPreferredSize to return an appropriate value.
1796 if (getLayoutOrientation() != VERTICAL)
1797 return getPreferredSize();
1799 int size = getModel().getSize();
1801 // Trivial case: if fixedCellWidth and fixedCellHeight were set
1802 // just use them
1803 if (fixedCellHeight != -1 && fixedCellWidth != -1)
1804 return new Dimension(fixedCellWidth, size * fixedCellHeight);
1806 // If the model is empty we use 16 * the number of visible rows
1807 // for the height and either fixedCellWidth (if set) or 256
1808 // for the width
1809 if (size == 0)
1811 if (fixedCellWidth == -1)
1812 return new Dimension(256, 16 * getVisibleRowCount());
1813 else
1814 return new Dimension(fixedCellWidth, 16 * getVisibleRowCount());
1817 // Calculate the width: if fixedCellWidth was set use that, otherwise
1818 // use the preferredWidth
1819 int prefWidth;
1820 if (fixedCellWidth != -1)
1821 prefWidth = fixedCellWidth;
1822 else
1823 prefWidth = getPreferredSize().width;
1825 // Calculate the height: if fixedCellHeight was set use that, otherwise
1826 // use the height of the first row multiplied by the number of visible
1827 // rows
1828 int prefHeight;
1829 if (fixedCellHeight != -1)
1830 prefHeight = fixedCellHeight;
1831 else
1832 prefHeight = getVisibleRowCount() * getCellBounds(0, 0).height;
1834 return new Dimension (prefWidth, prefHeight);
1838 * <p>Return the number of pixels the list must scroll in order to move a
1839 * "unit" of the list into the provided visible rectangle. When the
1840 * provided direction is positive, the call describes a "downwards"
1841 * scroll, which will be exposing a cell at a <em>greater</em> index in
1842 * the list than those elements currently showing. Then the provided
1843 * direction is negative, the call describes an "upwards" scroll, which
1844 * will be exposing a cell at a <em>lesser</em> index in the list than
1845 * those elements currently showing.</p>
1847 * <p>If the provided orientation is <code>HORIZONTAL</code>, the above
1848 * comments refer to "rightwards" for positive direction, and "leftwards"
1849 * for negative.</p>
1852 * @param visibleRect The rectangle to scroll an element into
1853 * @param orientation One of the numeric consants <code>VERTICAL</code>
1854 * or <code>HORIZONTAL</code>
1855 * @param direction An integer indicating the scroll direction: positive means
1856 * forwards (down, right), negative means backwards (up, left)
1858 * @return The scrollable unit increment, in pixels
1860 public int getScrollableUnitIncrement(Rectangle visibleRect,
1861 int orientation, int direction)
1863 ListUI lui = this.getUI();
1864 if (orientation == SwingConstants.VERTICAL)
1866 if (direction > 0)
1868 // Scrolling down
1869 Point bottomLeft = new Point(visibleRect.x,
1870 visibleRect.y + visibleRect.height);
1871 int curIdx = lui.locationToIndex(this, bottomLeft);
1872 Rectangle curBounds = lui.getCellBounds(this, curIdx, curIdx);
1873 if (curBounds.y + curBounds.height == bottomLeft.y)
1875 // we are at the exact bottom of the current cell, so we
1876 // are being asked to scroll to the end of the next one
1877 if (curIdx + 1 < model.getSize())
1879 // there *is* a next item in the list
1880 Rectangle nxtBounds = lui.getCellBounds(this, curIdx + 1, curIdx + 1);
1881 return nxtBounds.height;
1883 else
1885 // no next item, no advance possible
1886 return 0;
1889 else
1891 // we are part way through an existing cell, so we are being
1892 // asked to scroll to the bottom of it
1893 return (curBounds.y + curBounds.height) - bottomLeft.y;
1896 else
1898 // scrolling up
1899 Point topLeft = new Point(visibleRect.x, visibleRect.y);
1900 int curIdx = lui.locationToIndex(this, topLeft);
1901 Rectangle curBounds = lui.getCellBounds(this, curIdx, curIdx);
1902 if (curBounds.y == topLeft.y)
1904 // we are at the exact top of the current cell, so we
1905 // are being asked to scroll to the top of the previous one
1906 if (curIdx > 0)
1908 // there *is* a previous item in the list
1909 Rectangle nxtBounds = lui.getCellBounds(this, curIdx - 1, curIdx - 1);
1910 return -nxtBounds.height;
1912 else
1914 // no previous item, no advance possible
1915 return 0;
1918 else
1920 // we are part way through an existing cell, so we are being
1921 // asked to scroll to the top of it
1922 return curBounds.y - topLeft.y;
1927 // FIXME: handle horizontal scrolling (also wrapping?)
1928 return 1;
1932 * <p>Return the number of pixels the list must scroll in order to move a
1933 * "block" of the list into the provided visible rectangle. When the
1934 * provided direction is positive, the call describes a "downwards"
1935 * scroll, which will be exposing a cell at a <em>greater</em> index in
1936 * the list than those elements currently showing. Then the provided
1937 * direction is negative, the call describes an "upwards" scroll, which
1938 * will be exposing a cell at a <em>lesser</em> index in the list than
1939 * those elements currently showing.</p>
1941 * <p>If the provided orientation is <code>HORIZONTAL</code>, the above
1942 * comments refer to "rightwards" for positive direction, and "leftwards"
1943 * for negative.</p>
1946 * @param visibleRect The rectangle to scroll an element into
1947 * @param orientation One of the numeric consants <code>VERTICAL</code>
1948 * or <code>HORIZONTAL</code>
1949 * @param direction An integer indicating the scroll direction: positive means
1950 * forwards (down, right), negative means backwards (up, left)
1952 * @return The scrollable unit increment, in pixels
1954 public int getScrollableBlockIncrement(Rectangle visibleRect,
1955 int orientation, int direction)
1957 if (orientation == VERTICAL)
1958 return visibleRect.height * direction;
1959 else
1960 return visibleRect.width * direction;
1964 * Gets the value of the <code>scrollableTracksViewportWidth</code> property.
1966 * @return <code>true</code> if the viewport is larger (horizontally)
1967 * than the list and the list should be expanded to fit the viewport;
1968 * <code>false</code> if the viewport is smaller than the list and the
1969 * list should scroll (horizontally) within the viewport
1971 public boolean getScrollableTracksViewportWidth()
1973 Component parent = getParent();
1974 boolean retVal = false;
1975 if (parent instanceof JViewport)
1977 JViewport viewport = (JViewport) parent;
1978 Dimension pref = getPreferredSize();
1979 if (viewport.getSize().width > pref.width)
1980 retVal = true;
1981 if ((getLayoutOrientation() == HORIZONTAL_WRAP)
1982 && (getVisibleRowCount() <= 0))
1983 retVal = true;
1985 return retVal;
1989 * Gets the value of the </code>scrollableTracksViewportWidth</code> property.
1991 * @return <code>true</code> if the viewport is larger (vertically)
1992 * than the list and the list should be expanded to fit the viewport;
1993 * <code>false</code> if the viewport is smaller than the list and the
1994 * list should scroll (vertically) within the viewport
1996 public boolean getScrollableTracksViewportHeight()
1998 Component parent = getParent();
1999 boolean retVal = false;
2000 if (parent instanceof JViewport)
2002 JViewport viewport = (JViewport) parent;
2003 Dimension pref = getPreferredSize();
2004 if (viewport.getSize().height > pref.height)
2005 retVal = true;
2006 if ((getLayoutOrientation() == VERTICAL_WRAP)
2007 && (getVisibleRowCount() <= 0))
2008 retVal = true;
2010 return retVal;
2013 public int getAnchorSelectionIndex()
2015 return selectionModel.getAnchorSelectionIndex();
2018 public int getLeadSelectionIndex()
2020 return selectionModel.getLeadSelectionIndex();
2023 public int getMinSelectionIndex()
2025 return selectionModel.getMaxSelectionIndex();
2028 public int getMaxSelectionIndex()
2030 return selectionModel.getMaxSelectionIndex();
2033 public void clearSelection()
2035 selectionModel.clearSelection();
2038 public void setSelectionInterval(int anchor, int lead)
2040 selectionModel.setSelectionInterval(anchor, lead);
2043 public void addSelectionInterval(int anchor, int lead)
2045 selectionModel.addSelectionInterval(anchor, lead);
2048 public void removeSelectionInterval(int index0, int index1)
2050 selectionModel.removeSelectionInterval(index0, index1);
2054 * Returns the value of the <code>valueIsAdjusting</code> property.
2056 * @return the value
2058 public boolean getValueIsAdjusting()
2060 return valueIsAdjusting;
2064 * Sets the <code>valueIsAdjusting</code> property.
2066 * @param isAdjusting the new value
2068 public void setValueIsAdjusting(boolean isAdjusting)
2070 valueIsAdjusting = isAdjusting;
2074 * Return the value of the <code>dragEnabled</code> property.
2076 * @return the value
2078 * @since 1.4
2080 public boolean getDragEnabled()
2082 return dragEnabled;
2086 * Set the <code>dragEnabled</code> property.
2088 * @param enabled new value
2090 * @since 1.4
2092 public void setDragEnabled(boolean enabled)
2094 dragEnabled = enabled;
2098 * Returns the layout orientation.
2100 * @return the orientation, one of <code>JList.VERTICAL</code>,
2101 * <code>JList.VERTICAL_WRAP</code> and <code>JList.HORIZONTAL_WRAP</code>
2103 * @since 1.4
2105 public int getLayoutOrientation()
2107 return layoutOrientation;
2111 * Sets the layout orientation.
2113 * @param orientation the orientation to set, one of <code>JList.VERTICAL</code>,
2114 * <code>JList.VERTICAL_WRAP</code> and <code>JList.HORIZONTAL_WRAP</code>
2116 * @since 1.4
2118 public void setLayoutOrientation(int orientation)
2120 if (layoutOrientation == orientation)
2121 return;
2123 int old = layoutOrientation;
2124 layoutOrientation = orientation;
2125 firePropertyChange("layoutOrientation", old, orientation);
2129 * Returns the bounds of the rectangle that encloses both list cells
2130 * with index0 and index1.
2132 * @param index0 the index of the first cell
2133 * @param index1 the index of the second cell
2135 * @return the bounds of the rectangle that encloses both list cells
2136 * with index0 and index1, <code>null</code> if one of the indices is
2137 * not valid
2139 public Rectangle getCellBounds(int index0, int index1)
2141 return getUI().getCellBounds(this, index0, index1);
2145 * Returns the next list element (beginning from <code>startIndex</code>
2146 * that starts with <code>prefix</code>. Searching is done in the direction
2147 * specified by <code>bias</code>.
2149 * @param prefix the prefix to search for in the cell values
2150 * @param startIndex the index where to start searching from
2151 * @param bias the search direction, either {@link Position.Bias#Forward}
2152 * or {@link Position.Bias#Backward}
2154 * @return the index of the found element or -1 if no such element has
2155 * been found
2157 * @throws IllegalArgumentException if prefix is <code>null</code> or
2158 * startIndex is not valid
2160 * @since 1.4
2162 public int getNextMatch(String prefix, int startIndex, Position.Bias bias)
2164 if (prefix == null)
2165 throw new IllegalArgumentException("The argument 'prefix' must not be"
2166 + " null.");
2167 if (startIndex < 0)
2168 throw new IllegalArgumentException("The argument 'startIndex' must not"
2169 + " be less than zero.");
2171 int size = model.getSize();
2172 if (startIndex > model.getSize())
2173 throw new IllegalArgumentException("The argument 'startIndex' must not"
2174 + " be greater than the number of"
2175 + " elements in the ListModel.");
2177 int index = -1;
2178 if (bias == Position.Bias.Forward)
2180 for (int i = startIndex; i < size; i++)
2182 String item = model.getElementAt(i).toString();
2183 if (item.startsWith(prefix))
2185 index = i;
2186 break;
2190 else
2192 for (int i = startIndex; i >= 0; i--)
2194 String item = model.getElementAt(i).toString();
2195 if (item.startsWith(prefix))
2197 index = i;
2198 break;
2202 return index;