0N/A/*
3261N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage javax.swing;
0N/A
2333N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/A
0N/Aimport java.util.Vector;
0N/Aimport java.util.Locale;
1983N/Aimport java.util.ArrayList;
1983N/Aimport java.util.Collections;
1983N/Aimport java.util.List;
0N/A
243N/Aimport java.beans.PropertyChangeEvent;
243N/Aimport java.beans.PropertyChangeListener;
243N/Aimport java.beans.Transient;
0N/A
0N/Aimport javax.swing.event.*;
0N/Aimport javax.accessibility.*;
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.text.Position;
0N/A
0N/Aimport java.io.ObjectOutputStream;
0N/Aimport java.io.ObjectInputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.io.Serializable;
0N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/Aimport sun.swing.SwingUtilities2.Section;
0N/Aimport static sun.swing.SwingUtilities2.Section.*;
0N/A
0N/A
0N/A/**
0N/A * A component that displays a list of objects and allows the user to select
0N/A * one or more items. A separate model, {@code ListModel}, maintains the
0N/A * contents of the list.
0N/A * <p>
0N/A * It's easy to display an array or Vector of objects, using the {@code JList}
0N/A * constructor that automatically builds a read-only {@code ListModel} instance
0N/A * for you:
0N/A * <pre>
1983N/A * {@code
0N/A * // Create a JList that displays strings from an array
0N/A *
0N/A * String[] data = {"one", "two", "three", "four"};
1983N/A * JList<String> myList = new JList<String>(data);
0N/A *
0N/A * // Create a JList that displays the superclasses of JList.class, by
0N/A * // creating it with a Vector populated with this data
0N/A *
1983N/A * Vector<Class<?>> superClasses = new Vector<Class<?>>();
1983N/A * Class<JList> rootClass = javax.swing.JList.class;
1983N/A * for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) {
0N/A * superClasses.addElement(cls);
0N/A * }
1983N/A * JList<Class<?>> myList = new JList<Class<?>>(superClasses);
0N/A *
0N/A * // The automatically created model is stored in JList's "model"
0N/A * // property, which you can retrieve
0N/A *
1983N/A * ListModel<Class<?>> model = myList.getModel();
0N/A * for(int i = 0; i < model.getSize(); i++) {
0N/A * System.out.println(model.getElementAt(i));
0N/A * }
1983N/A * }
0N/A * </pre>
0N/A * <p>
0N/A * A {@code ListModel} can be supplied directly to a {@code JList} by way of a
0N/A * constructor or the {@code setModel} method. The contents need not be static -
0N/A * the number of items, and the values of items can change over time. A correct
0N/A * {@code ListModel} implementation notifies the set of
0N/A * {@code javax.swing.event.ListDataListener}s that have been added to it, each
0N/A * time a change occurs. These changes are characterized by a
0N/A * {@code javax.swing.event.ListDataEvent}, which identifies the range of list
0N/A * indices that have been modified, added, or removed. {@code JList}'s
0N/A * {@code ListUI} is responsible for keeping the visual representation up to
0N/A * date with changes, by listening to the model.
0N/A * <p>
0N/A * Simple, dynamic-content, {@code JList} applications can use the
0N/A * {@code DefaultListModel} class to maintain list elements. This class
0N/A * implements the {@code ListModel} interface and also provides a
0N/A * <code>java.util.Vector</code>-like API. Applications that need a more
0N/A * custom <code>ListModel</code> implementation may instead wish to subclass
0N/A * {@code AbstractListModel}, which provides basic support for managing and
0N/A * notifying listeners. For example, a read-only implementation of
0N/A * {@code AbstractListModel}:
0N/A * <pre>
1983N/A * {@code
0N/A * // This list model has about 2^16 elements. Enjoy scrolling.
0N/A *
1983N/A * ListModel<String> bigData = new AbstractListModel<String>() {
0N/A * public int getSize() { return Short.MAX_VALUE; }
1983N/A * public String getElementAt(int index) { return "Index " + index; }
0N/A * };
1983N/A * }
0N/A * </pre>
0N/A * <p>
0N/A * The selection state of a {@code JList} is managed by another separate
0N/A * model, an instance of {@code ListSelectionModel}. {@code JList} is
0N/A * initialized with a selection model on construction, and also contains
0N/A * methods to query or set this selection model. Additionally, {@code JList}
0N/A * provides convenient methods for easily managing the selection. These methods,
0N/A * such as {@code setSelectedIndex} and {@code getSelectedValue}, are cover
0N/A * methods that take care of the details of interacting with the selection
0N/A * model. By default, {@code JList}'s selection model is configured to allow any
0N/A * combination of items to be selected at a time; selection mode
0N/A * {@code MULTIPLE_INTERVAL_SELECTION}. The selection mode can be changed
0N/A * on the selection model directly, or via {@code JList}'s cover method.
0N/A * Responsibility for updating the selection model in response to user gestures
0N/A * lies with the list's {@code ListUI}.
0N/A * <p>
0N/A * A correct {@code ListSelectionModel} implementation notifies the set of
0N/A * {@code javax.swing.event.ListSelectionListener}s that have been added to it
0N/A * each time a change to the selection occurs. These changes are characterized
0N/A * by a {@code javax.swing.event.ListSelectionEvent}, which identifies the range
0N/A * of the selection change.
0N/A * <p>
0N/A * The preferred way to listen for changes in list selection is to add
0N/A * {@code ListSelectionListener}s directly to the {@code JList}. {@code JList}
0N/A * then takes care of listening to the the selection model and notifying your
0N/A * listeners of change.
0N/A * <p>
0N/A * Responsibility for listening to selection changes in order to keep the list's
0N/A * visual representation up to date lies with the list's {@code ListUI}.
0N/A * <p>
0N/A * <a name="renderer">
0N/A * Painting of cells in a {@code JList} is handled by a delegate called a
0N/A * cell renderer, installed on the list as the {@code cellRenderer} property.
0N/A * The renderer provides a {@code java.awt.Component} that is used
0N/A * like a "rubber stamp" to paint the cells. Each time a cell needs to be
0N/A * painted, the list's {@code ListUI} asks the cell renderer for the component,
0N/A * moves it into place, and has it paint the contents of the cell by way of its
0N/A * {@code paint} method. A default cell renderer, which uses a {@code JLabel}
0N/A * component to render, is installed by the lists's {@code ListUI}. You can
0N/A * substitute your own renderer using code like this:
0N/A * <pre>
1983N/A * {@code
0N/A * // Display an icon and a string for each object in the list.
0N/A *
1983N/A * class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
0N/A * final static ImageIcon longIcon = new ImageIcon("long.gif");
0N/A * final static ImageIcon shortIcon = new ImageIcon("short.gif");
0N/A *
0N/A * // This is the only method defined by ListCellRenderer.
0N/A * // We just reconfigure the JLabel each time we're called.
0N/A *
0N/A * public Component getListCellRendererComponent(
1983N/A * JList<?> list, // the list
0N/A * Object value, // value to display
0N/A * int index, // cell index
0N/A * boolean isSelected, // is the cell selected
0N/A * boolean cellHasFocus) // does the cell have focus
0N/A * {
0N/A * String s = value.toString();
0N/A * setText(s);
0N/A * setIcon((s.length() > 10) ? longIcon : shortIcon);
0N/A * if (isSelected) {
0N/A * setBackground(list.getSelectionBackground());
0N/A * setForeground(list.getSelectionForeground());
0N/A * } else {
0N/A * setBackground(list.getBackground());
0N/A * setForeground(list.getForeground());
0N/A * }
0N/A * setEnabled(list.isEnabled());
0N/A * setFont(list.getFont());
0N/A * setOpaque(true);
0N/A * return this;
0N/A * }
0N/A * }
0N/A *
0N/A * myList.setCellRenderer(new MyCellRenderer());
1983N/A * }
0N/A * </pre>
0N/A * <p>
0N/A * Another job for the cell renderer is in helping to determine sizing
0N/A * information for the list. By default, the list's {@code ListUI} determines
0N/A * the size of cells by asking the cell renderer for its preferred
0N/A * size for each list item. This can be expensive for large lists of items.
0N/A * To avoid these calculations, you can set a {@code fixedCellWidth} and
0N/A * {@code fixedCellHeight} on the list, or have these values calculated
0N/A * automatically based on a single prototype value:
0N/A * <a name="prototype_example">
0N/A * <pre>
1983N/A * {@code
1983N/A * JList<String> bigDataList = new JList<String>(bigData);
0N/A *
0N/A * // We don't want the JList implementation to compute the width
0N/A * // or height of all of the list cells, so we give it a string
0N/A * // that's as big as we'll need for any cell. It uses this to
0N/A * // compute values for the fixedCellWidth and fixedCellHeight
0N/A * // properties.
0N/A *
0N/A * bigDataList.setPrototypeCellValue("Index 1234567890");
1983N/A * }
0N/A * </pre>
0N/A * <p>
0N/A * {@code JList} doesn't implement scrolling directly. To create a list that
0N/A * scrolls, make it the viewport view of a {@code JScrollPane}. For example:
0N/A * <pre>
0N/A * JScrollPane scrollPane = new JScrollPane(myList);
0N/A *
0N/A * // Or in two steps:
0N/A * JScrollPane scrollPane = new JScrollPane();
0N/A * scrollPane.getViewport().setView(myList);
0N/A * </pre>
0N/A * <p>
0N/A * {@code JList} doesn't provide any special handling of double or triple
0N/A * (or N) mouse clicks, but it's easy to add a {@code MouseListener} if you
0N/A * wish to take action on these events. Use the {@code locationToIndex}
0N/A * method to determine what cell was clicked. For example:
0N/A * <pre>
0N/A * MouseListener mouseListener = new MouseAdapter() {
0N/A * public void mouseClicked(MouseEvent e) {
0N/A * if (e.getClickCount() == 2) {
0N/A * int index = list.locationToIndex(e.getPoint());
0N/A * System.out.println("Double clicked on Item " + index);
0N/A * }
0N/A * }
0N/A * };
0N/A * list.addMouseListener(mouseListener);
0N/A * </pre>
0N/A * <p>
0N/A * <strong>Warning:</strong> Swing is not thread safe. For more
0N/A * information see <a
0N/A * href="package-summary.html#threading">Swing's Threading
0N/A * Policy</a>.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A * <p>
0N/A * See <a href="http://java.sun.com/docs/books/tutorial/uiswing/components/list.html">How to Use Lists</a>
0N/A * in <a href="http://java.sun.com/Series/Tutorial/index.html"><em>The Java Tutorial</em></a>
0N/A * for further documentation.
0N/A * Also see the article <a href="http://java.sun.com/products/jfc/tsc/tech_topics/jlist_1/jlist.html">Advanced JList Programming</a>
0N/A * in <a href="http://java.sun.com/products/jfc/tsc"><em>The Swing Connection</em></a>.
0N/A * <p>
0N/A * @see ListModel
0N/A * @see AbstractListModel
0N/A * @see DefaultListModel
0N/A * @see ListSelectionModel
0N/A * @see DefaultListSelectionModel
0N/A * @see ListCellRenderer
0N/A * @see DefaultListCellRenderer
0N/A *
1983N/A * @param <E> the type of the elements of this list
1983N/A *
0N/A * @beaninfo
0N/A * attribute: isContainer false
0N/A * description: A component which allows for the selection of one or more objects from a list.
0N/A *
0N/A * @author Hans Muller
0N/A */
1983N/Apublic class JList<E> extends JComponent implements Scrollable, Accessible
0N/A{
0N/A /**
0N/A * @see #getUIClassID
0N/A * @see #readObject
0N/A */
0N/A private static final String uiClassID = "ListUI";
0N/A
0N/A /**
0N/A * Indicates a vertical layout of cells, in a single column;
0N/A * the default layout.
0N/A * @see #setLayoutOrientation
0N/A * @since 1.4
0N/A */
0N/A public static final int VERTICAL = 0;
0N/A
0N/A /**
0N/A * Indicates a "newspaper style" layout with cells flowing vertically
0N/A * then horizontally.
0N/A * @see #setLayoutOrientation
0N/A * @since 1.4
0N/A */
0N/A public static final int VERTICAL_WRAP = 1;
0N/A
0N/A /**
0N/A * Indicates a "newspaper style" layout with cells flowing horizontally
0N/A * then vertically.
0N/A * @see #setLayoutOrientation
0N/A * @since 1.4
0N/A */
0N/A public static final int HORIZONTAL_WRAP = 2;
0N/A
0N/A private int fixedCellWidth = -1;
0N/A private int fixedCellHeight = -1;
0N/A private int horizontalScrollIncrement = -1;
1983N/A private E prototypeCellValue;
0N/A private int visibleRowCount = 8;
0N/A private Color selectionForeground;
0N/A private Color selectionBackground;
0N/A private boolean dragEnabled;
0N/A
0N/A private ListSelectionModel selectionModel;
1983N/A private ListModel<E> dataModel;
1983N/A private ListCellRenderer<? super E> cellRenderer;
0N/A private ListSelectionListener selectionListener;
0N/A
0N/A /**
0N/A * How to lay out the cells; defaults to <code>VERTICAL</code>.
0N/A */
0N/A private int layoutOrientation;
0N/A
0N/A /**
0N/A * The drop mode for this component.
0N/A */
0N/A private DropMode dropMode = DropMode.USE_SELECTION;
0N/A
0N/A /**
0N/A * The drop location.
0N/A */
0N/A private transient DropLocation dropLocation;
0N/A
0N/A /**
0N/A * A subclass of <code>TransferHandler.DropLocation</code> representing
0N/A * a drop location for a <code>JList</code>.
0N/A *
0N/A * @see #getDropLocation
0N/A * @since 1.6
0N/A */
0N/A public static final class DropLocation extends TransferHandler.DropLocation {
0N/A private final int index;
0N/A private final boolean isInsert;
0N/A
0N/A private DropLocation(Point p, int index, boolean isInsert) {
0N/A super(p);
0N/A this.index = index;
0N/A this.isInsert = isInsert;
0N/A }
0N/A
0N/A /**
0N/A * Returns the index where dropped data should be placed in the
0N/A * list. Interpretation of the value depends on the drop mode set on
0N/A * the associated component. If the drop mode is either
0N/A * <code>DropMode.USE_SELECTION</code> or <code>DropMode.ON</code>,
0N/A * the return value is an index of a row in the list. If the drop mode is
0N/A * <code>DropMode.INSERT</code>, the return value refers to the index
0N/A * where the data should be inserted. If the drop mode is
0N/A * <code>DropMode.ON_OR_INSERT</code>, the value of
0N/A * <code>isInsert()</code> indicates whether the index is an index
0N/A * of a row, or an insert index.
0N/A * <p>
0N/A * <code>-1</code> indicates that the drop occurred over empty space,
0N/A * and no index could be calculated.
0N/A *
0N/A * @return the drop index
0N/A */
0N/A public int getIndex() {
0N/A return index;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not this location represents an insert
0N/A * location.
0N/A *
0N/A * @return whether or not this is an insert location
0N/A */
0N/A public boolean isInsert() {
0N/A return isInsert;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string representation of this drop location.
0N/A * This method is intended to be used for debugging purposes,
0N/A * and the content and format of the returned string may vary
0N/A * between implementations.
0N/A *
0N/A * @return a string representation of this drop location
0N/A */
0N/A public String toString() {
0N/A return getClass().getName()
0N/A + "[dropPoint=" + getDropPoint() + ","
0N/A + "index=" + index + ","
0N/A + "insert=" + isInsert + "]";
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Constructs a {@code JList} that displays elements from the specified,
0N/A * {@code non-null}, model. All {@code JList} constructors delegate to
0N/A * this one.
0N/A * <p>
0N/A * This constructor registers the list with the {@code ToolTipManager},
0N/A * allowing for tooltips to be provided by the cell renderers.
0N/A *
0N/A * @param dataModel the model for the list
0N/A * @exception IllegalArgumentException if the model is {@code null}
0N/A */
1983N/A public JList(ListModel<E> dataModel)
0N/A {
0N/A if (dataModel == null) {
0N/A throw new IllegalArgumentException("dataModel must be non null");
0N/A }
0N/A
0N/A // Register with the ToolTipManager so that tooltips from the
0N/A // renderer show through.
0N/A ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
0N/A toolTipManager.registerComponent(this);
0N/A
0N/A layoutOrientation = VERTICAL;
0N/A
0N/A this.dataModel = dataModel;
0N/A selectionModel = createSelectionModel();
0N/A setAutoscrolls(true);
0N/A setOpaque(true);
0N/A updateUI();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a <code>JList</code> that displays the elements in
0N/A * the specified array. This constructor creates a read-only model
0N/A * for the given array, and then delegates to the constructor that
0N/A * takes a {@code ListModel}.
0N/A * <p>
0N/A * Attempts to pass a {@code null} value to this method results in
0N/A * undefined behavior and, most likely, exceptions. The created model
0N/A * references the given array directly. Attempts to modify the array
0N/A * after constructing the list results in undefined behavior.
0N/A *
0N/A * @param listData the array of Objects to be loaded into the data model,
0N/A * {@code non-null}
0N/A */
1983N/A public JList(final E[] listData)
0N/A {
0N/A this (
1983N/A new AbstractListModel<E>() {
0N/A public int getSize() { return listData.length; }
1983N/A public E getElementAt(int i) { return listData[i]; }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a <code>JList</code> that displays the elements in
0N/A * the specified <code>Vector</code>. This constructor creates a read-only
0N/A * model for the given {@code Vector}, and then delegates to the constructor
0N/A * that takes a {@code ListModel}.
0N/A * <p>
0N/A * Attempts to pass a {@code null} value to this method results in
0N/A * undefined behavior and, most likely, exceptions. The created model
0N/A * references the given {@code Vector} directly. Attempts to modify the
0N/A * {@code Vector} after constructing the list results in undefined behavior.
0N/A *
0N/A * @param listData the <code>Vector</code> to be loaded into the
0N/A * data model, {@code non-null}
0N/A */
1983N/A public JList(final Vector<? extends E> listData) {
0N/A this (
1983N/A new AbstractListModel<E>() {
0N/A public int getSize() { return listData.size(); }
1983N/A public E getElementAt(int i) { return listData.elementAt(i); }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a <code>JList</code> with an empty, read-only, model.
0N/A */
0N/A public JList() {
0N/A this (
1983N/A new AbstractListModel<E>() {
0N/A public int getSize() { return 0; }
1983N/A public E getElementAt(int i) { throw new IndexOutOfBoundsException("No Data Model"); }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the {@code ListUI}, the look and feel object that
0N/A * renders this component.
0N/A *
0N/A * @return the <code>ListUI</code> object that renders this component
0N/A */
0N/A public ListUI getUI() {
0N/A return (ListUI)ui;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the {@code ListUI}, the look and feel object that
0N/A * renders this component.
0N/A *
0N/A * @param ui the <code>ListUI</code> object
0N/A * @see UIDefaults#getUI
0N/A * @beaninfo
0N/A * bound: true
0N/A * hidden: true
0N/A * attribute: visualUpdate true
0N/A * description: The UI object that implements the Component's LookAndFeel.
0N/A */
0N/A public void setUI(ListUI ui) {
0N/A super.setUI(ui);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Resets the {@code ListUI} property by setting it to the value provided
0N/A * by the current look and feel. If the current cell renderer was installed
0N/A * by the developer (rather than the look and feel itself), this also causes
0N/A * the cell renderer and its children to be updated, by calling
0N/A * {@code SwingUtilities.updateComponentTreeUI} on it.
0N/A *
0N/A * @see UIManager#getUI
0N/A * @see SwingUtilities#updateComponentTreeUI
0N/A */
0N/A public void updateUI() {
0N/A setUI((ListUI)UIManager.getUI(this));
0N/A
1983N/A ListCellRenderer<? super E> renderer = getCellRenderer();
0N/A if (renderer instanceof Component) {
0N/A SwingUtilities.updateComponentTreeUI((Component)renderer);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns {@code "ListUI"}, the <code>UIDefaults</code> key used to look
0N/A * up the name of the {@code javax.swing.plaf.ListUI} class that defines
0N/A * the look and feel for this component.
0N/A *
0N/A * @return the string "ListUI"
0N/A * @see JComponent#getUIClassID
0N/A * @see UIDefaults#getUI
0N/A */
0N/A public String getUIClassID() {
0N/A return uiClassID;
0N/A }
0N/A
0N/A
0N/A /* -----private-----
0N/A * This method is called by setPrototypeCellValue and setCellRenderer
0N/A * to update the fixedCellWidth and fixedCellHeight properties from the
0N/A * current value of prototypeCellValue (if it's non null).
0N/A * <p>
0N/A * This method sets fixedCellWidth and fixedCellHeight but does <b>not</b>
0N/A * generate PropertyChangeEvents for them.
0N/A *
0N/A * @see #setPrototypeCellValue
0N/A * @see #setCellRenderer
0N/A */
0N/A private void updateFixedCellSize()
0N/A {
1983N/A ListCellRenderer<? super E> cr = getCellRenderer();
1983N/A E value = getPrototypeCellValue();
0N/A
0N/A if ((cr != null) && (value != null)) {
0N/A Component c = cr.getListCellRendererComponent(this, value, 0, false, false);
0N/A
0N/A /* The ListUI implementation will add Component c to its private
0N/A * CellRendererPane however we can't assume that's already
0N/A * been done here. So we temporarily set the one "inherited"
0N/A * property that may affect the renderer components preferred size:
0N/A * its font.
0N/A */
0N/A Font f = c.getFont();
0N/A c.setFont(getFont());
0N/A
0N/A Dimension d = c.getPreferredSize();
0N/A fixedCellWidth = d.width;
0N/A fixedCellHeight = d.height;
0N/A
0N/A c.setFont(f);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the "prototypical" cell value -- a value used to calculate a
0N/A * fixed width and height for cells. This can be {@code null} if there
0N/A * is no such value.
0N/A *
0N/A * @return the value of the {@code prototypeCellValue} property
0N/A * @see #setPrototypeCellValue
0N/A */
1983N/A public E getPrototypeCellValue() {
0N/A return prototypeCellValue;
0N/A }
0N/A
0N/A /**
0N/A * Sets the {@code prototypeCellValue} property, and then (if the new value
0N/A * is {@code non-null}), computes the {@code fixedCellWidth} and
0N/A * {@code fixedCellHeight} properties by requesting the cell renderer
0N/A * component for the given value (and index 0) from the cell renderer, and
0N/A * using that component's preferred size.
0N/A * <p>
0N/A * This method is useful when the list is too long to allow the
0N/A * {@code ListUI} to compute the width/height of each cell, and there is a
0N/A * single cell value that is known to occupy as much space as any of the
0N/A * others, a so-called prototype.
0N/A * <p>
0N/A * While all three of the {@code prototypeCellValue},
0N/A * {@code fixedCellHeight}, and {@code fixedCellWidth} properties may be
0N/A * modified by this method, {@code PropertyChangeEvent} notifications are
0N/A * only sent when the {@code prototypeCellValue} property changes.
0N/A * <p>
0N/A * To see an example which sets this property, see the
0N/A * <a href="#prototype_example">class description</a> above.
0N/A * <p>
0N/A * The default value of this property is <code>null</code>.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param prototypeCellValue the value on which to base
0N/A * <code>fixedCellWidth</code> and
0N/A * <code>fixedCellHeight</code>
0N/A * @see #getPrototypeCellValue
0N/A * @see #setFixedCellWidth
0N/A * @see #setFixedCellHeight
0N/A * @see JComponent#addPropertyChangeListener
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The cell prototype value, used to compute cell width and height.
0N/A */
1983N/A public void setPrototypeCellValue(E prototypeCellValue) {
1983N/A E oldValue = this.prototypeCellValue;
0N/A this.prototypeCellValue = prototypeCellValue;
0N/A
0N/A /* If the prototypeCellValue has changed and is non-null,
0N/A * then recompute fixedCellWidth and fixedCellHeight.
0N/A */
0N/A
0N/A if ((prototypeCellValue != null) && !prototypeCellValue.equals(oldValue)) {
0N/A updateFixedCellSize();
0N/A }
0N/A
0N/A firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the value of the {@code fixedCellWidth} property.
0N/A *
0N/A * @return the fixed cell width
0N/A * @see #setFixedCellWidth
0N/A */
0N/A public int getFixedCellWidth() {
0N/A return fixedCellWidth;
0N/A }
0N/A
0N/A /**
0N/A * Sets a fixed value to be used for the width of every cell in the list.
0N/A * If {@code width} is -1, cell widths are computed in the {@code ListUI}
0N/A * by applying <code>getPreferredSize</code> to the cell renderer component
0N/A * for each list element.
0N/A * <p>
0N/A * The default value of this property is {@code -1}.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param width the width to be used for all cells in the list
0N/A * @see #setPrototypeCellValue
0N/A * @see #setFixedCellWidth
0N/A * @see JComponent#addPropertyChangeListener
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: Defines a fixed cell width when greater than zero.
0N/A */
0N/A public void setFixedCellWidth(int width) {
0N/A int oldValue = fixedCellWidth;
0N/A fixedCellWidth = width;
0N/A firePropertyChange("fixedCellWidth", oldValue, fixedCellWidth);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the value of the {@code fixedCellHeight} property.
0N/A *
0N/A * @return the fixed cell height
0N/A * @see #setFixedCellHeight
0N/A */
0N/A public int getFixedCellHeight() {
0N/A return fixedCellHeight;
0N/A }
0N/A
0N/A /**
0N/A * Sets a fixed value to be used for the height of every cell in the list.
0N/A * If {@code height} is -1, cell heights are computed in the {@code ListUI}
0N/A * by applying <code>getPreferredSize</code> to the cell renderer component
0N/A * for each list element.
0N/A * <p>
0N/A * The default value of this property is {@code -1}.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param height the height to be used for for all cells in the list
0N/A * @see #setPrototypeCellValue
0N/A * @see #setFixedCellWidth
0N/A * @see JComponent#addPropertyChangeListener
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: Defines a fixed cell height when greater than zero.
0N/A */
0N/A public void setFixedCellHeight(int height) {
0N/A int oldValue = fixedCellHeight;
0N/A fixedCellHeight = height;
0N/A firePropertyChange("fixedCellHeight", oldValue, fixedCellHeight);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the object responsible for painting list items.
0N/A *
0N/A * @return the value of the {@code cellRenderer} property
0N/A * @see #setCellRenderer
0N/A */
243N/A @Transient
1983N/A public ListCellRenderer<? super E> getCellRenderer() {
0N/A return cellRenderer;
0N/A }
0N/A
0N/A /**
0N/A * Sets the delegate that is used to paint each cell in the list.
0N/A * The job of a cell renderer is discussed in detail in the
0N/A * <a href="#renderer">class level documentation</a>.
0N/A * <p>
0N/A * If the {@code prototypeCellValue} property is {@code non-null},
0N/A * setting the cell renderer also causes the {@code fixedCellWidth} and
0N/A * {@code fixedCellHeight} properties to be re-calculated. Only one
0N/A * <code>PropertyChangeEvent</code> is generated however -
0N/A * for the <code>cellRenderer</code> property.
0N/A * <p>
0N/A * The default value of this property is provided by the {@code ListUI}
0N/A * delegate, i.e. by the look and feel implementation.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param cellRenderer the <code>ListCellRenderer</code>
0N/A * that paints list cells
0N/A * @see #getCellRenderer
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The component used to draw the cells.
0N/A */
1983N/A public void setCellRenderer(ListCellRenderer<? super E> cellRenderer) {
1983N/A ListCellRenderer<? super E> oldValue = this.cellRenderer;
0N/A this.cellRenderer = cellRenderer;
0N/A
0N/A /* If the cellRenderer has changed and prototypeCellValue
0N/A * was set, then recompute fixedCellWidth and fixedCellHeight.
0N/A */
0N/A if ((cellRenderer != null) && !cellRenderer.equals(oldValue)) {
0N/A updateFixedCellSize();
0N/A }
0N/A
0N/A firePropertyChange("cellRenderer", oldValue, cellRenderer);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the color used to draw the foreground of selected items.
0N/A * {@code DefaultListCellRenderer} uses this color to draw the foreground
0N/A * of items in the selected state, as do the renderers installed by most
0N/A * {@code ListUI} implementations.
0N/A *
0N/A * @return the color to draw the foreground of selected items
0N/A * @see #setSelectionForeground
0N/A * @see DefaultListCellRenderer
0N/A */
0N/A public Color getSelectionForeground() {
0N/A return selectionForeground;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the color used to draw the foreground of selected items, which
0N/A * cell renderers can use to render text and graphics.
0N/A * {@code DefaultListCellRenderer} uses this color to draw the foreground
0N/A * of items in the selected state, as do the renderers installed by most
0N/A * {@code ListUI} implementations.
0N/A * <p>
0N/A * The default value of this property is defined by the look and feel
0N/A * implementation.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param selectionForeground the {@code Color} to use in the foreground
0N/A * for selected list items
0N/A * @see #getSelectionForeground
0N/A * @see #setSelectionBackground
0N/A * @see #setForeground
0N/A * @see #setBackground
0N/A * @see #setFont
0N/A * @see DefaultListCellRenderer
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The foreground color of selected cells.
0N/A */
0N/A public void setSelectionForeground(Color selectionForeground) {
0N/A Color oldValue = this.selectionForeground;
0N/A this.selectionForeground = selectionForeground;
0N/A firePropertyChange("selectionForeground", oldValue, selectionForeground);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the color used to draw the background of selected items.
0N/A * {@code DefaultListCellRenderer} uses this color to draw the background
0N/A * of items in the selected state, as do the renderers installed by most
0N/A * {@code ListUI} implementations.
0N/A *
0N/A * @return the color to draw the background of selected items
0N/A * @see #setSelectionBackground
0N/A * @see DefaultListCellRenderer
0N/A */
0N/A public Color getSelectionBackground() {
0N/A return selectionBackground;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the color used to draw the background of selected items, which
0N/A * cell renderers can use fill selected cells.
0N/A * {@code DefaultListCellRenderer} uses this color to fill the background
0N/A * of items in the selected state, as do the renderers installed by most
0N/A * {@code ListUI} implementations.
0N/A * <p>
0N/A * The default value of this property is defined by the look
0N/A * and feel implementation.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param selectionBackground the {@code Color} to use for the
0N/A * background of selected cells
0N/A * @see #getSelectionBackground
0N/A * @see #setSelectionForeground
0N/A * @see #setForeground
0N/A * @see #setBackground
0N/A * @see #setFont
0N/A * @see DefaultListCellRenderer
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The background color of selected cells.
0N/A */
0N/A public void setSelectionBackground(Color selectionBackground) {
0N/A Color oldValue = this.selectionBackground;
0N/A this.selectionBackground = selectionBackground;
0N/A firePropertyChange("selectionBackground", oldValue, selectionBackground);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the value of the {@code visibleRowCount} property. See the
0N/A * documentation for {@link #setVisibleRowCount} for details on how to
0N/A * interpret this value.
0N/A *
0N/A * @return the value of the {@code visibleRowCount} property.
0N/A * @see #setVisibleRowCount
0N/A */
0N/A public int getVisibleRowCount() {
0N/A return visibleRowCount;
0N/A }
0N/A
0N/A /**
0N/A * Sets the {@code visibleRowCount} property, which has different meanings
0N/A * depending on the layout orientation: For a {@code VERTICAL} layout
0N/A * orientation, this sets the preferred number of rows to display without
0N/A * requiring scrolling; for other orientations, it affects the wrapping of
0N/A * cells.
0N/A * <p>
0N/A * In {@code VERTICAL} orientation:<br>
0N/A * Setting this property affects the return value of the
0N/A * {@link #getPreferredScrollableViewportSize} method, which is used to
0N/A * calculate the preferred size of an enclosing viewport. See that method's
0N/A * documentation for more details.
0N/A * <p>
0N/A * In {@code HORIZONTAL_WRAP} and {@code VERTICAL_WRAP} orientations:<br>
0N/A * This affects how cells are wrapped. See the documentation of
0N/A * {@link #setLayoutOrientation} for more details.
0N/A * <p>
0N/A * The default value of this property is {@code 8}.
0N/A * <p>
0N/A * Calling this method with a negative value results in the property
0N/A * being set to {@code 0}.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param visibleRowCount an integer specifying the preferred number of
0N/A * rows to display without requiring scrolling
0N/A * @see #getVisibleRowCount
0N/A * @see #getPreferredScrollableViewportSize
0N/A * @see #setLayoutOrientation
0N/A * @see JComponent#getVisibleRect
0N/A * @see JViewport
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The preferred number of rows to display without
0N/A * requiring scrolling
0N/A */
0N/A public void setVisibleRowCount(int visibleRowCount) {
0N/A int oldValue = this.visibleRowCount;
0N/A this.visibleRowCount = Math.max(0, visibleRowCount);
0N/A firePropertyChange("visibleRowCount", oldValue, visibleRowCount);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the layout orientation property for the list: {@code VERTICAL}
0N/A * if the layout is a single column of cells, {@code VERTICAL_WRAP} if the
0N/A * layout is "newspaper style" with the content flowing vertically then
0N/A * horizontally, or {@code HORIZONTAL_WRAP} if the layout is "newspaper
0N/A * style" with the content flowing horizontally then vertically.
0N/A *
0N/A * @return the value of the {@code layoutOrientation} property
0N/A * @see #setLayoutOrientation
0N/A * @since 1.4
0N/A */
0N/A public int getLayoutOrientation() {
0N/A return layoutOrientation;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Defines the way list cells are layed out. Consider a {@code JList}
0N/A * with five cells. Cells can be layed out in one of the following ways:
0N/A * <p>
0N/A * <pre>
0N/A * VERTICAL: 0
0N/A * 1
0N/A * 2
0N/A * 3
0N/A * 4
0N/A *
0N/A * HORIZONTAL_WRAP: 0 1 2
0N/A * 3 4
0N/A *
0N/A * VERTICAL_WRAP: 0 3
0N/A * 1 4
0N/A * 2
0N/A * </pre>
0N/A * <p>
0N/A * A description of these layouts follows:
0N/A *
0N/A * <table border="1"
0N/A * summary="Describes layouts VERTICAL, HORIZONTAL_WRAP, and VERTICAL_WRAP">
0N/A * <tr><th><p align="left">Value</p></th><th><p align="left">Description</p></th></tr>
0N/A * <tr><td><code>VERTICAL</code>
0N/A * <td>Cells are layed out vertically in a single column.
0N/A * <tr><td><code>HORIZONTAL_WRAP</code>
0N/A * <td>Cells are layed out horizontally, wrapping to a new row as
0N/A * necessary. If the {@code visibleRowCount} property is less than
0N/A * or equal to zero, wrapping is determined by the width of the
0N/A * list; otherwise wrapping is done in such a way as to ensure
0N/A * {@code visibleRowCount} rows in the list.
0N/A * <tr><td><code>VERTICAL_WRAP</code>
0N/A * <td>Cells are layed out vertically, wrapping to a new column as
0N/A * necessary. If the {@code visibleRowCount} property is less than
0N/A * or equal to zero, wrapping is determined by the height of the
0N/A * list; otherwise wrapping is done at {@code visibleRowCount} rows.
0N/A * </table>
0N/A * <p>
0N/A * The default value of this property is <code>VERTICAL</code>.
0N/A *
0N/A * @param layoutOrientation the new layout orientation, one of:
0N/A * {@code VERTICAL}, {@code HORIZONTAL_WRAP} or {@code VERTICAL_WRAP}
0N/A * @see #getLayoutOrientation
0N/A * @see #setVisibleRowCount
0N/A * @see #getScrollableTracksViewportHeight
0N/A * @see #getScrollableTracksViewportWidth
0N/A * @throws IllegalArgumentException if {@code layoutOrientation} isn't one of the
0N/A * allowable values
0N/A * @since 1.4
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: Defines the way list cells are layed out.
0N/A * enum: VERTICAL JList.VERTICAL
0N/A * HORIZONTAL_WRAP JList.HORIZONTAL_WRAP
0N/A * VERTICAL_WRAP JList.VERTICAL_WRAP
0N/A */
0N/A public void setLayoutOrientation(int layoutOrientation) {
0N/A int oldValue = this.layoutOrientation;
0N/A switch (layoutOrientation) {
0N/A case VERTICAL:
0N/A case VERTICAL_WRAP:
0N/A case HORIZONTAL_WRAP:
0N/A this.layoutOrientation = layoutOrientation;
0N/A firePropertyChange("layoutOrientation", oldValue, layoutOrientation);
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("layoutOrientation must be one of: VERTICAL, HORIZONTAL_WRAP or VERTICAL_WRAP");
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the smallest list index that is currently visible.
0N/A * In a left-to-right {@code componentOrientation}, the first visible
0N/A * cell is found closest to the list's upper-left corner. In right-to-left
0N/A * orientation, it is found closest to the upper-right corner.
0N/A * If nothing is visible or the list is empty, {@code -1} is returned.
0N/A * Note that the returned cell may only be partially visible.
0N/A *
0N/A * @return the index of the first visible cell
0N/A * @see #getLastVisibleIndex
0N/A * @see JComponent#getVisibleRect
0N/A */
0N/A public int getFirstVisibleIndex() {
0N/A Rectangle r = getVisibleRect();
0N/A int first;
0N/A if (this.getComponentOrientation().isLeftToRight()) {
0N/A first = locationToIndex(r.getLocation());
0N/A } else {
0N/A first = locationToIndex(new Point((r.x + r.width) - 1, r.y));
0N/A }
0N/A if (first != -1) {
0N/A Rectangle bounds = getCellBounds(first, first);
0N/A if (bounds != null) {
0N/A SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds);
0N/A if (bounds.width == 0 || bounds.height == 0) {
0N/A first = -1;
0N/A }
0N/A }
0N/A }
0N/A return first;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the largest list index that is currently visible.
0N/A * If nothing is visible or the list is empty, {@code -1} is returned.
0N/A * Note that the returned cell may only be partially visible.
0N/A *
0N/A * @return the index of the last visible cell
0N/A * @see #getFirstVisibleIndex
0N/A * @see JComponent#getVisibleRect
0N/A */
0N/A public int getLastVisibleIndex() {
0N/A boolean leftToRight = this.getComponentOrientation().isLeftToRight();
0N/A Rectangle r = getVisibleRect();
0N/A Point lastPoint;
0N/A if (leftToRight) {
0N/A lastPoint = new Point((r.x + r.width) - 1, (r.y + r.height) - 1);
0N/A } else {
0N/A lastPoint = new Point(r.x, (r.y + r.height) - 1);
0N/A }
0N/A int location = locationToIndex(lastPoint);
0N/A
0N/A if (location != -1) {
0N/A Rectangle bounds = getCellBounds(location, location);
0N/A
0N/A if (bounds != null) {
0N/A SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds);
0N/A if (bounds.width == 0 || bounds.height == 0) {
0N/A // Try the top left(LTR) or top right(RTL) corner, and
0N/A // then go across checking each cell for HORIZONTAL_WRAP.
0N/A // Try the lower left corner, and then go across checking
0N/A // each cell for other list layout orientation.
0N/A boolean isHorizontalWrap =
0N/A (getLayoutOrientation() == HORIZONTAL_WRAP);
0N/A Point visibleLocation = isHorizontalWrap ?
0N/A new Point(lastPoint.x, r.y) :
0N/A new Point(r.x, lastPoint.y);
0N/A int last;
0N/A int visIndex = -1;
0N/A int lIndex = location;
0N/A location = -1;
0N/A
0N/A do {
0N/A last = visIndex;
0N/A visIndex = locationToIndex(visibleLocation);
0N/A
0N/A if (visIndex != -1) {
0N/A bounds = getCellBounds(visIndex, visIndex);
0N/A if (visIndex != lIndex && bounds != null &&
0N/A bounds.contains(visibleLocation)) {
0N/A location = visIndex;
0N/A if (isHorizontalWrap) {
0N/A visibleLocation.y = bounds.y + bounds.height;
0N/A if (visibleLocation.y >= lastPoint.y) {
0N/A // Past visible region, bail.
0N/A last = visIndex;
0N/A }
0N/A }
0N/A else {
0N/A visibleLocation.x = bounds.x + bounds.width;
0N/A if (visibleLocation.x >= lastPoint.x) {
0N/A // Past visible region, bail.
0N/A last = visIndex;
0N/A }
0N/A }
0N/A
0N/A }
0N/A else {
0N/A last = visIndex;
0N/A }
0N/A }
0N/A } while (visIndex != -1 && last != visIndex);
0N/A }
0N/A }
0N/A }
0N/A return location;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Scrolls the list within an enclosing viewport to make the specified
0N/A * cell completely visible. This calls {@code scrollRectToVisible} with
0N/A * the bounds of the specified cell. For this method to work, the
0N/A * {@code JList} must be within a <code>JViewport</code>.
0N/A * <p>
0N/A * If the given index is outside the list's range of cells, this method
0N/A * results in nothing.
0N/A *
0N/A * @param index the index of the cell to make visible
0N/A * @see JComponent#scrollRectToVisible
0N/A * @see #getVisibleRect
0N/A */
0N/A public void ensureIndexIsVisible(int index) {
0N/A Rectangle cellBounds = getCellBounds(index, index);
0N/A if (cellBounds != null) {
0N/A scrollRectToVisible(cellBounds);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Turns on or off automatic drag handling. In order to enable automatic
0N/A * drag handling, this property should be set to {@code true}, and the
0N/A * list's {@code TransferHandler} needs to be {@code non-null}.
0N/A * The default value of the {@code dragEnabled} property is {@code false}.
0N/A * <p>
0N/A * The job of honoring this property, and recognizing a user drag gesture,
0N/A * lies with the look and feel implementation, and in particular, the list's
0N/A * {@code ListUI}. When automatic drag handling is enabled, most look and
0N/A * feels (including those that subclass {@code BasicLookAndFeel}) begin a
0N/A * drag and drop operation whenever the user presses the mouse button over
0N/A * an item and then moves the mouse a few pixels. Setting this property to
0N/A * {@code true} can therefore have a subtle effect on how selections behave.
0N/A * <p>
0N/A * If a look and feel is used that ignores this property, you can still
0N/A * begin a drag and drop operation by calling {@code exportAsDrag} on the
0N/A * list's {@code TransferHandler}.
0N/A *
0N/A * @param b whether or not to enable automatic drag handling
0N/A * @exception HeadlessException if
0N/A * <code>b</code> is <code>true</code> and
0N/A * <code>GraphicsEnvironment.isHeadless()</code>
0N/A * returns <code>true</code>
0N/A * @see java.awt.GraphicsEnvironment#isHeadless
0N/A * @see #getDragEnabled
0N/A * @see #setTransferHandler
0N/A * @see TransferHandler
0N/A * @since 1.4
0N/A *
0N/A * @beaninfo
0N/A * description: determines whether automatic drag handling is enabled
0N/A * bound: false
0N/A */
0N/A public void setDragEnabled(boolean b) {
0N/A if (b && GraphicsEnvironment.isHeadless()) {
0N/A throw new HeadlessException();
0N/A }
0N/A dragEnabled = b;
0N/A }
0N/A
0N/A /**
0N/A * Returns whether or not automatic drag handling is enabled.
0N/A *
0N/A * @return the value of the {@code dragEnabled} property
0N/A * @see #setDragEnabled
0N/A * @since 1.4
0N/A */
0N/A public boolean getDragEnabled() {
0N/A return dragEnabled;
0N/A }
0N/A
0N/A /**
0N/A * Sets the drop mode for this component. For backward compatibility,
0N/A * the default for this property is <code>DropMode.USE_SELECTION</code>.
0N/A * Usage of one of the other modes is recommended, however, for an
0N/A * improved user experience. <code>DropMode.ON</code>, for instance,
0N/A * offers similar behavior of showing items as selected, but does so without
0N/A * affecting the actual selection in the list.
0N/A * <p>
0N/A * <code>JList</code> supports the following drop modes:
0N/A * <ul>
0N/A * <li><code>DropMode.USE_SELECTION</code></li>
0N/A * <li><code>DropMode.ON</code></li>
0N/A * <li><code>DropMode.INSERT</code></li>
0N/A * <li><code>DropMode.ON_OR_INSERT</code></li>
0N/A * </ul>
0N/A * The drop mode is only meaningful if this component has a
0N/A * <code>TransferHandler</code> that accepts drops.
0N/A *
0N/A * @param dropMode the drop mode to use
0N/A * @throws IllegalArgumentException if the drop mode is unsupported
0N/A * or <code>null</code>
0N/A * @see #getDropMode
0N/A * @see #getDropLocation
0N/A * @see #setTransferHandler
0N/A * @see TransferHandler
0N/A * @since 1.6
0N/A */
0N/A public final void setDropMode(DropMode dropMode) {
0N/A if (dropMode != null) {
0N/A switch (dropMode) {
0N/A case USE_SELECTION:
0N/A case ON:
0N/A case INSERT:
0N/A case ON_OR_INSERT:
0N/A this.dropMode = dropMode;
0N/A return;
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for list");
0N/A }
0N/A
0N/A /**
0N/A * Returns the drop mode for this component.
0N/A *
0N/A * @return the drop mode for this component
0N/A * @see #setDropMode
0N/A * @since 1.6
0N/A */
0N/A public final DropMode getDropMode() {
0N/A return dropMode;
0N/A }
0N/A
0N/A /**
0N/A * Calculates a drop location in this component, representing where a
0N/A * drop at the given point should insert data.
0N/A *
0N/A * @param p the point to calculate a drop location for
0N/A * @return the drop location, or <code>null</code>
0N/A */
0N/A DropLocation dropLocationForPoint(Point p) {
0N/A DropLocation location = null;
0N/A Rectangle rect = null;
0N/A
0N/A int index = locationToIndex(p);
0N/A if (index != -1) {
0N/A rect = getCellBounds(index, index);
0N/A }
0N/A
0N/A switch(dropMode) {
0N/A case USE_SELECTION:
0N/A case ON:
0N/A location = new DropLocation(p,
0N/A (rect != null && rect.contains(p)) ? index : -1,
0N/A false);
0N/A
0N/A break;
0N/A case INSERT:
0N/A if (index == -1) {
0N/A location = new DropLocation(p, getModel().getSize(), true);
0N/A break;
0N/A }
0N/A
0N/A if (layoutOrientation == HORIZONTAL_WRAP) {
0N/A boolean ltr = getComponentOrientation().isLeftToRight();
0N/A
0N/A if (SwingUtilities2.liesInHorizontal(rect, p, ltr, false) == TRAILING) {
0N/A index++;
0N/A // special case for below all cells
0N/A } else if (index == getModel().getSize() - 1 && p.y >= rect.y + rect.height) {
0N/A index++;
0N/A }
0N/A } else {
0N/A if (SwingUtilities2.liesInVertical(rect, p, false) == TRAILING) {
0N/A index++;
0N/A }
0N/A }
0N/A
0N/A location = new DropLocation(p, index, true);
0N/A
0N/A break;
0N/A case ON_OR_INSERT:
0N/A if (index == -1) {
0N/A location = new DropLocation(p, getModel().getSize(), true);
0N/A break;
0N/A }
0N/A
0N/A boolean between = false;
0N/A
0N/A if (layoutOrientation == HORIZONTAL_WRAP) {
0N/A boolean ltr = getComponentOrientation().isLeftToRight();
0N/A
0N/A Section section = SwingUtilities2.liesInHorizontal(rect, p, ltr, true);
0N/A if (section == TRAILING) {
0N/A index++;
0N/A between = true;
0N/A // special case for below all cells
0N/A } else if (index == getModel().getSize() - 1 && p.y >= rect.y + rect.height) {
0N/A index++;
0N/A between = true;
0N/A } else if (section == LEADING) {
0N/A between = true;
0N/A }
0N/A } else {
0N/A Section section = SwingUtilities2.liesInVertical(rect, p, true);
0N/A if (section == LEADING) {
0N/A between = true;
0N/A } else if (section == TRAILING) {
0N/A index++;
0N/A between = true;
0N/A }
0N/A }
0N/A
0N/A location = new DropLocation(p, index, between);
0N/A
0N/A break;
0N/A default:
0N/A assert false : "Unexpected drop mode";
0N/A }
0N/A
0N/A return location;
0N/A }
0N/A
0N/A /**
0N/A * Called to set or clear the drop location during a DnD operation.
0N/A * In some cases, the component may need to use it's internal selection
0N/A * temporarily to indicate the drop location. To help facilitate this,
0N/A * this method returns and accepts as a parameter a state object.
0N/A * This state object can be used to store, and later restore, the selection
0N/A * state. Whatever this method returns will be passed back to it in
0N/A * future calls, as the state parameter. If it wants the DnD system to
0N/A * continue storing the same state, it must pass it back every time.
0N/A * Here's how this is used:
0N/A * <p>
0N/A * Let's say that on the first call to this method the component decides
0N/A * to save some state (because it is about to use the selection to show
0N/A * a drop index). It can return a state object to the caller encapsulating
0N/A * any saved selection state. On a second call, let's say the drop location
0N/A * is being changed to something else. The component doesn't need to
0N/A * restore anything yet, so it simply passes back the same state object
0N/A * to have the DnD system continue storing it. Finally, let's say this
0N/A * method is messaged with <code>null</code>. This means DnD
0N/A * is finished with this component for now, meaning it should restore
0N/A * state. At this point, it can use the state parameter to restore
0N/A * said state, and of course return <code>null</code> since there's
0N/A * no longer anything to store.
0N/A *
0N/A * @param location the drop location (as calculated by
0N/A * <code>dropLocationForPoint</code>) or <code>null</code>
0N/A * if there's no longer a valid drop location
0N/A * @param state the state object saved earlier for this component,
0N/A * or <code>null</code>
0N/A * @param forDrop whether or not the method is being called because an
0N/A * actual drop occurred
0N/A * @return any saved state for this component, or <code>null</code> if none
0N/A */
0N/A Object setDropLocation(TransferHandler.DropLocation location,
0N/A Object state,
0N/A boolean forDrop) {
0N/A
0N/A Object retVal = null;
0N/A DropLocation listLocation = (DropLocation)location;
0N/A
0N/A if (dropMode == DropMode.USE_SELECTION) {
0N/A if (listLocation == null) {
0N/A if (!forDrop && state != null) {
0N/A setSelectedIndices(((int[][])state)[0]);
0N/A
0N/A int anchor = ((int[][])state)[1][0];
0N/A int lead = ((int[][])state)[1][1];
0N/A
0N/A SwingUtilities2.setLeadAnchorWithoutSelection(
0N/A getSelectionModel(), lead, anchor);
0N/A }
0N/A } else {
0N/A if (dropLocation == null) {
0N/A int[] inds = getSelectedIndices();
0N/A retVal = new int[][] {inds, {getAnchorSelectionIndex(),
0N/A getLeadSelectionIndex()}};
0N/A } else {
0N/A retVal = state;
0N/A }
0N/A
0N/A int index = listLocation.getIndex();
0N/A if (index == -1) {
0N/A clearSelection();
0N/A getSelectionModel().setAnchorSelectionIndex(-1);
0N/A getSelectionModel().setLeadSelectionIndex(-1);
0N/A } else {
0N/A setSelectionInterval(index, index);
0N/A }
0N/A }
0N/A }
0N/A
0N/A DropLocation old = dropLocation;
0N/A dropLocation = listLocation;
0N/A firePropertyChange("dropLocation", old, dropLocation);
0N/A
0N/A return retVal;
0N/A }
0N/A
0N/A /**
0N/A * Returns the location that this component should visually indicate
0N/A * as the drop location during a DnD operation over the component,
0N/A * or {@code null} if no location is to currently be shown.
0N/A * <p>
0N/A * This method is not meant for querying the drop location
0N/A * from a {@code TransferHandler}, as the drop location is only
0N/A * set after the {@code TransferHandler}'s <code>canImport</code>
0N/A * has returned and has allowed for the location to be shown.
0N/A * <p>
0N/A * When this property changes, a property change event with
0N/A * name "dropLocation" is fired by the component.
0N/A * <p>
0N/A * By default, responsibility for listening for changes to this property
0N/A * and indicating the drop location visually lies with the list's
0N/A * {@code ListUI}, which may paint it directly and/or install a cell
0N/A * renderer to do so. Developers wishing to implement custom drop location
0N/A * painting and/or replace the default cell renderer, may need to honor
0N/A * this property.
0N/A *
0N/A * @return the drop location
0N/A * @see #setDropMode
0N/A * @see TransferHandler#canImport(TransferHandler.TransferSupport)
0N/A * @since 1.6
0N/A */
0N/A public final DropLocation getDropLocation() {
0N/A return dropLocation;
0N/A }
0N/A
0N/A /**
0N/A * Returns the next list element whose {@code toString} value
0N/A * starts with the given prefix.
0N/A *
0N/A * @param prefix the string to test for a match
0N/A * @param startIndex the index for starting the search
0N/A * @param bias the search direction, either
0N/A * Position.Bias.Forward or Position.Bias.Backward.
0N/A * @return the index of the next list element that
0N/A * starts with the prefix; otherwise {@code -1}
0N/A * @exception IllegalArgumentException if prefix is {@code null}
0N/A * or startIndex is out of bounds
0N/A * @since 1.4
0N/A */
0N/A public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
1983N/A ListModel<E> model = getModel();
0N/A int max = model.getSize();
0N/A if (prefix == null) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A if (startIndex < 0 || startIndex >= max) {
0N/A throw new IllegalArgumentException();
0N/A }
0N/A prefix = prefix.toUpperCase();
0N/A
0N/A // start search from the next element after the selected element
0N/A int increment = (bias == Position.Bias.Forward) ? 1 : -1;
0N/A int index = startIndex;
0N/A do {
1983N/A E element = model.getElementAt(index);
1983N/A
1983N/A if (element != null) {
0N/A String string;
0N/A
1983N/A if (element instanceof String) {
1983N/A string = ((String)element).toUpperCase();
0N/A }
0N/A else {
1983N/A string = element.toString();
0N/A if (string != null) {
0N/A string = string.toUpperCase();
0N/A }
0N/A }
0N/A
0N/A if (string != null && string.startsWith(prefix)) {
0N/A return index;
0N/A }
0N/A }
0N/A index = (index + increment + max) % max;
0N/A } while (index != startIndex);
0N/A return -1;
0N/A }
0N/A
0N/A /**
0N/A * Returns the tooltip text to be used for the given event. This overrides
0N/A * {@code JComponent}'s {@code getToolTipText} to first check the cell
0N/A * renderer component for the cell over which the event occurred, returning
0N/A * its tooltip text, if any. This implementation allows you to specify
0N/A * tooltip text on the cell level, by using {@code setToolTipText} on your
0N/A * cell renderer component.
0N/A * <p>
0N/A * <bold>Note:</bold> For <code>JList</code> to properly display the
0N/A * tooltips of its renderers in this manner, <code>JList</code> must be a
0N/A * registered component with the <code>ToolTipManager</code>. This registration
0N/A * is done automatically in the constructor. However, if at a later point
0N/A * <code>JList</code> is unregistered, by way of a call to
0N/A * {@code setToolTipText(null)}, tips from the renderers will no longer display.
0N/A *
0N/A * @param event the {@code MouseEvent} to fetch the tooltip text for
0N/A * @see JComponent#setToolTipText
0N/A * @see JComponent#getToolTipText
0N/A */
0N/A public String getToolTipText(MouseEvent event) {
0N/A if(event != null) {
0N/A Point p = event.getPoint();
0N/A int index = locationToIndex(p);
1983N/A ListCellRenderer<? super E> r = getCellRenderer();
0N/A Rectangle cellBounds;
0N/A
0N/A if (index != -1 && r != null && (cellBounds =
0N/A getCellBounds(index, index)) != null &&
0N/A cellBounds.contains(p.x, p.y)) {
0N/A ListSelectionModel lsm = getSelectionModel();
0N/A Component rComponent = r.getListCellRendererComponent(
0N/A this, getModel().getElementAt(index), index,
0N/A lsm.isSelectedIndex(index),
0N/A (hasFocus() && (lsm.getLeadSelectionIndex() ==
0N/A index)));
0N/A
0N/A if(rComponent instanceof JComponent) {
0N/A MouseEvent newEvent;
0N/A
0N/A p.translate(-cellBounds.x, -cellBounds.y);
0N/A newEvent = new MouseEvent(rComponent, event.getID(),
0N/A event.getWhen(),
0N/A event.getModifiers(),
0N/A p.x, p.y,
0N/A event.getXOnScreen(),
0N/A event.getYOnScreen(),
0N/A event.getClickCount(),
0N/A event.isPopupTrigger(),
0N/A MouseEvent.NOBUTTON);
0N/A
0N/A String tip = ((JComponent)rComponent).getToolTipText(
0N/A newEvent);
0N/A
0N/A if (tip != null) {
0N/A return tip;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return super.getToolTipText();
0N/A }
0N/A
0N/A /**
0N/A * --- ListUI Delegations ---
0N/A */
0N/A
0N/A
0N/A /**
0N/A * Returns the cell index closest to the given location in the list's
0N/A * coordinate system. To determine if the cell actually contains the
0N/A * specified location, compare the point against the cell's bounds,
0N/A * as provided by {@code getCellBounds}. This method returns {@code -1}
0N/A * if the model is empty
0N/A * <p>
0N/A * This is a cover method that delegates to the method of the same name
0N/A * in the list's {@code ListUI}. It returns {@code -1} if the list has
0N/A * no {@code ListUI}.
0N/A *
0N/A * @param location the coordinates of the point
0N/A * @return the cell index closest to the given location, or {@code -1}
0N/A */
0N/A public int locationToIndex(Point location) {
0N/A ListUI ui = getUI();
0N/A return (ui != null) ? ui.locationToIndex(this, location) : -1;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the origin of the specified item in the list's coordinate
0N/A * system. This method returns {@code null} if the index isn't valid.
0N/A * <p>
0N/A * This is a cover method that delegates to the method of the same name
0N/A * in the list's {@code ListUI}. It returns {@code null} if the list has
0N/A * no {@code ListUI}.
0N/A *
0N/A * @param index the cell index
0N/A * @return the origin of the cell, or {@code null}
0N/A */
0N/A public Point indexToLocation(int index) {
0N/A ListUI ui = getUI();
0N/A return (ui != null) ? ui.indexToLocation(this, index) : null;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the bounding rectangle, in the list's coordinate system,
0N/A * for the range of cells specified by the two indices.
0N/A * These indices can be supplied in any order.
0N/A * <p>
0N/A * If the smaller index is outside the list's range of cells, this method
0N/A * returns {@code null}. If the smaller index is valid, but the larger
0N/A * index is outside the list's range, the bounds of just the first index
0N/A * is returned. Otherwise, the bounds of the valid range is returned.
0N/A * <p>
0N/A * This is a cover method that delegates to the method of the same name
0N/A * in the list's {@code ListUI}. It returns {@code null} if the list has
0N/A * no {@code ListUI}.
0N/A *
0N/A * @param index0 the first index in the range
0N/A * @param index1 the second index in the range
0N/A * @return the bounding rectangle for the range of cells, or {@code null}
0N/A */
0N/A public Rectangle getCellBounds(int index0, int index1) {
0N/A ListUI ui = getUI();
0N/A return (ui != null) ? ui.getCellBounds(this, index0, index1) : null;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * --- ListModel Support ---
0N/A */
0N/A
0N/A
0N/A /**
0N/A * Returns the data model that holds the list of items displayed
0N/A * by the <code>JList</code> component.
0N/A *
0N/A * @return the <code>ListModel</code> that provides the displayed
0N/A * list of items
0N/A * @see #setModel
0N/A */
1983N/A public ListModel<E> getModel() {
0N/A return dataModel;
0N/A }
0N/A
0N/A /**
0N/A * Sets the model that represents the contents or "value" of the
0N/A * list, notifies property change listeners, and then clears the
0N/A * list's selection.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param model the <code>ListModel</code> that provides the
0N/A * list of items for display
0N/A * @exception IllegalArgumentException if <code>model</code> is
0N/A * <code>null</code>
0N/A * @see #getModel
0N/A * @see #clearSelection
0N/A * @beaninfo
0N/A * bound: true
0N/A * attribute: visualUpdate true
0N/A * description: The object that contains the data to be drawn by this JList.
0N/A */
1983N/A public void setModel(ListModel<E> model) {
0N/A if (model == null) {
0N/A throw new IllegalArgumentException("model must be non null");
0N/A }
1983N/A ListModel<E> oldValue = dataModel;
0N/A dataModel = model;
0N/A firePropertyChange("model", oldValue, dataModel);
0N/A clearSelection();
0N/A }
0N/A
0N/A
0N/A /**
1983N/A * Constructs a read-only <code>ListModel</code> from an array of items,
0N/A * and calls {@code setModel} with this model.
0N/A * <p>
0N/A * Attempts to pass a {@code null} value to this method results in
0N/A * undefined behavior and, most likely, exceptions. The created model
0N/A * references the given array directly. Attempts to modify the array
0N/A * after invoking this method results in undefined behavior.
0N/A *
1983N/A * @param listData an array of {@code E} containing the items to
0N/A * display in the list
0N/A * @see #setModel
0N/A */
1983N/A public void setListData(final E[] listData) {
0N/A setModel (
1983N/A new AbstractListModel<E>() {
0N/A public int getSize() { return listData.length; }
1983N/A public E getElementAt(int i) { return listData[i]; }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Constructs a read-only <code>ListModel</code> from a <code>Vector</code>
0N/A * and calls {@code setModel} with this model.
0N/A * <p>
0N/A * Attempts to pass a {@code null} value to this method results in
0N/A * undefined behavior and, most likely, exceptions. The created model
0N/A * references the given {@code Vector} directly. Attempts to modify the
0N/A * {@code Vector} after invoking this method results in undefined behavior.
0N/A *
0N/A * @param listData a <code>Vector</code> containing the items to
0N/A * display in the list
0N/A * @see #setModel
0N/A */
1983N/A public void setListData(final Vector<? extends E> listData) {
0N/A setModel (
1983N/A new AbstractListModel<E>() {
0N/A public int getSize() { return listData.size(); }
1983N/A public E getElementAt(int i) { return listData.elementAt(i); }
0N/A }
0N/A );
0N/A }
0N/A
0N/A
0N/A /**
0N/A * --- ListSelectionModel delegations and extensions ---
0N/A */
0N/A
0N/A
0N/A /**
0N/A * Returns an instance of {@code DefaultListSelectionModel}; called
0N/A * during construction to initialize the list's selection model
0N/A * property.
0N/A *
0N/A * @return a {@code DefaultListSelecitonModel}, used to initialize
0N/A * the list's selection model property during construction
0N/A * @see #setSelectionModel
0N/A * @see DefaultListSelectionModel
0N/A */
0N/A protected ListSelectionModel createSelectionModel() {
0N/A return new DefaultListSelectionModel();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the current selection model. The selection model maintains the
0N/A * selection state of the list. See the class level documentation for more
0N/A * details.
0N/A *
0N/A * @return the <code>ListSelectionModel</code> that maintains the
0N/A * list's selections
0N/A *
0N/A * @see #setSelectionModel
0N/A * @see ListSelectionModel
0N/A */
0N/A public ListSelectionModel getSelectionModel() {
0N/A return selectionModel;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Notifies {@code ListSelectionListener}s added directly to the list
0N/A * of selection changes made to the selection model. {@code JList}
0N/A * listens for changes made to the selection in the selection model,
0N/A * and forwards notification to listeners added to the list directly,
0N/A * by calling this method.
0N/A * <p>
0N/A * This method constructs a {@code ListSelectionEvent} with this list
0N/A * as the source, and the specified arguments, and sends it to the
0N/A * registered {@code ListSelectionListeners}.
0N/A *
0N/A * @param firstIndex the first index in the range, {@code <= lastIndex}
0N/A * @param lastIndex the last index in the range, {@code >= firstIndex}
0N/A * @param isAdjusting whether or not this is one in a series of
0N/A * multiple events, where changes are still being made
0N/A *
0N/A * @see #addListSelectionListener
0N/A * @see #removeListSelectionListener
0N/A * @see javax.swing.event.ListSelectionEvent
0N/A * @see EventListenerList
0N/A */
0N/A protected void fireSelectionValueChanged(int firstIndex, int lastIndex,
0N/A boolean isAdjusting)
0N/A {
0N/A Object[] listeners = listenerList.getListenerList();
0N/A ListSelectionEvent e = null;
0N/A
0N/A for (int i = listeners.length - 2; i >= 0; i -= 2) {
0N/A if (listeners[i] == ListSelectionListener.class) {
0N/A if (e == null) {
0N/A e = new ListSelectionEvent(this, firstIndex, lastIndex,
0N/A isAdjusting);
0N/A }
0N/A ((ListSelectionListener)listeners[i+1]).valueChanged(e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /* A ListSelectionListener that forwards ListSelectionEvents from
0N/A * the selectionModel to the JList ListSelectionListeners. The
0N/A * forwarded events only differ from the originals in that their
0N/A * source is the JList instead of the selectionModel itself.
0N/A */
0N/A private class ListSelectionHandler implements ListSelectionListener, Serializable
0N/A {
0N/A public void valueChanged(ListSelectionEvent e) {
0N/A fireSelectionValueChanged(e.getFirstIndex(),
0N/A e.getLastIndex(),
0N/A e.getValueIsAdjusting());
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Adds a listener to the list, to be notified each time a change to the
0N/A * selection occurs; the preferred way of listening for selection state
0N/A * changes. {@code JList} takes care of listening for selection state
0N/A * changes in the selection model, and notifies the given listener of
0N/A * each change. {@code ListSelectionEvent}s sent to the listener have a
0N/A * {@code source} property set to this list.
0N/A *
0N/A * @param listener the {@code ListSelectionListener} to add
0N/A * @see #getSelectionModel
0N/A * @see #getListSelectionListeners
0N/A */
0N/A public void addListSelectionListener(ListSelectionListener listener)
0N/A {
0N/A if (selectionListener == null) {
0N/A selectionListener = new ListSelectionHandler();
0N/A getSelectionModel().addListSelectionListener(selectionListener);
0N/A }
0N/A
0N/A listenerList.add(ListSelectionListener.class, listener);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes a selection listener from the list.
0N/A *
0N/A * @param listener the {@code ListSelectionListener} to remove
0N/A * @see #addListSelectionListener
0N/A * @see #getSelectionModel
0N/A */
0N/A public void removeListSelectionListener(ListSelectionListener listener) {
0N/A listenerList.remove(ListSelectionListener.class, listener);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of all the {@code ListSelectionListener}s added
0N/A * to this {@code JList} by way of {@code addListSelectionListener}.
0N/A *
0N/A * @return all of the {@code ListSelectionListener}s on this list, or
0N/A * an empty array if no listeners have been added
0N/A * @see #addListSelectionListener
0N/A * @since 1.4
0N/A */
0N/A public ListSelectionListener[] getListSelectionListeners() {
625N/A return listenerList.getListeners(ListSelectionListener.class);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the <code>selectionModel</code> for the list to a
0N/A * non-<code>null</code> <code>ListSelectionModel</code>
0N/A * implementation. The selection model handles the task of making single
0N/A * selections, selections of contiguous ranges, and non-contiguous
0N/A * selections.
0N/A * <p>
0N/A * This is a JavaBeans bound property.
0N/A *
0N/A * @param selectionModel the <code>ListSelectionModel</code> that
0N/A * implements the selections
0N/A * @exception IllegalArgumentException if <code>selectionModel</code>
0N/A * is <code>null</code>
0N/A * @see #getSelectionModel
0N/A * @beaninfo
0N/A * bound: true
0N/A * description: The selection model, recording which cells are selected.
0N/A */
0N/A public void setSelectionModel(ListSelectionModel selectionModel) {
0N/A if (selectionModel == null) {
0N/A throw new IllegalArgumentException("selectionModel must be non null");
0N/A }
0N/A
0N/A /* Remove the forwarding ListSelectionListener from the old
0N/A * selectionModel, and add it to the new one, if necessary.
0N/A */
0N/A if (selectionListener != null) {
0N/A this.selectionModel.removeListSelectionListener(selectionListener);
0N/A selectionModel.addListSelectionListener(selectionListener);
0N/A }
0N/A
0N/A ListSelectionModel oldValue = this.selectionModel;
0N/A this.selectionModel = selectionModel;
0N/A firePropertyChange("selectionModel", oldValue, selectionModel);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the selection mode for the list. This is a cover method that sets
0N/A * the selection mode directly on the selection model.
0N/A * <p>
0N/A * The following list describes the accepted selection modes:
0N/A * <ul>
0N/A * <li>{@code ListSelectionModel.SINGLE_SELECTION} -
0N/A * Only one list index can be selected at a time. In this mode,
0N/A * {@code setSelectionInterval} and {@code addSelectionInterval} are
0N/A * equivalent, both replacing the current selection with the index
0N/A * represented by the second argument (the "lead").
0N/A * <li>{@code ListSelectionModel.SINGLE_INTERVAL_SELECTION} -
0N/A * Only one contiguous interval can be selected at a time.
0N/A * In this mode, {@code addSelectionInterval} behaves like
0N/A * {@code setSelectionInterval} (replacing the current selection},
0N/A * unless the given interval is immediately adjacent to or overlaps
0N/A * the existing selection, and can be used to grow the selection.
0N/A * <li>{@code ListSelectionModel.MULTIPLE_INTERVAL_SELECTION} -
0N/A * In this mode, there's no restriction on what can be selected.
0N/A * This mode is the default.
0N/A * </ul>
0N/A *
0N/A * @param selectionMode the selection mode
0N/A * @see #getSelectionMode
0N/A * @throws IllegalArgumentException if the selection mode isn't
0N/A * one of those allowed
0N/A * @beaninfo
0N/A * description: The selection mode.
0N/A * enum: SINGLE_SELECTION ListSelectionModel.SINGLE_SELECTION
0N/A * SINGLE_INTERVAL_SELECTION ListSelectionModel.SINGLE_INTERVAL_SELECTION
0N/A * MULTIPLE_INTERVAL_SELECTION ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
0N/A */
0N/A public void setSelectionMode(int selectionMode) {
0N/A getSelectionModel().setSelectionMode(selectionMode);
0N/A }
0N/A
0N/A /**
0N/A * Returns the current selection mode for the list. This is a cover
0N/A * method that delegates to the method of the same name on the
0N/A * list's selection model.
0N/A *
0N/A * @return the current selection mode
0N/A * @see #setSelectionMode
0N/A */
0N/A public int getSelectionMode() {
0N/A return getSelectionModel().getSelectionMode();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the anchor selection index. This is a cover method that
0N/A * delegates to the method of the same name on the list's selection model.
0N/A *
0N/A * @return the anchor selection index
0N/A * @see ListSelectionModel#getAnchorSelectionIndex
0N/A */
0N/A public int getAnchorSelectionIndex() {
0N/A return getSelectionModel().getAnchorSelectionIndex();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the lead selection index. This is a cover method that
0N/A * delegates to the method of the same name on the list's selection model.
0N/A *
0N/A * @return the lead selection index
0N/A * @see ListSelectionModel#getLeadSelectionIndex
0N/A * @beaninfo
0N/A * description: The lead selection index.
0N/A */
0N/A public int getLeadSelectionIndex() {
0N/A return getSelectionModel().getLeadSelectionIndex();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the smallest selected cell index, or {@code -1} if the selection
0N/A * is empty. This is a cover method that delegates to the method of the same
0N/A * name on the list's selection model.
0N/A *
0N/A * @return the smallest selected cell index, or {@code -1}
0N/A * @see ListSelectionModel#getMinSelectionIndex
0N/A */
0N/A public int getMinSelectionIndex() {
0N/A return getSelectionModel().getMinSelectionIndex();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the largest selected cell index, or {@code -1} if the selection
0N/A * is empty. This is a cover method that delegates to the method of the same
0N/A * name on the list's selection model.
0N/A *
0N/A * @return the largest selected cell index
0N/A * @see ListSelectionModel#getMaxSelectionIndex
0N/A */
0N/A public int getMaxSelectionIndex() {
0N/A return getSelectionModel().getMaxSelectionIndex();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns {@code true} if the specified index is selected,
0N/A * else {@code false}. This is a cover method that delegates to the method
0N/A * of the same name on the list's selection model.
0N/A *
0N/A * @param index index to be queried for selection state
0N/A * @return {@code true} if the specified index is selected,
0N/A * else {@code false}
0N/A * @see ListSelectionModel#isSelectedIndex
0N/A * @see #setSelectedIndex
0N/A */
0N/A public boolean isSelectedIndex(int index) {
0N/A return getSelectionModel().isSelectedIndex(index);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns {@code true} if nothing is selected, else {@code false}.
0N/A * This is a cover method that delegates to the method of the same
0N/A * name on the list's selection model.
0N/A *
0N/A * @return {@code true} if nothing is selected, else {@code false}
0N/A * @see ListSelectionModel#isSelectionEmpty
0N/A * @see #clearSelection
0N/A */
0N/A public boolean isSelectionEmpty() {
0N/A return getSelectionModel().isSelectionEmpty();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Clears the selection; after calling this method, {@code isSelectionEmpty}
0N/A * will return {@code true}. This is a cover method that delegates to the
0N/A * method of the same name on the list's selection model.
0N/A *
0N/A * @see ListSelectionModel#clearSelection
0N/A * @see #isSelectionEmpty
0N/A */
0N/A public void clearSelection() {
0N/A getSelectionModel().clearSelection();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Selects the specified interval. Both {@code anchor} and {@code lead}
0N/A * indices are included. {@code anchor} doesn't have to be less than or
0N/A * equal to {@code lead}. This is a cover method that delegates to the
0N/A * method of the same name on the list's selection model.
0N/A * <p>
0N/A * Refer to the documentation of the selection model class being used
0N/A * for details on how values less than {@code 0} are handled.
0N/A *
0N/A * @param anchor the first index to select
0N/A * @param lead the last index to select
0N/A * @see ListSelectionModel#setSelectionInterval
0N/A * @see DefaultListSelectionModel#setSelectionInterval
0N/A * @see #createSelectionModel
0N/A * @see #addSelectionInterval
0N/A * @see #removeSelectionInterval
0N/A */
0N/A public void setSelectionInterval(int anchor, int lead) {
0N/A getSelectionModel().setSelectionInterval(anchor, lead);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the selection to be the union of the specified interval with current
0N/A * selection. Both the {@code anchor} and {@code lead} indices are
0N/A * included. {@code anchor} doesn't have to be less than or
0N/A * equal to {@code lead}. This is a cover method that delegates to the
0N/A * method of the same name on the list's selection model.
0N/A * <p>
0N/A * Refer to the documentation of the selection model class being used
0N/A * for details on how values less than {@code 0} are handled.
0N/A *
0N/A * @param anchor the first index to add to the selection
0N/A * @param lead the last index to add to the selection
0N/A * @see ListSelectionModel#addSelectionInterval
0N/A * @see DefaultListSelectionModel#addSelectionInterval
0N/A * @see #createSelectionModel
0N/A * @see #setSelectionInterval
0N/A * @see #removeSelectionInterval
0N/A */
0N/A public void addSelectionInterval(int anchor, int lead) {
0N/A getSelectionModel().addSelectionInterval(anchor, lead);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the selection to be the set difference of the specified interval
0N/A * and the current selection. Both the {@code index0} and {@code index1}
0N/A * indices are removed. {@code index0} doesn't have to be less than or
0N/A * equal to {@code index1}. This is a cover method that delegates to the
0N/A * method of the same name on the list's selection model.
0N/A * <p>
0N/A * Refer to the documentation of the selection model class being used
0N/A * for details on how values less than {@code 0} are handled.
0N/A *
0N/A * @param index0 the first index to remove from the selection
0N/A * @param index1 the last index to remove from the selection
0N/A * @see ListSelectionModel#removeSelectionInterval
0N/A * @see DefaultListSelectionModel#removeSelectionInterval
0N/A * @see #createSelectionModel
0N/A * @see #setSelectionInterval
0N/A * @see #addSelectionInterval
0N/A */
0N/A public void removeSelectionInterval(int index0, int index1) {
0N/A getSelectionModel().removeSelectionInterval(index0, index1);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Sets the selection model's {@code valueIsAdjusting} property. When
0N/A * {@code true}, upcoming changes to selection should be considered part
0N/A * of a single change. This property is used internally and developers
0N/A * typically need not call this method. For example, when the model is being
0N/A * updated in response to a user drag, the value of the property is set
0N/A * to {@code true} when the drag is initiated and set to {@code false}
0N/A * when the drag is finished. This allows listeners to update only
0N/A * when a change has been finalized, rather than handling all of the
0N/A * intermediate values.
0N/A * <p>
0N/A * You may want to use this directly if making a series of changes
0N/A * that should be considered part of a single change.
0N/A * <p>
0N/A * This is a cover method that delegates to the method of the same name on
0N/A * the list's selection model. See the documentation for
0N/A * {@link javax.swing.ListSelectionModel#setValueIsAdjusting} for
0N/A * more details.
0N/A *
0N/A * @param b the new value for the property
0N/A * @see ListSelectionModel#setValueIsAdjusting
0N/A * @see javax.swing.event.ListSelectionEvent#getValueIsAdjusting
0N/A * @see #getValueIsAdjusting
0N/A */
0N/A public void setValueIsAdjusting(boolean b) {
0N/A getSelectionModel().setValueIsAdjusting(b);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the value of the selection model's {@code isAdjusting} property.
0N/A * <p>
0N/A * This is a cover method that delegates to the method of the same name on
0N/A * the list's selection model.
0N/A *
0N/A * @return the value of the selection model's {@code isAdjusting} property.
0N/A *
0N/A * @see #setValueIsAdjusting
0N/A * @see ListSelectionModel#getValueIsAdjusting
0N/A */
0N/A public boolean getValueIsAdjusting() {
0N/A return getSelectionModel().getValueIsAdjusting();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of all of the selected indices, in increasing
0N/A * order.
0N/A *
0N/A * @return all of the selected indices, in increasing order,
0N/A * or an empty array if nothing is selected
0N/A * @see #removeSelectionInterval
0N/A * @see #addListSelectionListener
0N/A */
243N/A @Transient
0N/A public int[] getSelectedIndices() {
0N/A ListSelectionModel sm = getSelectionModel();
0N/A int iMin = sm.getMinSelectionIndex();
0N/A int iMax = sm.getMaxSelectionIndex();
0N/A
0N/A if ((iMin < 0) || (iMax < 0)) {
0N/A return new int[0];
0N/A }
0N/A
0N/A int[] rvTmp = new int[1+ (iMax - iMin)];
0N/A int n = 0;
0N/A for(int i = iMin; i <= iMax; i++) {
0N/A if (sm.isSelectedIndex(i)) {
0N/A rvTmp[n++] = i;
0N/A }
0N/A }
0N/A int[] rv = new int[n];
0N/A System.arraycopy(rvTmp, 0, rv, 0, n);
0N/A return rv;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Selects a single cell. Does nothing if the given index is greater
0N/A * than or equal to the model size. This is a convenience method that uses
0N/A * {@code setSelectionInterval} on the selection model. Refer to the
0N/A * documentation for the selection model class being used for details on
0N/A * how values less than {@code 0} are handled.
0N/A *
0N/A * @param index the index of the cell to select
0N/A * @see ListSelectionModel#setSelectionInterval
0N/A * @see #isSelectedIndex
0N/A * @see #addListSelectionListener
0N/A * @beaninfo
0N/A * description: The index of the selected cell.
0N/A */
0N/A public void setSelectedIndex(int index) {
0N/A if (index >= getModel().getSize()) {
0N/A return;
0N/A }
0N/A getSelectionModel().setSelectionInterval(index, index);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Changes the selection to be the set of indices specified by the given
0N/A * array. Indices greater than or equal to the model size are ignored.
0N/A * This is a convenience method that clears the selection and then uses
0N/A * {@code addSelectionInterval} on the selection model to add the indices.
0N/A * Refer to the documentation of the selection model class being used for
0N/A * details on how values less than {@code 0} are handled.
0N/A *
0N/A * @param indices an array of the indices of the cells to select,
0N/A * {@code non-null}
0N/A * @see ListSelectionModel#addSelectionInterval
0N/A * @see #isSelectedIndex
0N/A * @see #addListSelectionListener
0N/A * @throws NullPointerException if the given array is {@code null}
0N/A */
0N/A public void setSelectedIndices(int[] indices) {
0N/A ListSelectionModel sm = getSelectionModel();
0N/A sm.clearSelection();
0N/A int size = getModel().getSize();
625N/A for (int i : indices) {
625N/A if (i < size) {
625N/A sm.addSelectionInterval(i, i);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns an array of all the selected values, in increasing order based
0N/A * on their indices in the list.
0N/A *
0N/A * @return the selected values, or an empty array if nothing is selected
0N/A * @see #isSelectedIndex
0N/A * @see #getModel
0N/A * @see #addListSelectionListener
1983N/A *
1983N/A * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
0N/A */
1983N/A @Deprecated
0N/A public Object[] getSelectedValues() {
0N/A ListSelectionModel sm = getSelectionModel();
1983N/A ListModel<E> dm = getModel();
0N/A
0N/A int iMin = sm.getMinSelectionIndex();
0N/A int iMax = sm.getMaxSelectionIndex();
0N/A
0N/A if ((iMin < 0) || (iMax < 0)) {
0N/A return new Object[0];
0N/A }
0N/A
0N/A Object[] rvTmp = new Object[1+ (iMax - iMin)];
0N/A int n = 0;
0N/A for(int i = iMin; i <= iMax; i++) {
0N/A if (sm.isSelectedIndex(i)) {
0N/A rvTmp[n++] = dm.getElementAt(i);
0N/A }
0N/A }
0N/A Object[] rv = new Object[n];
0N/A System.arraycopy(rvTmp, 0, rv, 0, n);
0N/A return rv;
0N/A }
0N/A
1983N/A /**
1983N/A * Returns a list of all the selected items, in increasing order based
1983N/A * on their indices in the list.
1983N/A *
1983N/A * @return the selected items, or an empty list if nothing is selected
1983N/A * @see #isSelectedIndex
1983N/A * @see #getModel
1983N/A * @see #addListSelectionListener
1983N/A *
1983N/A * @since 1.7
1983N/A */
1983N/A public List<E> getSelectedValuesList() {
1983N/A ListSelectionModel sm = getSelectionModel();
1983N/A ListModel<E> dm = getModel();
1983N/A
1983N/A int iMin = sm.getMinSelectionIndex();
1983N/A int iMax = sm.getMaxSelectionIndex();
1983N/A
1983N/A if ((iMin < 0) || (iMax < 0)) {
1983N/A return Collections.emptyList();
1983N/A }
1983N/A
1983N/A List<E> selectedItems = new ArrayList<E>();
1983N/A for(int i = iMin; i <= iMax; i++) {
1983N/A if (sm.isSelectedIndex(i)) {
1983N/A selectedItems.add(dm.getElementAt(i));
1983N/A }
1983N/A }
1983N/A return selectedItems;
1983N/A }
1983N/A
0N/A
0N/A /**
0N/A * Returns the smallest selected cell index; <i>the selection</i> when only
0N/A * a single item is selected in the list. When multiple items are selected,
0N/A * it is simply the smallest selected index. Returns {@code -1} if there is
0N/A * no selection.
0N/A * <p>
0N/A * This method is a cover that delegates to {@code getMinSelectionIndex}.
0N/A *
0N/A * @return the smallest selected cell index
0N/A * @see #getMinSelectionIndex
0N/A * @see #addListSelectionListener
0N/A */
0N/A public int getSelectedIndex() {
0N/A return getMinSelectionIndex();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the value for the smallest selected cell index;
0N/A * <i>the selected value</i> when only a single item is selected in the
0N/A * list. When multiple items are selected, it is simply the value for the
0N/A * smallest selected index. Returns {@code null} if there is no selection.
0N/A * <p>
0N/A * This is a convenience method that simply returns the model value for
0N/A * {@code getMinSelectionIndex}.
0N/A *
0N/A * @return the first selected value
0N/A * @see #getMinSelectionIndex
0N/A * @see #getModel
0N/A * @see #addListSelectionListener
0N/A */
1983N/A public E getSelectedValue() {
0N/A int i = getMinSelectionIndex();
0N/A return (i == -1) ? null : getModel().getElementAt(i);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Selects the specified object from the list.
0N/A *
0N/A * @param anObject the object to select
0N/A * @param shouldScroll {@code true} if the list should scroll to display
0N/A * the selected object, if one exists; otherwise {@code false}
0N/A */
0N/A public void setSelectedValue(Object anObject,boolean shouldScroll) {
0N/A if(anObject == null)
0N/A setSelectedIndex(-1);
0N/A else if(!anObject.equals(getSelectedValue())) {
0N/A int i,c;
1983N/A ListModel<E> dm = getModel();
0N/A for(i=0,c=dm.getSize();i<c;i++)
0N/A if(anObject.equals(dm.getElementAt(i))){
0N/A setSelectedIndex(i);
0N/A if(shouldScroll)
0N/A ensureIndexIsVisible(i);
0N/A repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
0N/A return;
0N/A }
0N/A setSelectedIndex(-1);
0N/A }
0N/A repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
0N/A }
0N/A
0N/A
0N/A
0N/A /**
0N/A * --- The Scrollable Implementation ---
0N/A */
0N/A
0N/A private void checkScrollableParameters(Rectangle visibleRect, int orientation) {
0N/A if (visibleRect == null) {
0N/A throw new IllegalArgumentException("visibleRect must be non-null");
0N/A }
0N/A switch (orientation) {
0N/A case SwingConstants.VERTICAL:
0N/A case SwingConstants.HORIZONTAL:
0N/A break;
0N/A default:
0N/A throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Computes the size of viewport needed to display {@code visibleRowCount}
0N/A * rows. The value returned by this method depends on the layout
0N/A * orientation:
0N/A * <p>
0N/A * <b>{@code VERTICAL}:</b>
0N/A * <br>
0N/A * This is trivial if both {@code fixedCellWidth} and {@code fixedCellHeight}
0N/A * have been set (either explicitly or by specifying a prototype cell value).
0N/A * The width is simply the {@code fixedCellWidth} plus the list's horizontal
0N/A * insets. The height is the {@code fixedCellHeight} multiplied by the
0N/A * {@code visibleRowCount}, plus the list's vertical insets.
0N/A * <p>
0N/A * If either {@code fixedCellWidth} or {@code fixedCellHeight} haven't been
0N/A * specified, heuristics are used. If the model is empty, the width is
0N/A * the {@code fixedCellWidth}, if greater than {@code 0}, or a hard-coded
0N/A * value of {@code 256}. The height is the {@code fixedCellHeight} multiplied
0N/A * by {@code visibleRowCount}, if {@code fixedCellHeight} is greater than
0N/A * {@code 0}, otherwise it is a hard-coded value of {@code 16} multiplied by
0N/A * {@code visibleRowCount}.
0N/A * <p>
0N/A * If the model isn't empty, the width is the preferred size's width,
0N/A * typically the width of the widest list element. The height is the
0N/A * {@code fixedCellHeight} multiplied by the {@code visibleRowCount},
0N/A * plus the list's vertical insets.
0N/A * <p>
0N/A * <b>{@code VERTICAL_WRAP} or {@code HORIZONTAL_WRAP}:</b>
0N/A * <br>
0N/A * This method simply returns the value from {@code getPreferredSize}.
0N/A * The list's {@code ListUI} is expected to override {@code getPreferredSize}
0N/A * to return an appropriate value.
0N/A *
0N/A * @return a dimension containing the size of the viewport needed
0N/A * to display {@code visibleRowCount} rows
0N/A * @see #getPreferredScrollableViewportSize
0N/A * @see #setPrototypeCellValue
0N/A */
0N/A public Dimension getPreferredScrollableViewportSize()
0N/A {
0N/A if (getLayoutOrientation() != VERTICAL) {
0N/A return getPreferredSize();
0N/A }
0N/A Insets insets = getInsets();
0N/A int dx = insets.left + insets.right;
0N/A int dy = insets.top + insets.bottom;
0N/A
0N/A int visibleRowCount = getVisibleRowCount();
0N/A int fixedCellWidth = getFixedCellWidth();
0N/A int fixedCellHeight = getFixedCellHeight();
0N/A
0N/A if ((fixedCellWidth > 0) && (fixedCellHeight > 0)) {
0N/A int width = fixedCellWidth + dx;
0N/A int height = (visibleRowCount * fixedCellHeight) + dy;
0N/A return new Dimension(width, height);
0N/A }
0N/A else if (getModel().getSize() > 0) {
0N/A int width = getPreferredSize().width;
0N/A int height;
0N/A Rectangle r = getCellBounds(0, 0);
0N/A if (r != null) {
0N/A height = (visibleRowCount * r.height) + dy;
0N/A }
0N/A else {
0N/A // Will only happen if UI null, shouldn't matter what we return
0N/A height = 1;
0N/A }
0N/A return new Dimension(width, height);
0N/A }
0N/A else {
0N/A fixedCellWidth = (fixedCellWidth > 0) ? fixedCellWidth : 256;
0N/A fixedCellHeight = (fixedCellHeight > 0) ? fixedCellHeight : 16;
0N/A return new Dimension(fixedCellWidth, fixedCellHeight * visibleRowCount);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the distance to scroll to expose the next or previous
0N/A * row (for vertical scrolling) or column (for horizontal scrolling).
0N/A * <p>
0N/A * For horizontal scrolling, if the layout orientation is {@code VERTICAL},
0N/A * then the list's font size is returned (or {@code 1} if the font is
0N/A * {@code null}).
0N/A *
0N/A * @param visibleRect the view area visible within the viewport
0N/A * @param orientation {@code SwingConstants.HORIZONTAL} or
0N/A * {@code SwingConstants.VERTICAL}
0N/A * @param direction less or equal to zero to scroll up/back,
0N/A * greater than zero for down/forward
0N/A * @return the "unit" increment for scrolling in the specified direction;
0N/A * always positive
0N/A * @see #getScrollableBlockIncrement
0N/A * @see Scrollable#getScrollableUnitIncrement
0N/A * @throws IllegalArgumentException if {@code visibleRect} is {@code null}, or
0N/A * {@code orientation} isn't one of {@code SwingConstants.VERTICAL} or
0N/A * {@code SwingConstants.HORIZONTAL}
0N/A */
0N/A public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
0N/A {
0N/A checkScrollableParameters(visibleRect, orientation);
0N/A
0N/A if (orientation == SwingConstants.VERTICAL) {
0N/A int row = locationToIndex(visibleRect.getLocation());
0N/A
0N/A if (row == -1) {
0N/A return 0;
0N/A }
0N/A else {
0N/A /* Scroll Down */
0N/A if (direction > 0) {
0N/A Rectangle r = getCellBounds(row, row);
0N/A return (r == null) ? 0 : r.height - (visibleRect.y - r.y);
0N/A }
0N/A /* Scroll Up */
0N/A else {
0N/A Rectangle r = getCellBounds(row, row);
0N/A
0N/A /* The first row is completely visible and it's row 0.
0N/A * We're done.
0N/A */
0N/A if ((r.y == visibleRect.y) && (row == 0)) {
0N/A return 0;
0N/A }
0N/A /* The first row is completely visible, return the
0N/A * height of the previous row or 0 if the first row
0N/A * is the top row of the list.
0N/A */
0N/A else if (r.y == visibleRect.y) {
0N/A Point loc = r.getLocation();
0N/A loc.y--;
0N/A int prevIndex = locationToIndex(loc);
0N/A Rectangle prevR = getCellBounds(prevIndex, prevIndex);
0N/A
0N/A if (prevR == null || prevR.y >= r.y) {
0N/A return 0;
0N/A }
0N/A return prevR.height;
0N/A }
0N/A /* The first row is partially visible, return the
0N/A * height of hidden part.
0N/A */
0N/A else {
0N/A return visibleRect.y - r.y;
0N/A }
0N/A }
0N/A }
0N/A } else if (orientation == SwingConstants.HORIZONTAL &&
0N/A getLayoutOrientation() != JList.VERTICAL) {
0N/A boolean leftToRight = getComponentOrientation().isLeftToRight();
0N/A int index;
0N/A Point leadingPoint;
0N/A
0N/A if (leftToRight) {
0N/A leadingPoint = visibleRect.getLocation();
0N/A }
0N/A else {
0N/A leadingPoint = new Point(visibleRect.x + visibleRect.width -1,
0N/A visibleRect.y);
0N/A }
0N/A index = locationToIndex(leadingPoint);
0N/A
0N/A if (index != -1) {
0N/A Rectangle cellBounds = getCellBounds(index, index);
0N/A if (cellBounds != null && cellBounds.contains(leadingPoint)) {
0N/A int leadingVisibleEdge;
0N/A int leadingCellEdge;
0N/A
0N/A if (leftToRight) {
0N/A leadingVisibleEdge = visibleRect.x;
0N/A leadingCellEdge = cellBounds.x;
0N/A }
0N/A else {
0N/A leadingVisibleEdge = visibleRect.x + visibleRect.width;
0N/A leadingCellEdge = cellBounds.x + cellBounds.width;
0N/A }
0N/A
0N/A if (leadingCellEdge != leadingVisibleEdge) {
0N/A if (direction < 0) {
0N/A // Show remainder of leading cell
0N/A return Math.abs(leadingVisibleEdge - leadingCellEdge);
0N/A
0N/A }
0N/A else if (leftToRight) {
0N/A // Hide rest of leading cell
0N/A return leadingCellEdge + cellBounds.width - leadingVisibleEdge;
0N/A }
0N/A else {
0N/A // Hide rest of leading cell
0N/A return leadingVisibleEdge - cellBounds.x;
0N/A }
0N/A }
0N/A // ASSUME: All cells are the same width
0N/A return cellBounds.width;
0N/A }
0N/A }
0N/A }
0N/A Font f = getFont();
0N/A return (f != null) ? f.getSize() : 1;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the distance to scroll to expose the next or previous block.
0N/A * <p>
0N/A * For vertical scrolling, the following rules are used:
0N/A * <ul>
0N/A * <li>if scrolling down, returns the distance to scroll so that the last
0N/A * visible element becomes the first completely visible element
0N/A * <li>if scrolling up, returns the distance to scroll so that the first
0N/A * visible element becomes the last completely visible element
0N/A * <li>returns {@code visibleRect.height} if the list is empty
0N/A * </ul>
0N/A * <p>
0N/A * For horizontal scrolling, when the layout orientation is either
0N/A * {@code VERTICAL_WRAP} or {@code HORIZONTAL_WRAP}:
0N/A * <ul>
0N/A * <li>if scrolling right, returns the distance to scroll so that the
0N/A * last visible element becomes
0N/A * the first completely visible element
0N/A * <li>if scrolling left, returns the distance to scroll so that the first
0N/A * visible element becomes the last completely visible element
0N/A * <li>returns {@code visibleRect.width} if the list is empty
0N/A * </ul>
0N/A * <p>
0N/A * For horizontal scrolling and {@code VERTICAL} orientation,
0N/A * returns {@code visibleRect.width}.
0N/A * <p>
0N/A * Note that the value of {@code visibleRect} must be the equal to
0N/A * {@code this.getVisibleRect()}.
0N/A *
0N/A * @param visibleRect the view area visible within the viewport
0N/A * @param orientation {@code SwingConstants.HORIZONTAL} or
0N/A * {@code SwingConstants.VERTICAL}
0N/A * @param direction less or equal to zero to scroll up/back,
0N/A * greater than zero for down/forward
0N/A * @return the "block" increment for scrolling in the specified direction;
0N/A * always positive
0N/A * @see #getScrollableUnitIncrement
0N/A * @see Scrollable#getScrollableBlockIncrement
0N/A * @throws IllegalArgumentException if {@code visibleRect} is {@code null}, or
0N/A * {@code orientation} isn't one of {@code SwingConstants.VERTICAL} or
0N/A * {@code SwingConstants.HORIZONTAL}
0N/A */
0N/A public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
0N/A checkScrollableParameters(visibleRect, orientation);
0N/A if (orientation == SwingConstants.VERTICAL) {
0N/A int inc = visibleRect.height;
0N/A /* Scroll Down */
0N/A if (direction > 0) {
0N/A // last cell is the lowest left cell
0N/A int last = locationToIndex(new Point(visibleRect.x, visibleRect.y+visibleRect.height-1));
0N/A if (last != -1) {
0N/A Rectangle lastRect = getCellBounds(last,last);
0N/A if (lastRect != null) {
0N/A inc = lastRect.y - visibleRect.y;
0N/A if ( (inc == 0) && (last < getModel().getSize()-1) ) {
0N/A inc = lastRect.height;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A /* Scroll Up */
0N/A else {
0N/A int newFirst = locationToIndex(new Point(visibleRect.x, visibleRect.y-visibleRect.height));
0N/A int first = getFirstVisibleIndex();
0N/A if (newFirst != -1) {
0N/A if (first == -1) {
0N/A first = locationToIndex(visibleRect.getLocation());
0N/A }
0N/A Rectangle newFirstRect = getCellBounds(newFirst,newFirst);
0N/A Rectangle firstRect = getCellBounds(first,first);
0N/A if ((newFirstRect != null) && (firstRect!=null)) {
0N/A while ( (newFirstRect.y + visibleRect.height <
0N/A firstRect.y + firstRect.height) &&
0N/A (newFirstRect.y < firstRect.y) ) {
0N/A newFirst++;
0N/A newFirstRect = getCellBounds(newFirst,newFirst);
0N/A }
0N/A inc = visibleRect.y - newFirstRect.y;
0N/A if ( (inc <= 0) && (newFirstRect.y > 0)) {
0N/A newFirst--;
0N/A newFirstRect = getCellBounds(newFirst,newFirst);
0N/A if (newFirstRect != null) {
0N/A inc = visibleRect.y - newFirstRect.y;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return inc;
0N/A }
0N/A else if (orientation == SwingConstants.HORIZONTAL &&
0N/A getLayoutOrientation() != JList.VERTICAL) {
0N/A boolean leftToRight = getComponentOrientation().isLeftToRight();
0N/A int inc = visibleRect.width;
0N/A /* Scroll Right (in ltr mode) or Scroll Left (in rtl mode) */
0N/A if (direction > 0) {
0N/A // position is upper right if ltr, or upper left otherwise
0N/A int x = visibleRect.x + (leftToRight ? (visibleRect.width - 1) : 0);
0N/A int last = locationToIndex(new Point(x, visibleRect.y));
0N/A
0N/A if (last != -1) {
0N/A Rectangle lastRect = getCellBounds(last,last);
0N/A if (lastRect != null) {
0N/A if (leftToRight) {
0N/A inc = lastRect.x - visibleRect.x;
0N/A } else {
0N/A inc = visibleRect.x + visibleRect.width
0N/A - (lastRect.x + lastRect.width);
0N/A }
0N/A if (inc < 0) {
0N/A inc += lastRect.width;
0N/A } else if ( (inc == 0) && (last < getModel().getSize()-1) ) {
0N/A inc = lastRect.width;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A /* Scroll Left (in ltr mode) or Scroll Right (in rtl mode) */
0N/A else {
0N/A // position is upper left corner of the visibleRect shifted
0N/A // left by the visibleRect.width if ltr, or upper right shifted
0N/A // right by the visibleRect.width otherwise
0N/A int x = visibleRect.x + (leftToRight
0N/A ? -visibleRect.width
0N/A : visibleRect.width - 1 + visibleRect.width);
0N/A int first = locationToIndex(new Point(x, visibleRect.y));
0N/A
0N/A if (first != -1) {
0N/A Rectangle firstRect = getCellBounds(first,first);
0N/A if (firstRect != null) {
0N/A // the right of the first cell
0N/A int firstRight = firstRect.x + firstRect.width;
0N/A
0N/A if (leftToRight) {
0N/A if ((firstRect.x < visibleRect.x - visibleRect.width)
0N/A && (firstRight < visibleRect.x)) {
0N/A inc = visibleRect.x - firstRight;
0N/A } else {
0N/A inc = visibleRect.x - firstRect.x;
0N/A }
0N/A } else {
0N/A int visibleRight = visibleRect.x + visibleRect.width;
0N/A
0N/A if ((firstRight > visibleRight + visibleRect.width)
0N/A && (firstRect.x > visibleRight)) {
0N/A inc = firstRect.x - visibleRight;
0N/A } else {
0N/A inc = firstRight - visibleRight;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return inc;
0N/A }
0N/A return visibleRect.width;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code JList} is displayed in a
0N/A * {@code JViewport} and the viewport is wider than the list's
0N/A * preferred width, or if the layout orientation is {@code HORIZONTAL_WRAP}
0N/A * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
0N/A * <p>
0N/A * If {@code false}, then don't track the viewport's width. This allows
0N/A * horizontal scrolling if the {@code JViewport} is itself embedded in a
0N/A * {@code JScrollPane}.
0N/A *
0N/A * @return whether or not an enclosing viewport should force the list's
0N/A * width to match its own
0N/A * @see Scrollable#getScrollableTracksViewportWidth
0N/A */
0N/A public boolean getScrollableTracksViewportWidth() {
0N/A if (getLayoutOrientation() == HORIZONTAL_WRAP &&
0N/A getVisibleRowCount() <= 0) {
0N/A return true;
0N/A }
2333N/A Container parent = SwingUtilities.getUnwrappedParent(this);
2333N/A if (parent instanceof JViewport) {
2333N/A return parent.getWidth() > getPreferredSize().width;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns {@code true} if this {@code JList} is displayed in a
0N/A * {@code JViewport} and the viewport is taller than the list's
0N/A * preferred height, or if the layout orientation is {@code VERTICAL_WRAP}
0N/A * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
0N/A * <p>
0N/A * If {@code false}, then don't track the viewport's height. This allows
0N/A * vertical scrolling if the {@code JViewport} is itself embedded in a
0N/A * {@code JScrollPane}.
0N/A *
0N/A * @return whether or not an enclosing viewport should force the list's
0N/A * height to match its own
0N/A * @see Scrollable#getScrollableTracksViewportHeight
0N/A */
0N/A public boolean getScrollableTracksViewportHeight() {
0N/A if (getLayoutOrientation() == VERTICAL_WRAP &&
0N/A getVisibleRowCount() <= 0) {
0N/A return true;
0N/A }
2333N/A Container parent = SwingUtilities.getUnwrappedParent(this);
2333N/A if (parent instanceof JViewport) {
2333N/A return parent.getHeight() > getPreferredSize().height;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * See {@code readObject} and {@code writeObject} in {@code JComponent}
0N/A * for more information about serialization in Swing.
0N/A */
0N/A private void writeObject(ObjectOutputStream s) throws IOException {
0N/A s.defaultWriteObject();
0N/A if (getUIClassID().equals(uiClassID)) {
0N/A byte count = JComponent.getWriteObjCounter(this);
0N/A JComponent.setWriteObjCounter(this, --count);
0N/A if (count == 0 && ui != null) {
0N/A ui.installUI(this);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns a {@code String} representation of this {@code JList}.
0N/A * This method is intended to be used only for debugging purposes,
0N/A * and the content and format of the returned {@code String} may vary
0N/A * between implementations. The returned {@code String} may be empty,
0N/A * but may not be {@code null}.
0N/A *
0N/A * @return a {@code String} representation of this {@code JList}.
0N/A */
0N/A protected String paramString() {
0N/A String selectionForegroundString = (selectionForeground != null ?
0N/A selectionForeground.toString() :
0N/A "");
0N/A String selectionBackgroundString = (selectionBackground != null ?
0N/A selectionBackground.toString() :
0N/A "");
0N/A
0N/A return super.paramString() +
0N/A ",fixedCellHeight=" + fixedCellHeight +
0N/A ",fixedCellWidth=" + fixedCellWidth +
0N/A ",horizontalScrollIncrement=" + horizontalScrollIncrement +
0N/A ",selectionBackground=" + selectionBackgroundString +
0N/A ",selectionForeground=" + selectionForegroundString +
0N/A ",visibleRowCount=" + visibleRowCount +
0N/A ",layoutOrientation=" + layoutOrientation;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * --- Accessibility Support ---
0N/A */
0N/A
0N/A /**
0N/A * Gets the {@code AccessibleContext} associated with this {@code JList}.
0N/A * For {@code JList}, the {@code AccessibleContext} takes the form of an
0N/A * {@code AccessibleJList}.
0N/A * <p>
0N/A * A new {@code AccessibleJList} instance is created if necessary.
0N/A *
0N/A * @return an {@code AccessibleJList} that serves as the
0N/A * {@code AccessibleContext} of this {@code JList}
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (accessibleContext == null) {
0N/A accessibleContext = new AccessibleJList();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support for the
0N/A * {@code JList} class. It provides an implementation of the
0N/A * Java Accessibility API appropriate to list user-interface
0N/A * elements.
0N/A * <p>
0N/A * <strong>Warning:</strong>
0N/A * Serialized objects of this class will not be compatible with
0N/A * future Swing releases. The current serialization support is
0N/A * appropriate for short term storage or RMI between applications running
0N/A * the same version of Swing. As of 1.4, support for long term storage
0N/A * of all JavaBeans<sup><font size="-2">TM</font></sup>
0N/A * has been added to the <code>java.beans</code> package.
0N/A * Please see {@link java.beans.XMLEncoder}.
0N/A */
0N/A protected class AccessibleJList extends AccessibleJComponent
0N/A implements AccessibleSelection, PropertyChangeListener,
0N/A ListSelectionListener, ListDataListener {
0N/A
0N/A int leadSelectionIndex;
0N/A
0N/A public AccessibleJList() {
0N/A super();
0N/A JList.this.addPropertyChangeListener(this);
0N/A JList.this.getSelectionModel().addListSelectionListener(this);
0N/A JList.this.getModel().addListDataListener(this);
0N/A leadSelectionIndex = JList.this.getLeadSelectionIndex();
0N/A }
0N/A
0N/A /**
0N/A * Property Change Listener change method. Used to track changes
0N/A * to the DataModel and ListSelectionModel, in order to re-set
0N/A * listeners to those for reporting changes there via the Accessibility
0N/A * PropertyChange mechanism.
0N/A *
0N/A * @param e PropertyChangeEvent
0N/A */
0N/A public void propertyChange(PropertyChangeEvent e) {
0N/A String name = e.getPropertyName();
0N/A Object oldValue = e.getOldValue();
0N/A Object newValue = e.getNewValue();
0N/A
0N/A // re-set listData listeners
0N/A if (name.compareTo("model") == 0) {
0N/A
0N/A if (oldValue != null && oldValue instanceof ListModel) {
0N/A ((ListModel) oldValue).removeListDataListener(this);
0N/A }
0N/A if (newValue != null && newValue instanceof ListModel) {
0N/A ((ListModel) newValue).addListDataListener(this);
0N/A }
0N/A
0N/A // re-set listSelectionModel listeners
0N/A } else if (name.compareTo("selectionModel") == 0) {
0N/A
0N/A if (oldValue != null && oldValue instanceof ListSelectionModel) {
0N/A ((ListSelectionModel) oldValue).removeListSelectionListener(this);
0N/A }
0N/A if (newValue != null && newValue instanceof ListSelectionModel) {
0N/A ((ListSelectionModel) newValue).addListSelectionListener(this);
0N/A }
0N/A
0N/A firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * List Selection Listener value change method. Used to fire
0N/A * the property change
0N/A *
0N/A * @param e ListSelectionEvent
0N/A *
0N/A */
0N/A public void valueChanged(ListSelectionEvent e) {
0N/A int oldLeadSelectionIndex = leadSelectionIndex;
0N/A leadSelectionIndex = JList.this.getLeadSelectionIndex();
0N/A if (oldLeadSelectionIndex != leadSelectionIndex) {
0N/A Accessible oldLS, newLS;
0N/A oldLS = (oldLeadSelectionIndex >= 0)
0N/A ? getAccessibleChild(oldLeadSelectionIndex)
0N/A : null;
0N/A newLS = (leadSelectionIndex >= 0)
0N/A ? getAccessibleChild(leadSelectionIndex)
0N/A : null;
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
0N/A oldLS, newLS);
0N/A }
0N/A
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A
0N/A // Process the State changes for Multiselectable
0N/A AccessibleStateSet s = getAccessibleStateSet();
0N/A ListSelectionModel lsm = JList.this.getSelectionModel();
0N/A if (lsm.getSelectionMode() != ListSelectionModel.SINGLE_SELECTION) {
0N/A if (!s.contains(AccessibleState.MULTISELECTABLE)) {
0N/A s.add(AccessibleState.MULTISELECTABLE);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A null, AccessibleState.MULTISELECTABLE);
0N/A }
0N/A } else {
0N/A if (s.contains(AccessibleState.MULTISELECTABLE)) {
0N/A s.remove(AccessibleState.MULTISELECTABLE);
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
0N/A AccessibleState.MULTISELECTABLE, null);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * List Data Listener interval added method. Used to fire the visible data property change
0N/A *
0N/A * @param e ListDataEvent
0N/A *
0N/A */
0N/A public void intervalAdded(ListDataEvent e) {
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A
0N/A /**
0N/A * List Data Listener interval removed method. Used to fire the visible data property change
0N/A *
0N/A * @param e ListDataEvent
0N/A *
0N/A */
0N/A public void intervalRemoved(ListDataEvent e) {
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A
0N/A /**
0N/A * List Data Listener contents changed method. Used to fire the visible data property change
0N/A *
0N/A * @param e ListDataEvent
0N/A *
0N/A */
0N/A public void contentsChanged(ListDataEvent e) {
0N/A firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
0N/A Boolean.valueOf(false), Boolean.valueOf(true));
0N/A }
0N/A
0N/A // AccessibleContext methods
0N/A
0N/A /**
0N/A * Get the state set of this object.
0N/A *
0N/A * @return an instance of AccessibleState containing the current state
0N/A * of the object
0N/A * @see AccessibleState
0N/A */
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A AccessibleStateSet states = super.getAccessibleStateSet();
0N/A if (selectionModel.getSelectionMode() !=
0N/A ListSelectionModel.SINGLE_SELECTION) {
0N/A states.add(AccessibleState.MULTISELECTABLE);
0N/A }
0N/A return states;
0N/A }
0N/A
0N/A /**
0N/A * Get the role of this object.
0N/A *
0N/A * @return an instance of AccessibleRole describing the role of the
0N/A * object
0N/A * @see AccessibleRole
0N/A */
0N/A public AccessibleRole getAccessibleRole() {
0N/A return AccessibleRole.LIST;
0N/A }
0N/A
0N/A /**
0N/A * Returns the <code>Accessible</code> child contained at
0N/A * the local coordinate <code>Point</code>, if one exists.
0N/A * Otherwise returns <code>null</code>.
0N/A *
0N/A * @return the <code>Accessible</code> at the specified
0N/A * location, if it exists
0N/A */
0N/A public Accessible getAccessibleAt(Point p) {
0N/A int i = locationToIndex(p);
0N/A if (i >= 0) {
0N/A return new AccessibleJListChild(JList.this, i);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the number of accessible children in the object. If all
0N/A * of the children of this object implement Accessible, than this
0N/A * method should return the number of children of this object.
0N/A *
0N/A * @return the number of accessible children in the object.
0N/A */
0N/A public int getAccessibleChildrenCount() {
0N/A return getModel().getSize();
0N/A }
0N/A
0N/A /**
0N/A * Return the nth Accessible child of the object.
0N/A *
0N/A * @param i zero-based index of child
0N/A * @return the nth Accessible child of the object
0N/A */
0N/A public Accessible getAccessibleChild(int i) {
0N/A if (i >= getModel().getSize()) {
0N/A return null;
0N/A } else {
0N/A return new AccessibleJListChild(JList.this, i);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleSelection associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleSelection interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return this;
0N/A }
0N/A
0N/A
0N/A // AccessibleSelection methods
0N/A
0N/A /**
0N/A * Returns the number of items currently selected.
0N/A * If no items are selected, the return value will be 0.
0N/A *
0N/A * @return the number of items currently selected.
0N/A */
0N/A public int getAccessibleSelectionCount() {
0N/A return JList.this.getSelectedIndices().length;
0N/A }
0N/A
0N/A /**
0N/A * Returns an Accessible representing the specified selected item
0N/A * in the object. If there isn't a selection, or there are
0N/A * fewer items selected than the integer passed in, the return
0N/A * value will be <code>null</code>.
0N/A *
0N/A * @param i the zero-based index of selected items
0N/A * @return an Accessible containing the selected item
0N/A */
0N/A public Accessible getAccessibleSelection(int i) {
0N/A int len = getAccessibleSelectionCount();
0N/A if (i < 0 || i >= len) {
0N/A return null;
0N/A } else {
0N/A return getAccessibleChild(JList.this.getSelectedIndices()[i]);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the current child of this object is selected.
0N/A *
0N/A * @param i the zero-based index of the child in this Accessible
0N/A * object.
0N/A * @see AccessibleContext#getAccessibleChild
0N/A */
0N/A public boolean isAccessibleChildSelected(int i) {
0N/A return isSelectedIndex(i);
0N/A }
0N/A
0N/A /**
0N/A * Adds the specified selected item in the object to the object's
0N/A * selection. If the object supports multiple selections,
0N/A * the specified item is added to any existing selection, otherwise
0N/A * it replaces any existing selection in the object. If the
0N/A * specified item is already selected, this method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void addAccessibleSelection(int i) {
0N/A JList.this.addSelectionInterval(i, i);
0N/A }
0N/A
0N/A /**
0N/A * Removes the specified selected item in the object from the object's
0N/A * selection. If the specified item isn't currently selected, this
0N/A * method has no effect.
0N/A *
0N/A * @param i the zero-based index of selectable items
0N/A */
0N/A public void removeAccessibleSelection(int i) {
0N/A JList.this.removeSelectionInterval(i, i);
0N/A }
0N/A
0N/A /**
0N/A * Clears the selection in the object, so that nothing in the
0N/A * object is selected.
0N/A */
0N/A public void clearAccessibleSelection() {
0N/A JList.this.clearSelection();
0N/A }
0N/A
0N/A /**
0N/A * Causes every selected item in the object to be selected
0N/A * if the object supports multiple selections.
0N/A */
0N/A public void selectAllAccessibleSelection() {
0N/A JList.this.addSelectionInterval(0, getAccessibleChildrenCount() -1);
0N/A }
0N/A
0N/A /**
0N/A * This class implements accessibility support appropriate
0N/A * for list children.
0N/A */
0N/A protected class AccessibleJListChild extends AccessibleContext
0N/A implements Accessible, AccessibleComponent {
1983N/A private JList<E> parent = null;
0N/A private int indexInParent;
0N/A private Component component = null;
0N/A private AccessibleContext accessibleContext = null;
1983N/A private ListModel<E> listModel;
1983N/A private ListCellRenderer<? super E> cellRenderer = null;
1983N/A
1983N/A public AccessibleJListChild(JList<E> parent, int indexInParent) {
0N/A this.parent = parent;
0N/A this.setAccessibleParent(parent);
0N/A this.indexInParent = indexInParent;
0N/A if (parent != null) {
0N/A listModel = parent.getModel();
0N/A cellRenderer = parent.getCellRenderer();
0N/A }
0N/A }
0N/A
0N/A private Component getCurrentComponent() {
0N/A return getComponentAtIndex(indexInParent);
0N/A }
0N/A
0N/A private AccessibleContext getCurrentAccessibleContext() {
0N/A Component c = getComponentAtIndex(indexInParent);
0N/A if (c instanceof Accessible) {
625N/A return c.getAccessibleContext();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A private Component getComponentAtIndex(int index) {
0N/A if (index < 0 || index >= listModel.getSize()) {
0N/A return null;
0N/A }
0N/A if ((parent != null)
0N/A && (listModel != null)
0N/A && cellRenderer != null) {
1983N/A E value = listModel.getElementAt(index);
0N/A boolean isSelected = parent.isSelectedIndex(index);
0N/A boolean isFocussed = parent.isFocusOwner()
0N/A && (index == parent.getLeadSelectionIndex());
0N/A return cellRenderer.getListCellRendererComponent(
0N/A parent,
0N/A value,
0N/A index,
0N/A isSelected,
0N/A isFocussed);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A
0N/A // Accessible Methods
0N/A /**
0N/A * Get the AccessibleContext for this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * returns this object, which is its own AccessibleContext.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A return this;
0N/A }
0N/A
0N/A
0N/A // AccessibleContext methods
0N/A
0N/A public String getAccessibleName() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleName();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setAccessibleName(String s) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.setAccessibleName(s);
0N/A }
0N/A }
0N/A
0N/A public String getAccessibleDescription() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleDescription();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setAccessibleDescription(String s) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.setAccessibleDescription(s);
0N/A }
0N/A }
0N/A
0N/A public AccessibleRole getAccessibleRole() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleRole();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public AccessibleStateSet getAccessibleStateSet() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A AccessibleStateSet s;
0N/A if (ac != null) {
0N/A s = ac.getAccessibleStateSet();
0N/A } else {
0N/A s = new AccessibleStateSet();
0N/A }
0N/A
0N/A s.add(AccessibleState.SELECTABLE);
0N/A if (parent.isFocusOwner()
0N/A && (indexInParent == parent.getLeadSelectionIndex())) {
0N/A s.add(AccessibleState.ACTIVE);
0N/A }
0N/A if (parent.isSelectedIndex(indexInParent)) {
0N/A s.add(AccessibleState.SELECTED);
0N/A }
0N/A if (this.isShowing()) {
0N/A s.add(AccessibleState.SHOWING);
0N/A } else if (s.contains(AccessibleState.SHOWING)) {
0N/A s.remove(AccessibleState.SHOWING);
0N/A }
0N/A if (this.isVisible()) {
0N/A s.add(AccessibleState.VISIBLE);
0N/A } else if (s.contains(AccessibleState.VISIBLE)) {
0N/A s.remove(AccessibleState.VISIBLE);
0N/A }
0N/A s.add(AccessibleState.TRANSIENT); // cell-rendered
0N/A return s;
0N/A }
0N/A
0N/A public int getAccessibleIndexInParent() {
0N/A return indexInParent;
0N/A }
0N/A
0N/A public int getAccessibleChildrenCount() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleChildrenCount();
0N/A } else {
0N/A return 0;
0N/A }
0N/A }
0N/A
0N/A public Accessible getAccessibleChild(int i) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A Accessible accessibleChild = ac.getAccessibleChild(i);
0N/A ac.setAccessibleParent(this);
0N/A return accessibleChild;
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public Locale getLocale() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getLocale();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void addPropertyChangeListener(PropertyChangeListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.addPropertyChangeListener(l);
0N/A }
0N/A }
0N/A
0N/A public void removePropertyChangeListener(PropertyChangeListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A ac.removePropertyChangeListener(l);
0N/A }
0N/A }
0N/A
0N/A public AccessibleAction getAccessibleAction() {
0N/A return getCurrentAccessibleContext().getAccessibleAction();
0N/A }
0N/A
0N/A /**
0N/A * Get the AccessibleComponent associated with this object. In the
0N/A * implementation of the Java Accessibility API for this class,
0N/A * return this object, which is responsible for implementing the
0N/A * AccessibleComponent interface on behalf of itself.
0N/A *
0N/A * @return this object
0N/A */
0N/A public AccessibleComponent getAccessibleComponent() {
0N/A return this; // to override getBounds()
0N/A }
0N/A
0N/A public AccessibleSelection getAccessibleSelection() {
0N/A return getCurrentAccessibleContext().getAccessibleSelection();
0N/A }
0N/A
0N/A public AccessibleText getAccessibleText() {
0N/A return getCurrentAccessibleContext().getAccessibleText();
0N/A }
0N/A
0N/A public AccessibleValue getAccessibleValue() {
0N/A return getCurrentAccessibleContext().getAccessibleValue();
0N/A }
0N/A
0N/A
0N/A // AccessibleComponent methods
0N/A
0N/A public Color getBackground() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getBackground();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getBackground();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setBackground(Color c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setBackground(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setBackground(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Color getForeground() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getForeground();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getForeground();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setForeground(Color c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setForeground(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setForeground(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Cursor getCursor() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getCursor();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getCursor();
0N/A } else {
0N/A Accessible ap = getAccessibleParent();
0N/A if (ap instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ap).getCursor();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setCursor(Cursor c) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setCursor(c);
0N/A } else {
0N/A Component cp = getCurrentComponent();
0N/A if (cp != null) {
0N/A cp.setCursor(c);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Font getFont() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getFont();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getFont();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setFont(Font f) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setFont(f);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setFont(f);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public FontMetrics getFontMetrics(Font f) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getFontMetrics(f);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.getFontMetrics(f);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean isEnabled() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).isEnabled();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.isEnabled();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void setEnabled(boolean b) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setEnabled(b);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setEnabled(b);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public boolean isVisible() {
0N/A int fi = parent.getFirstVisibleIndex();
0N/A int li = parent.getLastVisibleIndex();
0N/A // The UI incorrectly returns a -1 for the last
0N/A // visible index if the list is smaller than the
0N/A // viewport size.
0N/A if (li == -1) {
0N/A li = parent.getModel().getSize() - 1;
0N/A }
0N/A return ((indexInParent >= fi)
0N/A && (indexInParent <= li));
0N/A }
0N/A
0N/A public void setVisible(boolean b) {
0N/A }
0N/A
0N/A public boolean isShowing() {
0N/A return (parent.isShowing() && isVisible());
0N/A }
0N/A
0N/A public boolean contains(Point p) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A Rectangle r = ((AccessibleComponent) ac).getBounds();
0N/A return r.contains(p);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A Rectangle r = c.getBounds();
0N/A return r.contains(p);
0N/A } else {
0N/A return getBounds().contains(p);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Point getLocationOnScreen() {
0N/A if (parent != null) {
0N/A Point listLocation = parent.getLocationOnScreen();
0N/A Point componentLocation = parent.indexToLocation(indexInParent);
0N/A if (componentLocation != null) {
0N/A componentLocation.translate(listLocation.x, listLocation.y);
0N/A return componentLocation;
0N/A } else {
0N/A return null;
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public Point getLocation() {
0N/A if (parent != null) {
0N/A return parent.indexToLocation(indexInParent);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setLocation(Point p) {
0N/A if ((parent != null) && (parent.contains(p))) {
0N/A ensureIndexIsVisible(indexInParent);
0N/A }
0N/A }
0N/A
0N/A public Rectangle getBounds() {
0N/A if (parent != null) {
0N/A return parent.getCellBounds(indexInParent,indexInParent);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setBounds(Rectangle r) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setBounds(r);
0N/A }
0N/A }
0N/A
0N/A public Dimension getSize() {
0N/A Rectangle cellBounds = this.getBounds();
0N/A if (cellBounds != null) {
0N/A return cellBounds.getSize();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public void setSize (Dimension d) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).setSize(d);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.setSize(d);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public Accessible getAccessibleAt(Point p) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).getAccessibleAt(p);
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A public boolean isFocusTraversable() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A return ((AccessibleComponent) ac).isFocusTraversable();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A return c.isFocusTraversable();
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void requestFocus() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).requestFocus();
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.requestFocus();
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void addFocusListener(FocusListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).addFocusListener(l);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.addFocusListener(l);
0N/A }
0N/A }
0N/A }
0N/A
0N/A public void removeFocusListener(FocusListener l) {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac instanceof AccessibleComponent) {
0N/A ((AccessibleComponent) ac).removeFocusListener(l);
0N/A } else {
0N/A Component c = getCurrentComponent();
0N/A if (c != null) {
0N/A c.removeFocusListener(l);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // TIGER - 4733624
0N/A /**
0N/A * Returns the icon for the element renderer, as the only item
0N/A * of an array of <code>AccessibleIcon</code>s or a <code>null</code> array
0N/A * if the renderer component contains no icons.
0N/A *
0N/A * @return an array containing the accessible icon
0N/A * or a <code>null</code> array if none
0N/A * @since 1.3
0N/A */
0N/A public AccessibleIcon [] getAccessibleIcon() {
0N/A AccessibleContext ac = getCurrentAccessibleContext();
0N/A if (ac != null) {
0N/A return ac.getAccessibleIcon();
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A } // inner class AccessibleJListChild
0N/A } // inner class AccessibleJList
0N/A}