jdk/src/java.desktop/share/classes/javax/swing/JList.java
author ptbrunet
Mon, 18 Jul 2016 15:43:30 -0500
changeset 39873 76907ae1b680
parent 39858 882e63569349
child 40439 acd9a7547e59
permissions -rw-r--r--
8145207: [macosx] JList, VO can't access non-visible list items Summary: add support for single/multi select following focus and following VO cursor Reviewed-by: alexsch, ant
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
39858
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
     2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5449
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
    27
import java.awt.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Locale;
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    32
import java.util.ArrayList;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    33
import java.util.Collections;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    34
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
    36
import java.beans.JavaBean;
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
    37
import java.beans.BeanProperty;
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    38
import java.beans.PropertyChangeEvent;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    39
import java.beans.PropertyChangeListener;
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
    40
import java.beans.Transient;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.accessibility.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.text.Position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.swing.SwingUtilities2.Section;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import static sun.swing.SwingUtilities2.Section.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * A component that displays a list of objects and allows the user to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * one or more items. A separate model, {@code ListModel}, maintains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * contents of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * It's easy to display an array or Vector of objects, using the {@code JList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * constructor that automatically builds a read-only {@code ListModel} instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * for you:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <pre>
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    64
 * {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * // Create a JList that displays strings from an array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * String[] data = {"one", "two", "three", "four"};
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    68
 * JList<String> myList = new JList<String>(data);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * // Create a JList that displays the superclasses of JList.class, by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * // creating it with a Vector populated with this data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    73
 * Vector<Class<?>> superClasses = new Vector<Class<?>>();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    74
 * Class<JList> rootClass = javax.swing.JList.class;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    75
 * for(Class<?> cls = rootClass; cls != null; cls = cls.getSuperclass()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     superClasses.addElement(cls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    78
 * JList<Class<?>> myList = new JList<Class<?>>(superClasses);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * // The automatically created model is stored in JList's "model"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * // property, which you can retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    83
 * ListModel<Class<?>> model = myList.getModel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * for(int i = 0; i < model.getSize(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *     System.out.println(model.getElementAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
    87
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * A {@code ListModel} can be supplied directly to a {@code JList} by way of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * constructor or the {@code setModel} method. The contents need not be static -
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * the number of items, and the values of items can change over time. A correct
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * {@code ListModel} implementation notifies the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * {@code javax.swing.event.ListDataListener}s that have been added to it, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * time a change occurs. These changes are characterized by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * {@code javax.swing.event.ListDataEvent}, which identifies the range of list
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * indices that have been modified, added, or removed. {@code JList}'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * {@code ListUI} is responsible for keeping the visual representation up to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * date with changes, by listening to the model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * Simple, dynamic-content, {@code JList} applications can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * {@code DefaultListModel} class to maintain list elements. This class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * implements the {@code ListModel} interface and also provides a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <code>java.util.Vector</code>-like API. Applications that need a more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * custom <code>ListModel</code> implementation may instead wish to subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * {@code AbstractListModel}, which provides basic support for managing and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * notifying listeners. For example, a read-only implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * {@code AbstractListModel}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * <pre>
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   110
 * {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * // This list model has about 2^16 elements.  Enjoy scrolling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   113
 * ListModel<String> bigData = new AbstractListModel<String>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *     public int getSize() { return Short.MAX_VALUE; }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   115
 *     public String getElementAt(int index) { return "Index " + index; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * };
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   117
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * The selection state of a {@code JList} is managed by another separate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * model, an instance of {@code ListSelectionModel}. {@code JList} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * initialized with a selection model on construction, and also contains
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * methods to query or set this selection model. Additionally, {@code JList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * provides convenient methods for easily managing the selection. These methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * such as {@code setSelectedIndex} and {@code getSelectedValue}, are cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * methods that take care of the details of interacting with the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * model. By default, {@code JList}'s selection model is configured to allow any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * combination of items to be selected at a time; selection mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * {@code MULTIPLE_INTERVAL_SELECTION}. The selection mode can be changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * on the selection model directly, or via {@code JList}'s cover method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * Responsibility for updating the selection model in response to user gestures
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * lies with the list's {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * A correct {@code ListSelectionModel} implementation notifies the set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * {@code javax.swing.event.ListSelectionListener}s that have been added to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * each time a change to the selection occurs. These changes are characterized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * by a {@code javax.swing.event.ListSelectionEvent}, which identifies the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * of the selection change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * The preferred way to listen for changes in list selection is to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * {@code ListSelectionListener}s directly to the {@code JList}. {@code JList}
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   142
 * then takes care of listening to the selection model and notifying your
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * listeners of change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 * Responsibility for listening to selection changes in order to keep the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * visual representation up to date lies with the list's {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <p>
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
   148
 * <a name="renderer"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * Painting of cells in a {@code JList} is handled by a delegate called a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * cell renderer, installed on the list as the {@code cellRenderer} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * The renderer provides a {@code java.awt.Component} that is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * like a "rubber stamp" to paint the cells. Each time a cell needs to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * painted, the list's {@code ListUI} asks the cell renderer for the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * moves it into place, and has it paint the contents of the cell by way of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * {@code paint} method. A default cell renderer, which uses a {@code JLabel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * component to render, is installed by the lists's {@code ListUI}. You can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * substitute your own renderer using code like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <pre>
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   159
 * {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 *  // Display an icon and a string for each object in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   162
 * class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
32865
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30464
diff changeset
   163
 *     static final ImageIcon longIcon = new ImageIcon("long.gif");
f9cb6e427f9e 8136783: Run blessed-modifier-order script on java.desktop
prr
parents: 30464
diff changeset
   164
 *     static final ImageIcon shortIcon = new ImageIcon("short.gif");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 *     // This is the only method defined by ListCellRenderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 *     // We just reconfigure the JLabel each time we're called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *     public Component getListCellRendererComponent(
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   170
 *       JList<?> list,           // the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *       Object value,            // value to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *       int index,               // cell index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *       boolean isSelected,      // is the cell selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 *       boolean cellHasFocus)    // does the cell have focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *         String s = value.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *         setText(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *         setIcon((s.length() > 10) ? longIcon : shortIcon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *         if (isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *             setBackground(list.getSelectionBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *             setForeground(list.getSelectionForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *             setBackground(list.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *             setForeground(list.getForeground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *         setEnabled(list.isEnabled());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *         setFont(list.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *         setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *         return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 * myList.setCellRenderer(new MyCellRenderer());
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   194
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 * Another job for the cell renderer is in helping to determine sizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
 * information for the list. By default, the list's {@code ListUI} determines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 * the size of cells by asking the cell renderer for its preferred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 * size for each list item. This can be expensive for large lists of items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * To avoid these calculations, you can set a {@code fixedCellWidth} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * {@code fixedCellHeight} on the list, or have these values calculated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 * automatically based on a single prototype value:
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
   204
 * <a name="prototype_example"></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * <pre>
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   206
 * {@code
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   207
 * JList<String> bigDataList = new JList<String>(bigData);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * // We don't want the JList implementation to compute the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * // or height of all of the list cells, so we give it a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * // that's as big as we'll need for any cell.  It uses this to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
 * // compute values for the fixedCellWidth and fixedCellHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * // properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * bigDataList.setPrototypeCellValue("Index 1234567890");
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   216
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * {@code JList} doesn't implement scrolling directly. To create a list that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * scrolls, make it the viewport view of a {@code JScrollPane}. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * JScrollPane scrollPane = new JScrollPane(myList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * // Or in two steps:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * JScrollPane scrollPane = new JScrollPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * scrollPane.getViewport().setView(myList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * {@code JList} doesn't provide any special handling of double or triple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * (or N) mouse clicks, but it's easy to add a {@code MouseListener} if you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * wish to take action on these events. Use the {@code locationToIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * method to determine what cell was clicked. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * MouseListener mouseListener = new MouseAdapter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *     public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *         if (e.getClickCount() == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *             int index = list.locationToIndex(e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 *             System.out.println("Double clicked on Item " + index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 *          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
 * };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 * list.addMouseListener(mouseListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 * <strong>Warning:</strong> Swing is not thread safe. For more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 * information see <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 * href="package-summary.html#threading">Swing's Threading
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Policy</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   255
 * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 * <p>
20455
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 7668
diff changeset
   259
 * See <a href="http://docs.oracle.com/javase/tutorial/uiswing/components/list.html">How to Use Lists</a>
f6f9a0c2796b 8020688: Broken links in documentation at http://docs.oracle.com/javase/6/docs/api/index.
mcherkas
parents: 7668
diff changeset
   260
 * in <a href="http://docs.oracle.com/javase/tutorial/"><em>The Java Tutorial</em></a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * for further documentation.
23715
54ae9dd9df73 8039074: Tidy warnings cleanup for javax.swing
yan
parents: 22574
diff changeset
   262
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * @see ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * @see AbstractListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * @see DefaultListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 * @see ListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 * @see DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 * @see ListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   271
 * @param <E> the type of the elements of this list
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   272
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * @author Hans Muller
25201
4adc75e0c4e5 8046485: Add missing @since tag under javax.swing.*
henryjen
parents: 23715
diff changeset
   274
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   276
@JavaBean(defaultProperty = "UI", description = "A component which allows for the selection of one or more objects from a list.")
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   277
@SwingContainer(false)
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
   278
@SuppressWarnings("serial") // Same-version serialization only
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   279
public class JList<E> extends JComponent implements Scrollable, Accessible
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @see #getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * @see #readObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    private static final String uiClassID = "ListUI";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Indicates a vertical layout of cells, in a single column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * the default layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @see #setLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public static final int VERTICAL = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Indicates a "newspaper style" layout with cells flowing vertically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * then horizontally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @see #setLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public static final int VERTICAL_WRAP = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Indicates a "newspaper style" layout with cells flowing horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * then vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @see #setLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public static final int HORIZONTAL_WRAP = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    private int fixedCellWidth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private int fixedCellHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private int horizontalScrollIncrement = -1;
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   314
    private E prototypeCellValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    private int visibleRowCount = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private Color selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    private Color selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private boolean dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private ListSelectionModel selectionModel;
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   321
    private ListModel<E> dataModel;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   322
    private ListCellRenderer<? super E> cellRenderer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private ListSelectionListener selectionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * How to lay out the cells; defaults to <code>VERTICAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private int layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * The drop mode for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private DropMode dropMode = DropMode.USE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * The drop location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private transient DropLocation dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
39858
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   341
     * Flag to indicate UI update is in progress
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   342
     */
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   343
    private transient boolean updateInProgress;
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   344
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   345
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * A subclass of <code>TransferHandler.DropLocation</code> representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * a drop location for a <code>JList</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @see #getDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    public static final class DropLocation extends TransferHandler.DropLocation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        private final int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        private final boolean isInsert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        private DropLocation(Point p, int index, boolean isInsert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            super(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            this.index = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            this.isInsert = isInsert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         * Returns the index where dropped data should be placed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
         * list. Interpretation of the value depends on the drop mode set on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
         * the associated component. If the drop mode is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
         * <code>DropMode.USE_SELECTION</code> or <code>DropMode.ON</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
         * the return value is an index of a row in the list. If the drop mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
         * <code>DropMode.INSERT</code>, the return value refers to the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
         * where the data should be inserted. If the drop mode is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
         * <code>DropMode.ON_OR_INSERT</code>, the value of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
         * <code>isInsert()</code> indicates whether the index is an index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
         * of a row, or an insert index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
         * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
         * <code>-1</code> indicates that the drop occurred over empty space,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
         * and no index could be calculated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
         * @return the drop index
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        public int getIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * Returns whether or not this location represents an insert
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
         * @return whether or not this is an insert location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        public boolean isInsert() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            return isInsert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         * Returns a string representation of this drop location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * This method is intended to be used for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         * and the content and format of the returned string may vary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         * between implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         * @return a string representation of this drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                   + "[dropPoint=" + getDropPoint() + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                   + "index=" + index + ","
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                   + "insert=" + isInsert + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Constructs a {@code JList} that displays elements from the specified,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * {@code non-null}, model. All {@code JList} constructors delegate to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * This constructor registers the list with the {@code ToolTipManager},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * allowing for tooltips to be provided by the cell renderers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param dataModel the model for the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * @exception IllegalArgumentException if the model is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   420
    public JList(ListModel<E> dataModel)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (dataModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            throw new IllegalArgumentException("dataModel must be non null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        // Register with the ToolTipManager so that tooltips from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        // renderer show through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        ToolTipManager toolTipManager = ToolTipManager.sharedInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        toolTipManager.registerComponent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        layoutOrientation = VERTICAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        this.dataModel = dataModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        selectionModel = createSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        setAutoscrolls(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        updateUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Constructs a <code>JList</code> that displays the elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * the specified array. This constructor creates a read-only model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * for the given array, and then delegates to the constructor that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * takes a {@code ListModel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * Attempts to pass a {@code null} value to this method results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * undefined behavior and, most likely, exceptions. The created model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * references the given array directly. Attempts to modify the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * after constructing the list results in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * @param  listData  the array of Objects to be loaded into the data model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *                   {@code non-null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   455
    public JList(final E[] listData)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        this (
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   458
            new AbstractListModel<E>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                public int getSize() { return listData.length; }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   460
                public E getElementAt(int i) { return listData[i]; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Constructs a <code>JList</code> that displays the elements in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * the specified <code>Vector</code>. This constructor creates a read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * model for the given {@code Vector}, and then delegates to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * that takes a {@code ListModel}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * Attempts to pass a {@code null} value to this method results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * undefined behavior and, most likely, exceptions. The created model
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * references the given {@code Vector} directly. Attempts to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * {@code Vector} after constructing the list results in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @param  listData  the <code>Vector</code> to be loaded into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *                   data model, {@code non-null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   480
    public JList(final Vector<? extends E> listData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        this (
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   482
            new AbstractListModel<E>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                public int getSize() { return listData.size(); }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   484
                public E getElementAt(int i) { return listData.elementAt(i); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * Constructs a <code>JList</code> with an empty, read-only, model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    public JList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        this (
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   495
            new AbstractListModel<E>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
              public int getSize() { return 0; }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   497
              public E getElementAt(int i) { throw new IndexOutOfBoundsException("No Data Model"); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Returns the {@code ListUI}, the look and feel object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return the <code>ListUI</code> object that renders this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public ListUI getUI() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        return (ListUI)ui;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Sets the {@code ListUI}, the look and feel object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * renders this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @param ui  the <code>ListUI</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   521
    @BeanProperty(hidden = true, visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   522
            = "The UI object that implements the Component's LookAndFeel.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    public void setUI(ListUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        super.setUI(ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Resets the {@code ListUI} property by setting it to the value provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * by the current look and feel. If the current cell renderer was installed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * by the developer (rather than the look and feel itself), this also causes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     * the cell renderer and its children to be updated, by calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * {@code SwingUtilities.updateComponentTreeUI} on it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @see UIManager#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * @see SwingUtilities#updateComponentTreeUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public void updateUI() {
39858
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   539
        if (!updateInProgress) {
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   540
            updateInProgress = true;
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   541
            try {
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   542
                setUI((ListUI)UIManager.getUI(this));
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   543
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   544
                ListCellRenderer<? super E> renderer = getCellRenderer();
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   545
                if (renderer instanceof Component) {
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   546
                    SwingUtilities.updateComponentTreeUI((Component)renderer);
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   547
                }
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   548
            } finally {
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   549
                updateInProgress = false;
882e63569349 6567433: JComponent.updateUI() may create StackOverflowError
aghaisas
parents: 33253
diff changeset
   550
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Returns {@code "ListUI"}, the <code>UIDefaults</code> key used to look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * up the name of the {@code javax.swing.plaf.ListUI} class that defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * the look and feel for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * @return the string "ListUI"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @see JComponent#getUIClassID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see UIDefaults#getUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   564
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    public String getUIClassID() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        return uiClassID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /* -----private-----
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * This method is called by setPrototypeCellValue and setCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * to update the fixedCellWidth and fixedCellHeight properties from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * current value of prototypeCellValue (if it's non null).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * This method sets fixedCellWidth and fixedCellHeight but does <b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * generate PropertyChangeEvents for them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @see #setPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @see #setCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private void updateFixedCellSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   583
        ListCellRenderer<? super E> cr = getCellRenderer();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   584
        E value = getPrototypeCellValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        if ((cr != null) && (value != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            Component c = cr.getListCellRendererComponent(this, value, 0, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            /* The ListUI implementation will add Component c to its private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
             * CellRendererPane however we can't assume that's already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
             * been done here.  So we temporarily set the one "inherited"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
             * property that may affect the renderer components preferred size:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
             * its font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            Font f = c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            c.setFont(getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            Dimension d = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            fixedCellWidth = d.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            fixedCellHeight = d.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            c.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * Returns the "prototypical" cell value -- a value used to calculate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * fixed width and height for cells. This can be {@code null} if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * is no such value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @return the value of the {@code prototypeCellValue} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @see #setPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   615
    public E getPrototypeCellValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
        return prototypeCellValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Sets the {@code prototypeCellValue} property, and then (if the new value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * is {@code non-null}), computes the {@code fixedCellWidth} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * {@code fixedCellHeight} properties by requesting the cell renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     * component for the given value (and index 0) from the cell renderer, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * using that component's preferred size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * This method is useful when the list is too long to allow the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * {@code ListUI} to compute the width/height of each cell, and there is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * single cell value that is known to occupy as much space as any of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * others, a so-called prototype.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * While all three of the {@code prototypeCellValue},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * {@code fixedCellHeight}, and {@code fixedCellWidth} properties may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * modified by this method, {@code PropertyChangeEvent} notifications are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * only sent when the {@code prototypeCellValue} property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * To see an example which sets this property, see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * <a href="#prototype_example">class description</a> above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * The default value of this property is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * @param prototypeCellValue  the value on which to base
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *                          <code>fixedCellWidth</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *                          <code>fixedCellHeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * @see #getPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * @see #setFixedCellWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * @see #setFixedCellHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @see JComponent#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   651
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   652
            = "The cell prototype value, used to compute cell width and height.")
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   653
    public void setPrototypeCellValue(E prototypeCellValue) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   654
        E oldValue = this.prototypeCellValue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        this.prototypeCellValue = prototypeCellValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        /* If the prototypeCellValue has changed and is non-null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
         * then recompute fixedCellWidth and fixedCellHeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if ((prototypeCellValue != null) && !prototypeCellValue.equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            updateFixedCellSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        firePropertyChange("prototypeCellValue", oldValue, prototypeCellValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * Returns the value of the {@code fixedCellWidth} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @return the fixed cell width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * @see #setFixedCellWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
    public int getFixedCellWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return fixedCellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Sets a fixed value to be used for the width of every cell in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * If {@code width} is -1, cell widths are computed in the {@code ListUI}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * by applying <code>getPreferredSize</code> to the cell renderer component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * for each list element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * The default value of this property is {@code -1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * @param width the width to be used for all cells in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @see #setPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @see #setFixedCellWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see JComponent#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   694
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   695
            = "Defines a fixed cell width when greater than zero.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public void setFixedCellWidth(int width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        int oldValue = fixedCellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        fixedCellWidth = width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        firePropertyChange("fixedCellWidth", oldValue, fixedCellWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * Returns the value of the {@code fixedCellHeight} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * @return the fixed cell height
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * @see #setFixedCellHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    public int getFixedCellHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        return fixedCellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * Sets a fixed value to be used for the height of every cell in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * If {@code height} is -1, cell heights are computed in the {@code ListUI}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * by applying <code>getPreferredSize</code> to the cell renderer component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * for each list element.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * The default value of this property is {@code -1}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     *
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   723
     * @param height the height to be used for all cells in the list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @see #setPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @see #setFixedCellWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @see JComponent#addPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   728
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   729
            = "Defines a fixed cell height when greater than zero.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    public void setFixedCellHeight(int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        int oldValue = fixedCellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        fixedCellHeight = height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        firePropertyChange("fixedCellHeight", oldValue, fixedCellHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * Returns the object responsible for painting list items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     * @return the value of the {@code cellRenderer} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * @see #setCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
   743
    @Transient
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   744
    public ListCellRenderer<? super E> getCellRenderer() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return cellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Sets the delegate that is used to paint each cell in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * The job of a cell renderer is discussed in detail in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * <a href="#renderer">class level documentation</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * If the {@code prototypeCellValue} property is {@code non-null},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     * setting the cell renderer also causes the {@code fixedCellWidth} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * {@code fixedCellHeight} properties to be re-calculated. Only one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     * <code>PropertyChangeEvent</code> is generated however -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * for the <code>cellRenderer</code> property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * The default value of this property is provided by the {@code ListUI}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * delegate, i.e. by the look and feel implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * @param cellRenderer the <code>ListCellRenderer</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *                          that paints list cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @see #getCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   768
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   769
            = "The component used to draw the cells.")
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   770
    public void setCellRenderer(ListCellRenderer<? super E> cellRenderer) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
   771
        ListCellRenderer<? super E> oldValue = this.cellRenderer;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        this.cellRenderer = cellRenderer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        /* If the cellRenderer has changed and prototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         * was set, then recompute fixedCellWidth and fixedCellHeight.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        if ((cellRenderer != null) && !cellRenderer.equals(oldValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            updateFixedCellSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        firePropertyChange("cellRenderer", oldValue, cellRenderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Returns the color used to draw the foreground of selected items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * {@code DefaultListCellRenderer} uses this color to draw the foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * of items in the selected state, as do the renderers installed by most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     * {@code ListUI} implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * @return the color to draw the foreground of selected items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * @see #setSelectionForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    public Color getSelectionForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        return selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * Sets the color used to draw the foreground of selected items, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * cell renderers can use to render text and graphics.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * {@code DefaultListCellRenderer} uses this color to draw the foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * of items in the selected state, as do the renderers installed by most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * {@code ListUI} implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * The default value of this property is defined by the look and feel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * @param selectionForeground  the {@code Color} to use in the foreground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     *                             for selected list items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * @see #getSelectionForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @see #setSelectionBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @see #setForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * @see #setBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * @see #setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   821
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   822
            = "The foreground color of selected cells.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public void setSelectionForeground(Color selectionForeground) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        Color oldValue = this.selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        this.selectionForeground = selectionForeground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        firePropertyChange("selectionForeground", oldValue, selectionForeground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * Returns the color used to draw the background of selected items.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * {@code DefaultListCellRenderer} uses this color to draw the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * of items in the selected state, as do the renderers installed by most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * {@code ListUI} implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * @return the color to draw the background of selected items
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @see #setSelectionBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    public Color getSelectionBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        return selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * Sets the color used to draw the background of selected items, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * cell renderers can use fill selected cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * {@code DefaultListCellRenderer} uses this color to fill the background
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * of items in the selected state, as do the renderers installed by most
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * {@code ListUI} implementations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * The default value of this property is defined by the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * and feel implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * @param selectionBackground  the {@code Color} to use for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *                             background of selected cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @see #getSelectionBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * @see #setSelectionForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * @see #setForeground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @see #setBackground
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @see #setFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   866
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   867
            = "The background color of selected cells.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    public void setSelectionBackground(Color selectionBackground) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        Color oldValue = this.selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        this.selectionBackground = selectionBackground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        firePropertyChange("selectionBackground", oldValue, selectionBackground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Returns the value of the {@code visibleRowCount} property. See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * documentation for {@link #setVisibleRowCount} for details on how to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * interpret this value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @return the value of the {@code visibleRowCount} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @see #setVisibleRowCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
    public int getVisibleRowCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        return visibleRowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * Sets the {@code visibleRowCount} property, which has different meanings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * depending on the layout orientation: For a {@code VERTICAL} layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * orientation, this sets the preferred number of rows to display without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * requiring scrolling; for other orientations, it affects the wrapping of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * In {@code VERTICAL} orientation:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * Setting this property affects the return value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * {@link #getPreferredScrollableViewportSize} method, which is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * calculate the preferred size of an enclosing viewport. See that method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * documentation for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * In {@code HORIZONTAL_WRAP} and {@code VERTICAL_WRAP} orientations:<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * This affects how cells are wrapped. See the documentation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * {@link #setLayoutOrientation} for more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * The default value of this property is {@code 8}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * Calling this method with a negative value results in the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * being set to {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @param visibleRowCount  an integer specifying the preferred number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *                         rows to display without requiring scrolling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @see #getVisibleRowCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @see #getPreferredScrollableViewportSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @see #setLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @see JComponent#getVisibleRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @see JViewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   919
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   920
            = "The preferred number of rows to display without requiring scrolling")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    public void setVisibleRowCount(int visibleRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        int oldValue = this.visibleRowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        this.visibleRowCount = Math.max(0, visibleRowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        firePropertyChange("visibleRowCount", oldValue, visibleRowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * Returns the layout orientation property for the list: {@code VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     * if the layout is a single column of cells, {@code VERTICAL_WRAP} if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
     * layout is "newspaper style" with the content flowing vertically then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * horizontally, or {@code HORIZONTAL_WRAP} if the layout is "newspaper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * style" with the content flowing horizontally then vertically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * @return the value of the {@code layoutOrientation} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * @see #setLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
    public int getLayoutOrientation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        return layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * Defines the way list cells are layed out. Consider a {@code JList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     * with five cells. Cells can be layed out in one of the following ways:
21982
fd6e5fe509df 8029264: [doclint] more doclint and tidy cleanup
yan
parents: 21592
diff changeset
   947
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * VERTICAL:          0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     *                    1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *                    2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     *                    3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *                    4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * HORIZONTAL_WRAP:   0  1  2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     *                    3  4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * VERTICAL_WRAP:     0  3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *                    1  4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *                    2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
     * A description of these layouts follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * <table border="1"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     *  summary="Describes layouts VERTICAL, HORIZONTAL_WRAP, and VERTICAL_WRAP">
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
   967
     *   <tr><th><p style="text-align:left">Value</p></th><th><p style="text-align:left">Description</p></th></tr>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     *   <tr><td><code>VERTICAL</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     *       <td>Cells are layed out vertically in a single column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *   <tr><td><code>HORIZONTAL_WRAP</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     *       <td>Cells are layed out horizontally, wrapping to a new row as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *           necessary. If the {@code visibleRowCount} property is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *           or equal to zero, wrapping is determined by the width of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *           list; otherwise wrapping is done in such a way as to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     *           {@code visibleRowCount} rows in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     *   <tr><td><code>VERTICAL_WRAP</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *       <td>Cells are layed out vertically, wrapping to a new column as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     *           necessary. If the {@code visibleRowCount} property is less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *           or equal to zero, wrapping is determined by the height of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *           list; otherwise wrapping is done at {@code visibleRowCount} rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *  </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * The default value of this property is <code>VERTICAL</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @param layoutOrientation the new layout orientation, one of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *        {@code VERTICAL}, {@code HORIZONTAL_WRAP} or {@code VERTICAL_WRAP}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * @see #getLayoutOrientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * @see #setVisibleRowCount
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * @see #getScrollableTracksViewportHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @see #getScrollableTracksViewportWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @throws IllegalArgumentException if {@code layoutOrientation} isn't one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *         allowable values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   995
    @BeanProperty(visualUpdate = true, enumerationValues = {
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   996
            "JList.VERTICAL",
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   997
            "JList.HORIZONTAL_WRAP",
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   998
            "JList.VERTICAL_WRAP"}, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
   999
            = "Defines the way list cells are layed out.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
    public void setLayoutOrientation(int layoutOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        int oldValue = this.layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
        switch (layoutOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        case VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        case VERTICAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        case HORIZONTAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            this.layoutOrientation = layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            firePropertyChange("layoutOrientation", oldValue, layoutOrientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            throw new IllegalArgumentException("layoutOrientation must be one of: VERTICAL, HORIZONTAL_WRAP or VERTICAL_WRAP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * Returns the smallest list index that is currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * In a left-to-right {@code componentOrientation}, the first visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * cell is found closest to the list's upper-left corner. In right-to-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * orientation, it is found closest to the upper-right corner.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * If nothing is visible or the list is empty, {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Note that the returned cell may only be partially visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * @return the index of the first visible cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * @see #getLastVisibleIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * @see JComponent#getVisibleRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1027
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
    public int getFirstVisibleIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        Rectangle r = getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        int first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (this.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
            first = locationToIndex(r.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            first = locationToIndex(new Point((r.x + r.width) - 1, r.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        if (first != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            Rectangle bounds = getCellBounds(first, first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                if (bounds.width == 0 || bounds.height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                    first = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
        return first;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * Returns the largest list index that is currently visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     * If nothing is visible or the list is empty, {@code -1} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Note that the returned cell may only be partially visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * @return the index of the last visible cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * @see #getFirstVisibleIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * @see JComponent#getVisibleRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1058
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    public int getLastVisibleIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        boolean leftToRight = this.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        Rectangle r = getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        Point lastPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
            lastPoint = new Point((r.x + r.width) - 1, (r.y + r.height) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            lastPoint = new Point(r.x, (r.y + r.height) - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
        int location = locationToIndex(lastPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        if (location != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
            Rectangle bounds = getCellBounds(location, location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
            if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                SwingUtilities.computeIntersection(r.x, r.y, r.width, r.height, bounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                if (bounds.width == 0 || bounds.height == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    // Try the top left(LTR) or top right(RTL) corner, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                    // then go across checking each cell for HORIZONTAL_WRAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                    // Try the lower left corner, and then go across checking
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    // each cell for other list layout orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                    boolean isHorizontalWrap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                        (getLayoutOrientation() == HORIZONTAL_WRAP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    Point visibleLocation = isHorizontalWrap ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                        new Point(lastPoint.x, r.y) :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                        new Point(r.x, lastPoint.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    int last;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    int visIndex = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    int lIndex = location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                    location = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        last = visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        visIndex = locationToIndex(visibleLocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                        if (visIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                            bounds = getCellBounds(visIndex, visIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                            if (visIndex != lIndex && bounds != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                                bounds.contains(visibleLocation)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                                location = visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                                if (isHorizontalWrap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                                    visibleLocation.y = bounds.y + bounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                                    if (visibleLocation.y >= lastPoint.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                                        // Past visible region, bail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                                        last = visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                                    visibleLocation.x = bounds.x + bounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                                    if (visibleLocation.x >= lastPoint.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                                        // Past visible region, bail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                                        last = visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                                last = visIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                    } while (visIndex != -1 && last != visIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        return location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     * Scrolls the list within an enclosing viewport to make the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
     * cell completely visible. This calls {@code scrollRectToVisible} with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * the bounds of the specified cell. For this method to work, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * {@code JList} must be within a <code>JViewport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     * If the given index is outside the list's range of cells, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * results in nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @param index  the index of the cell to make visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     * @see JComponent#scrollRectToVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
     * @see #getVisibleRect
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    public void ensureIndexIsVisible(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        Rectangle cellBounds = getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        if (cellBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
            scrollRectToVisible(cellBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * Turns on or off automatic drag handling. In order to enable automatic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     * drag handling, this property should be set to {@code true}, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * list's {@code TransferHandler} needs to be {@code non-null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * The default value of the {@code dragEnabled} property is {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * The job of honoring this property, and recognizing a user drag gesture,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * lies with the look and feel implementation, and in particular, the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * {@code ListUI}. When automatic drag handling is enabled, most look and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     * feels (including those that subclass {@code BasicLookAndFeel}) begin a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
     * drag and drop operation whenever the user presses the mouse button over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * an item and then moves the mouse a few pixels. Setting this property to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * {@code true} can therefore have a subtle effect on how selections behave.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * If a look and feel is used that ignores this property, you can still
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * begin a drag and drop operation by calling {@code exportAsDrag} on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * list's {@code TransferHandler}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * @param b whether or not to enable automatic drag handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @exception HeadlessException if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *            <code>b</code> is <code>true</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     *            <code>GraphicsEnvironment.isHeadless()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *            returns <code>true</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @see java.awt.GraphicsEnvironment#isHeadless
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * @see #getDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     * @see #setTransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
     * @see TransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1176
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1177
            = "determines whether automatic drag handling is enabled")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
    public void setDragEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (b && GraphicsEnvironment.isHeadless()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            throw new HeadlessException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        dragEnabled = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
     * Returns whether or not automatic drag handling is enabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
     * @return the value of the {@code dragEnabled} property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * @see #setDragEnabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
    public boolean getDragEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        return dragEnabled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Sets the drop mode for this component. For backward compatibility,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * the default for this property is <code>DropMode.USE_SELECTION</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     * Usage of one of the other modes is recommended, however, for an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * improved user experience. <code>DropMode.ON</code>, for instance,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * offers similar behavior of showing items as selected, but does so without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * affecting the actual selection in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * <code>JList</code> supports the following drop modes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     *    <li><code>DropMode.USE_SELECTION</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     *    <li><code>DropMode.ON</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *    <li><code>DropMode.INSERT</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *    <li><code>DropMode.ON_OR_INSERT</code></li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * The drop mode is only meaningful if this component has a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * <code>TransferHandler</code> that accepts drops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     * @param dropMode the drop mode to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * @throws IllegalArgumentException if the drop mode is unsupported
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     *         or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * @see #getDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * @see #getDropLocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     * @see #setTransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @see TransferHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
    public final void setDropMode(DropMode dropMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        if (dropMode != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            switch (dropMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                case USE_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
                case ON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
                case INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
                case ON_OR_INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    this.dropMode = dropMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
        throw new IllegalArgumentException(dropMode + ": Unsupported drop mode for list");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
     * Returns the drop mode for this component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     * @return the drop mode for this component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
     * @see #setDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
    public final DropMode getDropMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        return dropMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * Calculates a drop location in this component, representing where a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * drop at the given point should insert data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     * @param p the point to calculate a drop location for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @return the drop location, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    DropLocation dropLocationForPoint(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
        DropLocation location = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        Rectangle rect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        int index = locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            rect = getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        switch(dropMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            case USE_SELECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            case ON:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                location = new DropLocation(p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
                    (rect != null && rect.contains(p)) ? index : -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                    false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            case INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                    location = new DropLocation(p, getModel().getSize(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                if (layoutOrientation == HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                    boolean ltr = getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                    if (SwingUtilities2.liesInHorizontal(rect, p, ltr, false) == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    // special case for below all cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                    } else if (index == getModel().getSize() - 1 && p.y >= rect.y + rect.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                    if (SwingUtilities2.liesInVertical(rect, p, false) == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                location = new DropLocation(p, index, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            case ON_OR_INSERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                    location = new DropLocation(p, getModel().getSize(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                boolean between = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                if (layoutOrientation == HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    boolean ltr = getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                    Section section = SwingUtilities2.liesInHorizontal(rect, p, ltr, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                    if (section == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                        between = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                    // special case for below all cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                    } else if (index == getModel().getSize() - 1 && p.y >= rect.y + rect.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                        between = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
                    } else if (section == LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
                        between = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                    Section section = SwingUtilities2.liesInVertical(rect, p, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    if (section == LEADING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                        between = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                    } else if (section == TRAILING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
                        index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
                        between = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
                location = new DropLocation(p, index, between);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
                assert false : "Unexpected drop mode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        return location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     * Called to set or clear the drop location during a DnD operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * In some cases, the component may need to use it's internal selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * temporarily to indicate the drop location. To help facilitate this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * this method returns and accepts as a parameter a state object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * This state object can be used to store, and later restore, the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
     * state. Whatever this method returns will be passed back to it in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * future calls, as the state parameter. If it wants the DnD system to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * continue storing the same state, it must pass it back every time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * Here's how this is used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     * Let's say that on the first call to this method the component decides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
     * to save some state (because it is about to use the selection to show
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     * a drop index). It can return a state object to the caller encapsulating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * any saved selection state. On a second call, let's say the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * is being changed to something else. The component doesn't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * restore anything yet, so it simply passes back the same state object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * to have the DnD system continue storing it. Finally, let's say this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * method is messaged with <code>null</code>. This means DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * is finished with this component for now, meaning it should restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * state. At this point, it can use the state parameter to restore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * said state, and of course return <code>null</code> since there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     * no longer anything to store.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * @param location the drop location (as calculated by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *        <code>dropLocationForPoint</code>) or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     *        if there's no longer a valid drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @param state the state object saved earlier for this component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     *        or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     * @param forDrop whether or not the method is being called because an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     *        actual drop occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * @return any saved state for this component, or <code>null</code> if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    Object setDropLocation(TransferHandler.DropLocation location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                           Object state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                           boolean forDrop) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        Object retVal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        DropLocation listLocation = (DropLocation)location;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        if (dropMode == DropMode.USE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
            if (listLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                if (!forDrop && state != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
                    setSelectedIndices(((int[][])state)[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
                    int anchor = ((int[][])state)[1][0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
                    int lead = ((int[][])state)[1][1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
                    SwingUtilities2.setLeadAnchorWithoutSelection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                            getSelectionModel(), lead, anchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
                if (dropLocation == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                    int[] inds = getSelectedIndices();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                    retVal = new int[][] {inds, {getAnchorSelectionIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
                                                 getLeadSelectionIndex()}};
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
                    retVal = state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
                int index = listLocation.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
                    clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
                    getSelectionModel().setAnchorSelectionIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
                    getSelectionModel().setLeadSelectionIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
                    setSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        DropLocation old = dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        dropLocation = listLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        firePropertyChange("dropLocation", old, dropLocation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
     * Returns the location that this component should visually indicate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     * as the drop location during a DnD operation over the component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * or {@code null} if no location is to currently be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * This method is not meant for querying the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     * from a {@code TransferHandler}, as the drop location is only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * set after the {@code TransferHandler}'s <code>canImport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * has returned and has allowed for the location to be shown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * When this property changes, a property change event with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * name "dropLocation" is fired by the component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
     * By default, responsibility for listening for changes to this property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
     * and indicating the drop location visually lies with the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * {@code ListUI}, which may paint it directly and/or install a cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
     * renderer to do so. Developers wishing to implement custom drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
     * painting and/or replace the default cell renderer, may need to honor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * this property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
     * @return the drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * @see #setDropMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     * @see TransferHandler#canImport(TransferHandler.TransferSupport)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1442
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    public final DropLocation getDropLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
        return dropLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * Returns the next list element whose {@code toString} value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     * starts with the given prefix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * @param prefix the string to test for a match
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * @param startIndex the index for starting the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @param bias the search direction, either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Position.Bias.Forward or Position.Bias.Backward.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * @return the index of the next list element that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * starts with the prefix; otherwise {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @exception IllegalArgumentException if prefix is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * or startIndex is out of bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1462
        ListModel<E> model = getModel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        int max = model.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
        if (prefix == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        if (startIndex < 0 || startIndex >= max) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        prefix = prefix.toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        // start search from the next element after the selected element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
        int increment = (bias == Position.Bias.Forward) ? 1 : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        int index = startIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
        do {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1476
            E element = model.getElementAt(index);
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1477
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1478
            if (element != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                String string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1481
                if (element instanceof String) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1482
                    string = ((String)element).toUpperCase();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
                else {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1485
                    string = element.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                    if (string != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                        string = string.toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
                if (string != null && string.startsWith(prefix)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                    return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
            index = (index + increment + max) % max;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        } while (index != startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
     * Returns the tooltip text to be used for the given event. This overrides
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
     * {@code JComponent}'s {@code getToolTipText} to first check the cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * renderer component for the cell over which the event occurred, returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     * its tooltip text, if any. This implementation allows you to specify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * tooltip text on the cell level, by using {@code setToolTipText} on your
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * cell renderer component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     * <p>
21592
da6abe91602a 8025234: [javadoc] fix some errors in javax.swing.**
yan
parents: 20458
diff changeset
  1508
     * <strong>Note:</strong> For <code>JList</code> to properly display the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
     * tooltips of its renderers in this manner, <code>JList</code> must be a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * registered component with the <code>ToolTipManager</code>. This registration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * is done automatically in the constructor. However, if at a later point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     * <code>JList</code> is unregistered, by way of a call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
     * {@code setToolTipText(null)}, tips from the renderers will no longer display.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
     * @param event the {@code MouseEvent} to fetch the tooltip text for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
     * @see JComponent#setToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
     * @see JComponent#getToolTipText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
    public String getToolTipText(MouseEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        if(event != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
            Point p = event.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            int index = locationToIndex(p);
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1523
            ListCellRenderer<? super E> r = getCellRenderer();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            Rectangle cellBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
            if (index != -1 && r != null && (cellBounds =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                               getCellBounds(index, index)) != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                               cellBounds.contains(p.x, p.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                ListSelectionModel lsm = getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                Component rComponent = r.getListCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
                           this, getModel().getElementAt(index), index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                           lsm.isSelectedIndex(index),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                           (hasFocus() && (lsm.getLeadSelectionIndex() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                                           index)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                if(rComponent instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    MouseEvent      newEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    p.translate(-cellBounds.x, -cellBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    newEvent = new MouseEvent(rComponent, event.getID(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                                              event.getWhen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                                              event.getModifiers(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                                              p.x, p.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                                              event.getXOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                                              event.getYOnScreen(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                                              event.getClickCount(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                                              event.isPopupTrigger(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                                              MouseEvent.NOBUTTON);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    String tip = ((JComponent)rComponent).getToolTipText(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                                              newEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    if (tip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                        return tip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
        return super.getToolTipText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * --- ListUI Delegations ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
     * Returns the cell index closest to the given location in the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * coordinate system. To determine if the cell actually contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * specified location, compare the point against the cell's bounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * as provided by {@code getCellBounds}. This method returns {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * if the model is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * This is a cover method that delegates to the method of the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     * in the list's {@code ListUI}. It returns {@code -1} if the list has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
     * no {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
     * @param location the coordinates of the point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     * @return the cell index closest to the given location, or {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    public int locationToIndex(Point location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        ListUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        return (ui != null) ? ui.locationToIndex(this, location) : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * Returns the origin of the specified item in the list's coordinate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * system. This method returns {@code null} if the index isn't valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * This is a cover method that delegates to the method of the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * in the list's {@code ListUI}. It returns {@code null} if the list has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * no {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * @param index the cell index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * @return the origin of the cell, or {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    public Point indexToLocation(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
        ListUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        return (ui != null) ? ui.indexToLocation(this, index) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * Returns the bounding rectangle, in the list's coordinate system,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * for the range of cells specified by the two indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * These indices can be supplied in any order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * If the smaller index is outside the list's range of cells, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * returns {@code null}. If the smaller index is valid, but the larger
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * index is outside the list's range, the bounds of just the first index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * is returned. Otherwise, the bounds of the valid range is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * This is a cover method that delegates to the method of the same name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * in the list's {@code ListUI}. It returns {@code null} if the list has
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * no {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * @param index0 the first index in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * @param index1 the second index in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * @return the bounding rectangle for the range of cells, or {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
    public Rectangle getCellBounds(int index0, int index1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        ListUI ui = getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        return (ui != null) ? ui.getCellBounds(this, index0, index1) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * --- ListModel Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * Returns the data model that holds the list of items displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * by the <code>JList</code> component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * @return the <code>ListModel</code> that provides the displayed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     *                          list of items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1641
    public ListModel<E> getModel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        return dataModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
     * Sets the model that represents the contents or "value" of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * list, notifies property change listeners, and then clears the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * list's selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * @param model  the <code>ListModel</code> that provides the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     *                                          list of items for display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * @exception IllegalArgumentException  if <code>model</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     *                                          <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * @see #clearSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1659
    @BeanProperty(visualUpdate = true, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1660
            = "The object that contains the data to be drawn by this JList.")
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1661
    public void setModel(ListModel<E> model) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
        if (model == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
            throw new IllegalArgumentException("model must be non null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
        }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1665
        ListModel<E> oldValue = dataModel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        dataModel = model;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        firePropertyChange("model", oldValue, dataModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /**
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1673
     * Constructs a read-only <code>ListModel</code> from an array of items,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * and calls {@code setModel} with this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * Attempts to pass a {@code null} value to this method results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * undefined behavior and, most likely, exceptions. The created model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * references the given array directly. Attempts to modify the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     * after invoking this method results in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
     *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1681
     * @param listData an array of {@code E} containing the items to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     *        display in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1685
    public void setListData(final E[] listData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        setModel (
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1687
            new AbstractListModel<E>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
                public int getSize() { return listData.length; }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1689
                public E getElementAt(int i) { return listData[i]; }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     * Constructs a read-only <code>ListModel</code> from a <code>Vector</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     * and calls {@code setModel} with this model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * Attempts to pass a {@code null} value to this method results in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * undefined behavior and, most likely, exceptions. The created model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * references the given {@code Vector} directly. Attempts to modify the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * {@code Vector} after invoking this method results in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * @param listData a <code>Vector</code> containing the items to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     *                                          display in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     * @see #setModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1708
    public void setListData(final Vector<? extends E> listData) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
        setModel (
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1710
            new AbstractListModel<E>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                public int getSize() { return listData.size(); }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  1712
                public E getElementAt(int i) { return listData.elementAt(i); }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * --- ListSelectionModel delegations and extensions ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * Returns an instance of {@code DefaultListSelectionModel}; called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * during construction to initialize the list's selection model
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * @return a {@code DefaultListSelecitonModel}, used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     *         the list's selection model property during construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     * @see #setSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * @see DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    protected ListSelectionModel createSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        return new DefaultListSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * Returns the current selection model. The selection model maintains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * selection state of the list. See the class level documentation for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     * details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * @return the <code>ListSelectionModel</code> that maintains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     *         list's selections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * @see #setSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     * @see ListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    public ListSelectionModel getSelectionModel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        return selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * Notifies {@code ListSelectionListener}s added directly to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * of selection changes made to the selection model. {@code JList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * listens for changes made to the selection in the selection model,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * and forwards notification to listeners added to the list directly,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * by calling this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * This method constructs a {@code ListSelectionEvent} with this list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * as the source, and the specified arguments, and sends it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * registered {@code ListSelectionListeners}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     * @param firstIndex the first index in the range, {@code <= lastIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
     * @param lastIndex the last index in the range, {@code >= firstIndex}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     * @param isAdjusting whether or not this is one in a series of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     *        multiple events, where changes are still being made
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * @see #removeListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * @see javax.swing.event.ListSelectionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
     * @see EventListenerList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    protected void fireSelectionValueChanged(int firstIndex, int lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                                             boolean isAdjusting)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        Object[] listeners = listenerList.getListenerList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        ListSelectionEvent e = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        for (int i = listeners.length - 2; i >= 0; i -= 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
            if (listeners[i] == ListSelectionListener.class) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                if (e == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
                    e = new ListSelectionEvent(this, firstIndex, lastIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                                               isAdjusting);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                ((ListSelectionListener)listeners[i+1]).valueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
    /* A ListSelectionListener that forwards ListSelectionEvents from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
     * the selectionModel to the JList ListSelectionListeners.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
     * forwarded events only differ from the originals in that their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
     * source is the JList instead of the selectionModel itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    private class ListSelectionHandler implements ListSelectionListener, Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
            fireSelectionValueChanged(e.getFirstIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
                                      e.getLastIndex(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                                      e.getValueIsAdjusting());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
     * Adds a listener to the list, to be notified each time a change to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     * selection occurs; the preferred way of listening for selection state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
     * changes. {@code JList} takes care of listening for selection state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
     * changes in the selection model, and notifies the given listener of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
     * each change. {@code ListSelectionEvent}s sent to the listener have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * {@code source} property set to this list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * @param listener the {@code ListSelectionListener} to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * @see #getSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * @see #getListSelectionListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    public void addListSelectionListener(ListSelectionListener listener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        if (selectionListener == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
            selectionListener = new ListSelectionHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
            getSelectionModel().addListSelectionListener(selectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        listenerList.add(ListSelectionListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
     * Removes a selection listener from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
     * @param listener the {@code ListSelectionListener} to remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
     * @see #getSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    public void removeListSelectionListener(ListSelectionListener listener) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        listenerList.remove(ListSelectionListener.class, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
     * Returns an array of all the {@code ListSelectionListener}s added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
     * to this {@code JList} by way of {@code addListSelectionListener}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * @return all of the {@code ListSelectionListener}s on this list, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
     *         an empty array if no listeners have been added
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1852
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    public ListSelectionListener[] getListSelectionListeners() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  1854
        return listenerList.getListeners(ListSelectionListener.class);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
     * Sets the <code>selectionModel</code> for the list to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
     * non-<code>null</code> <code>ListSelectionModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
     * implementation. The selection model handles the task of making single
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
     * selections, selections of contiguous ranges, and non-contiguous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
     * selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
     * This is a JavaBeans bound property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
     * @param selectionModel  the <code>ListSelectionModel</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
     *                          implements the selections
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
     * @exception IllegalArgumentException   if <code>selectionModel</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     *                                          is <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     * @see #getSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1873
    @BeanProperty(description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1874
            = "The selection model, recording which cells are selected.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
    public void setSelectionModel(ListSelectionModel selectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
        if (selectionModel == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            throw new IllegalArgumentException("selectionModel must be non null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        /* Remove the forwarding ListSelectionListener from the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
         * selectionModel, and add it to the new one, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        if (selectionListener != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            this.selectionModel.removeListSelectionListener(selectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            selectionModel.addListSelectionListener(selectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        ListSelectionModel oldValue = this.selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        this.selectionModel = selectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        firePropertyChange("selectionModel", oldValue, selectionModel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
     * Sets the selection mode for the list. This is a cover method that sets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
     * the selection mode directly on the selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
     * The following list describes the accepted selection modes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
     * <li>{@code ListSelectionModel.SINGLE_SELECTION} -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
     *   Only one list index can be selected at a time. In this mode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
     *   {@code setSelectionInterval} and {@code addSelectionInterval} are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
     *   equivalent, both replacing the current selection with the index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
     *   represented by the second argument (the "lead").
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
     * <li>{@code ListSelectionModel.SINGLE_INTERVAL_SELECTION} -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     *   Only one contiguous interval can be selected at a time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     *   In this mode, {@code addSelectionInterval} behaves like
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *   {@code setSelectionInterval} (replacing the current selection},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     *   unless the given interval is immediately adjacent to or overlaps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     *   the existing selection, and can be used to grow the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * <li>{@code ListSelectionModel.MULTIPLE_INTERVAL_SELECTION} -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     *   In this mode, there's no restriction on what can be selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     *   This mode is the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
     * @param selectionMode the selection mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
     * @see #getSelectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
     * @throws IllegalArgumentException if the selection mode isn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
     *         one of those allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1921
    @BeanProperty(bound = false, enumerationValues = {
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1922
            "ListSelectionModel.SINGLE_SELECTION",
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1923
            "ListSelectionModel.SINGLE_INTERVAL_SELECTION",
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1924
            "ListSelectionModel.MULTIPLE_INTERVAL_SELECTION"}, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1925
            = "The selection mode.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
    public void setSelectionMode(int selectionMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        getSelectionModel().setSelectionMode(selectionMode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
     * Returns the current selection mode for the list. This is a cover
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
     * method that delegates to the method of the same name on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
     * list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
     * @return the current selection mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
     * @see #setSelectionMode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
    public int getSelectionMode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        return getSelectionModel().getSelectionMode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
     * Returns the anchor selection index. This is a cover method that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * delegates to the method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * @return the anchor selection index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * @see ListSelectionModel#getAnchorSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1950
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    public int getAnchorSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        return getSelectionModel().getAnchorSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
     * Returns the lead selection index. This is a cover method that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
     * delegates to the method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
     * @return the lead selection index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
     * @see ListSelectionModel#getLeadSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1963
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1964
            = "The lead selection index.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
    public int getLeadSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        return getSelectionModel().getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     * Returns the smallest selected cell index, or {@code -1} if the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * is empty. This is a cover method that delegates to the method of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * @return the smallest selected cell index, or {@code -1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @see ListSelectionModel#getMinSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1978
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    public int getMinSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        return getSelectionModel().getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * Returns the largest selected cell index, or {@code -1} if the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     * is empty. This is a cover method that delegates to the method of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
     * name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
     * @return the largest selected cell index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
     * @see ListSelectionModel#getMaxSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  1992
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    public int getMaxSelectionIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        return getSelectionModel().getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
     * Returns {@code true} if the specified index is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
     * else {@code false}. This is a cover method that delegates to the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
     * of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
     * @param index index to be queried for selection state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * @return {@code true} if the specified index is selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     *         else {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
     * @see ListSelectionModel#isSelectedIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * @see #setSelectedIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    public boolean isSelectedIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        return getSelectionModel().isSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
     * Returns {@code true} if nothing is selected, else {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
     * This is a cover method that delegates to the method of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
     * name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
     * @return {@code true} if nothing is selected, else {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
     * @see ListSelectionModel#isSelectionEmpty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
     * @see #clearSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2023
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    public boolean isSelectionEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        return getSelectionModel().isSelectionEmpty();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
     * Clears the selection; after calling this method, {@code isSelectionEmpty}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
     * will return {@code true}. This is a cover method that delegates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
     * method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
     * @see ListSelectionModel#clearSelection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
     * @see #isSelectionEmpty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    public void clearSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
        getSelectionModel().clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
     * Selects the specified interval. Both {@code anchor} and {@code lead}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
     * indices are included. {@code anchor} doesn't have to be less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * equal to {@code lead}. This is a cover method that delegates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
     * Refer to the documentation of the selection model class being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
     * for details on how values less than {@code 0} are handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
     * @param anchor the first index to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
     * @param lead the last index to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
     * @see ListSelectionModel#setSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
     * @see DefaultListSelectionModel#setSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
     * @see #createSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
     * @see #addSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
     * @see #removeSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    public void setSelectionInterval(int anchor, int lead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        getSelectionModel().setSelectionInterval(anchor, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     * Sets the selection to be the union of the specified interval with current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * selection. Both the {@code anchor} and {@code lead} indices are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * included. {@code anchor} doesn't have to be less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * equal to {@code lead}. This is a cover method that delegates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     * method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     * Refer to the documentation of the selection model class being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
     * for details on how values less than {@code 0} are handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
     * @param anchor the first index to add to the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
     * @param lead the last index to add to the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
     * @see ListSelectionModel#addSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
     * @see DefaultListSelectionModel#addSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
     * @see #createSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
     * @see #setSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
     * @see #removeSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    public void addSelectionInterval(int anchor, int lead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        getSelectionModel().addSelectionInterval(anchor, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
     * Sets the selection to be the set difference of the specified interval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
     * and the current selection. Both the {@code index0} and {@code index1}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
     * indices are removed. {@code index0} doesn't have to be less than or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
     * equal to {@code index1}. This is a cover method that delegates to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
     * method of the same name on the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
     * Refer to the documentation of the selection model class being used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
     * for details on how values less than {@code 0} are handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
     * @param index0 the first index to remove from the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
     * @param index1 the last index to remove from the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
     * @see ListSelectionModel#removeSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     * @see DefaultListSelectionModel#removeSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
     * @see #createSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @see #setSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     * @see #addSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    public void removeSelectionInterval(int index0, int index1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        getSelectionModel().removeSelectionInterval(index0, index1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     * Sets the selection model's {@code valueIsAdjusting} property. When
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     * {@code true}, upcoming changes to selection should be considered part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     * of a single change. This property is used internally and developers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     * typically need not call this method. For example, when the model is being
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * updated in response to a user drag, the value of the property is set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     * to {@code true} when the drag is initiated and set to {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
     * when the drag is finished. This allows listeners to update only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
     * when a change has been finalized, rather than handling all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
     * intermediate values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
     * You may want to use this directly if making a series of changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
     * that should be considered part of a single change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
     * This is a cover method that delegates to the method of the same name on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
     * the list's selection model. See the documentation for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
     * {@link javax.swing.ListSelectionModel#setValueIsAdjusting} for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
     * more details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
     * @param b the new value for the property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * @see ListSelectionModel#setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     * @see javax.swing.event.ListSelectionEvent#getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * @see #getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    public void setValueIsAdjusting(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
        getSelectionModel().setValueIsAdjusting(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     * Returns the value of the selection model's {@code isAdjusting} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     * This is a cover method that delegates to the method of the same name on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     * the list's selection model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
     * @return the value of the selection model's {@code isAdjusting} property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
     * @see #setValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
     * @see ListSelectionModel#getValueIsAdjusting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
    public boolean getValueIsAdjusting() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
        return getSelectionModel().getValueIsAdjusting();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
     * Returns an array of all of the selected indices, in increasing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     * order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
     * @return all of the selected indices, in increasing order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
     *         or an empty array if nothing is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * @see #removeSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
     */
466
6acd5ec503a8 4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents: 2
diff changeset
  2164
    @Transient
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    public int[] getSelectedIndices() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
        ListSelectionModel sm = getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        int iMin = sm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
        int iMax = sm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        if ((iMin < 0) || (iMax < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
            return new int[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
        int[] rvTmp = new int[1+ (iMax - iMin)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        for(int i = iMin; i <= iMax; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
            if (sm.isSelectedIndex(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                rvTmp[n++] = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        int[] rv = new int[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
        System.arraycopy(rvTmp, 0, rv, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
     * Selects a single cell. Does nothing if the given index is greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
     * than or equal to the model size. This is a convenience method that uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
     * {@code setSelectionInterval} on the selection model. Refer to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * documentation for the selection model class being used for details on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     * how values less than {@code 0} are handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     * @param index the index of the cell to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
     * @see ListSelectionModel#setSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * @see #isSelectedIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2199
    @BeanProperty(bound = false, description
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2200
            = "The index of the selected cell.")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
    public void setSelectedIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        if (index >= getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
        getSelectionModel().setSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
     * Changes the selection to be the set of indices specified by the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
     * array. Indices greater than or equal to the model size are ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
     * This is a convenience method that clears the selection and then uses
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
     * {@code addSelectionInterval} on the selection model to add the indices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
     * Refer to the documentation of the selection model class being used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
     * details on how values less than {@code 0} are handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
     * @param indices an array of the indices of the cells to select,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
     *                {@code non-null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
     * @see ListSelectionModel#addSelectionInterval
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
     * @see #isSelectedIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
     * @throws NullPointerException if the given array is {@code null}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    public void setSelectedIndices(int[] indices) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
        ListSelectionModel sm = getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        sm.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
        int size = getModel().getSize();
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  2228
        for (int i : indices) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  2229
            if (i < size) {
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  2230
                sm.addSelectionInterval(i, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
     * Returns an array of all the selected values, in increasing order based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * on their indices in the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
     * @return the selected values, or an empty array if nothing is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * @see #isSelectedIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
     * @see #addListSelectionListener
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2244
     *
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2245
     * @deprecated As of JDK 1.7, replaced by {@link #getSelectedValuesList()}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
     */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2247
    @Deprecated
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2248
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
    public Object[] getSelectedValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        ListSelectionModel sm = getSelectionModel();
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2251
        ListModel<E> dm = getModel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        int iMin = sm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
        int iMax = sm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
        if ((iMin < 0) || (iMax < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
            return new Object[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
        Object[] rvTmp = new Object[1+ (iMax - iMin)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
        for(int i = iMin; i <= iMax; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
            if (sm.isSelectedIndex(i)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                rvTmp[n++] = dm.getElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
        Object[] rv = new Object[n];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
        System.arraycopy(rvTmp, 0, rv, 0, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
        return rv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2272
    /**
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2273
     * Returns a list of all the selected items, in increasing order based
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2274
     * on their indices in the list.
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2275
     *
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2276
     * @return the selected items, or an empty list if nothing is selected
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2277
     * @see #isSelectedIndex
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2278
     * @see #getModel
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2279
     * @see #addListSelectionListener
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2280
     *
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2281
     * @since 1.7
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2282
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2283
    @BeanProperty(bound = false)
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2284
    public List<E> getSelectedValuesList() {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2285
        ListSelectionModel sm = getSelectionModel();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2286
        ListModel<E> dm = getModel();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2287
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2288
        int iMin = sm.getMinSelectionIndex();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2289
        int iMax = sm.getMaxSelectionIndex();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2290
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2291
        if ((iMin < 0) || (iMax < 0)) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2292
            return Collections.emptyList();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2293
        }
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2294
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2295
        List<E> selectedItems = new ArrayList<E>();
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2296
        for(int i = iMin; i <= iMax; i++) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2297
            if (sm.isSelectedIndex(i)) {
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2298
                selectedItems.add(dm.getElementAt(i));
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2299
            }
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2300
        }
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2301
        return selectedItems;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2302
    }
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2303
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
     * Returns the smallest selected cell index; <i>the selection</i> when only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     * a single item is selected in the list. When multiple items are selected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
     * it is simply the smallest selected index. Returns {@code -1} if there is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
     * no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
     * This method is a cover that delegates to {@code getMinSelectionIndex}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * @return the smallest selected cell index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * @see #getMinSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
    public int getSelectedIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
        return getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
     * Returns the value for the smallest selected cell index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
     * <i>the selected value</i> when only a single item is selected in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
     * list. When multiple items are selected, it is simply the value for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
     * smallest selected index. Returns {@code null} if there is no selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
     * This is a convenience method that simply returns the model value for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
     * {@code getMinSelectionIndex}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     * @return the first selected value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
     * @see #getMinSelectionIndex
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * @see #getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * @see #addListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2336
    @BeanProperty(bound = false)
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2337
    public E getSelectedValue() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
        int i = getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
        return (i == -1) ? null : getModel().getElementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     * Selects the specified object from the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
     * @param anObject      the object to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
     * @param shouldScroll  {@code true} if the list should scroll to display
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
     *                      the selected object, if one exists; otherwise {@code false}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
    public void setSelectedValue(Object anObject,boolean shouldScroll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
        if(anObject == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            setSelectedIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
        else if(!anObject.equals(getSelectedValue())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            int i,c;
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  2355
            ListModel<E> dm = getModel();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
            for(i=0,c=dm.getSize();i<c;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                if(anObject.equals(dm.getElementAt(i))){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                    setSelectedIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
                    if(shouldScroll)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
                        ensureIndexIsVisible(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                    repaint();  /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            setSelectedIndex(-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
        repaint(); /** FIX-ME setSelectedIndex does not redraw all the time with the basic l&f**/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * --- The Scrollable Implementation ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
    private void checkScrollableParameters(Rectangle visibleRect, int orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
        if (visibleRect == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            throw new IllegalArgumentException("visibleRect must be non-null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
        switch (orientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
        case SwingConstants.VERTICAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
        case SwingConstants.HORIZONTAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
            throw new IllegalArgumentException("orientation must be one of: VERTICAL, HORIZONTAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * Computes the size of viewport needed to display {@code visibleRowCount}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
     * rows. The value returned by this method depends on the layout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
     * orientation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     * <b>{@code VERTICAL}:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * This is trivial if both {@code fixedCellWidth} and {@code fixedCellHeight}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
     * have been set (either explicitly or by specifying a prototype cell value).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * The width is simply the {@code fixedCellWidth} plus the list's horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     * insets. The height is the {@code fixedCellHeight} multiplied by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * {@code visibleRowCount}, plus the list's vertical insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * If either {@code fixedCellWidth} or {@code fixedCellHeight} haven't been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * specified, heuristics are used. If the model is empty, the width is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
     * the {@code fixedCellWidth}, if greater than {@code 0}, or a hard-coded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
     * value of {@code 256}. The height is the {@code fixedCellHeight} multiplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * by {@code visibleRowCount}, if {@code fixedCellHeight} is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
     * {@code 0}, otherwise it is a hard-coded value of {@code 16} multiplied by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
     * {@code visibleRowCount}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * If the model isn't empty, the width is the preferred size's width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * typically the width of the widest list element. The height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     * {@code fixedCellHeight} multiplied by the {@code visibleRowCount},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
     * plus the list's vertical insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
     * <b>{@code VERTICAL_WRAP} or {@code HORIZONTAL_WRAP}:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
     * <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * This method simply returns the value from {@code getPreferredSize}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     * The list's {@code ListUI} is expected to override {@code getPreferredSize}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
     * to return an appropriate value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
     * @return a dimension containing the size of the viewport needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     *          to display {@code visibleRowCount} rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * @see #getPreferredScrollableViewportSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     * @see #setPrototypeCellValue
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2426
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
    public Dimension getPreferredScrollableViewportSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
        if (getLayoutOrientation() != VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
            return getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        Insets insets = getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
        int dx = insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
        int dy = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        int visibleRowCount = getVisibleRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
        int fixedCellWidth = getFixedCellWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        int fixedCellHeight = getFixedCellHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
        if ((fixedCellWidth > 0) && (fixedCellHeight > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
            int width = fixedCellWidth + dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
            int height = (visibleRowCount * fixedCellHeight) + dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
            return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        else if (getModel().getSize() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
            int width = getPreferredSize().width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
            int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
            Rectangle r = getCellBounds(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
            if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
                height = (visibleRowCount * r.height) + dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
                // Will only happen if UI null, shouldn't matter what we return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
                height = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
            return new Dimension(width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
            fixedCellWidth = (fixedCellWidth > 0) ? fixedCellWidth : 256;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
            fixedCellHeight = (fixedCellHeight > 0) ? fixedCellHeight : 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
            return new Dimension(fixedCellWidth, fixedCellHeight * visibleRowCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     * Returns the distance to scroll to expose the next or previous
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     * row (for vertical scrolling) or column (for horizontal scrolling).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     * For horizontal scrolling, if the layout orientation is {@code VERTICAL},
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     * then the list's font size is returned (or {@code 1} if the font is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     * {@code null}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * @param orientation {@code SwingConstants.HORIZONTAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *                    {@code SwingConstants.VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     * @param direction less or equal to zero to scroll up/back,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     *                  greater than zero for down/forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     * @return the "unit" increment for scrolling in the specified direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     *         always positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * @see #getScrollableBlockIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     * @see Scrollable#getScrollableUnitIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     * @throws IllegalArgumentException if {@code visibleRect} is {@code null}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     *         {@code orientation} isn't one of {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     *         {@code SwingConstants.HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        checkScrollableParameters(visibleRect, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
        if (orientation == SwingConstants.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
            int row = locationToIndex(visibleRect.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
            if (row == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
                /* Scroll Down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                    Rectangle r = getCellBounds(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
                    return (r == null) ? 0 : r.height - (visibleRect.y - r.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                /* Scroll Up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
                    Rectangle r = getCellBounds(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                    /* The first row is completely visible and it's row 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                     * We're done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
                    if ((r.y == visibleRect.y) && (row == 0))  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
                    /* The first row is completely visible, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
                     * height of the previous row or 0 if the first row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                     * is the top row of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
                    else if (r.y == visibleRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
                        Point loc = r.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                        loc.y--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
                        int prevIndex = locationToIndex(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
                        Rectangle prevR = getCellBounds(prevIndex, prevIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
                        if (prevR == null || prevR.y >= r.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
                            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                        return prevR.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
                    /* The first row is partially visible, return the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
                     * height of hidden part.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                        return visibleRect.y - r.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
        } else if (orientation == SwingConstants.HORIZONTAL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                           getLayoutOrientation() != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
            boolean leftToRight = getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
            Point leadingPoint;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
            if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
                leadingPoint = visibleRect.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                leadingPoint = new Point(visibleRect.x + visibleRect.width -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                                         visibleRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
            index = locationToIndex(leadingPoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
                Rectangle cellBounds = getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
                if (cellBounds != null && cellBounds.contains(leadingPoint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
                    int leadingVisibleEdge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                    int leadingCellEdge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
                    if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
                        leadingVisibleEdge = visibleRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
                        leadingCellEdge = cellBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
                        leadingVisibleEdge = visibleRect.x + visibleRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                        leadingCellEdge = cellBounds.x + cellBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
                    if (leadingCellEdge != leadingVisibleEdge) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                        if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
                            // Show remainder of leading cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
                            return Math.abs(leadingVisibleEdge - leadingCellEdge);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
                        else if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
                            // Hide rest of leading cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
                            return leadingCellEdge + cellBounds.width - leadingVisibleEdge;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
                            // Hide rest of leading cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
                            return leadingVisibleEdge - cellBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                    // ASSUME: All cells are the same width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                    return cellBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        Font f = getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
        return (f != null) ? f.getSize() : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
     * Returns the distance to scroll to expose the next or previous block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
     * For vertical scrolling, the following rules are used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
     * <li>if scrolling down, returns the distance to scroll so that the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
     * visible element becomes the first completely visible element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
     * <li>if scrolling up, returns the distance to scroll so that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
     * visible element becomes the last completely visible element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
     * <li>returns {@code visibleRect.height} if the list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
     * For horizontal scrolling, when the layout orientation is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
     * {@code VERTICAL_WRAP} or {@code HORIZONTAL_WRAP}:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
     * <li>if scrolling right, returns the distance to scroll so that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
     * last visible element becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
     * the first completely visible element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
     * <li>if scrolling left, returns the distance to scroll so that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
     * visible element becomes the last completely visible element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
     * <li>returns {@code visibleRect.width} if the list is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
     * For horizontal scrolling and {@code VERTICAL} orientation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
     * returns {@code visibleRect.width}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
     * Note that the value of {@code visibleRect} must be the equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * {@code this.getVisibleRect()}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
     * @param visibleRect the view area visible within the viewport
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * @param orientation {@code SwingConstants.HORIZONTAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     *                    {@code SwingConstants.VERTICAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
     * @param direction less or equal to zero to scroll up/back,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
     *                  greater than zero for down/forward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * @return the "block" increment for scrolling in the specified direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     *         always positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
     * @see #getScrollableUnitIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     * @see Scrollable#getScrollableBlockIncrement
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * @throws IllegalArgumentException if {@code visibleRect} is {@code null}, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
     *         {@code orientation} isn't one of {@code SwingConstants.VERTICAL} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
     *         {@code SwingConstants.HORIZONTAL}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
        checkScrollableParameters(visibleRect, orientation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
        if (orientation == SwingConstants.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
            int inc = visibleRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            /* Scroll Down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
            if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
                // last cell is the lowest left cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
                int last = locationToIndex(new Point(visibleRect.x, visibleRect.y+visibleRect.height-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
                if (last != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
                    Rectangle lastRect = getCellBounds(last,last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                    if (lastRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
                        inc = lastRect.y - visibleRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
                        if ( (inc == 0) && (last < getModel().getSize()-1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                            inc = lastRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            /* Scroll Up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
                int newFirst = locationToIndex(new Point(visibleRect.x, visibleRect.y-visibleRect.height));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
                int first = getFirstVisibleIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
                if (newFirst != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
                    if (first == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
                        first = locationToIndex(visibleRect.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                    Rectangle newFirstRect = getCellBounds(newFirst,newFirst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                    Rectangle firstRect = getCellBounds(first,first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
                    if ((newFirstRect != null) && (firstRect!=null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
                        while ( (newFirstRect.y + visibleRect.height <
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
                                 firstRect.y + firstRect.height) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
                                (newFirstRect.y < firstRect.y) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
                            newFirst++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
                            newFirstRect = getCellBounds(newFirst,newFirst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                        inc = visibleRect.y - newFirstRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                        if ( (inc <= 0) && (newFirstRect.y > 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                            newFirst--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
                            newFirstRect = getCellBounds(newFirst,newFirst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
                            if (newFirstRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                                inc = visibleRect.y - newFirstRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
            return inc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
        else if (orientation == SwingConstants.HORIZONTAL &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
                 getLayoutOrientation() != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
            boolean leftToRight = getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
            int inc = visibleRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
            /* Scroll Right (in ltr mode) or Scroll Left (in rtl mode) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
            if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                // position is upper right if ltr, or upper left otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
                int x = visibleRect.x + (leftToRight ? (visibleRect.width - 1) : 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                int last = locationToIndex(new Point(x, visibleRect.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                if (last != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                    Rectangle lastRect = getCellBounds(last,last);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
                    if (lastRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
                        if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
                            inc = lastRect.x - visibleRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
                            inc = visibleRect.x + visibleRect.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
                                      - (lastRect.x + lastRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
                        if (inc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
                            inc += lastRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
                        } else if ( (inc == 0) && (last < getModel().getSize()-1) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
                            inc = lastRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
            /* Scroll Left (in ltr mode) or Scroll Right (in rtl mode) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                // position is upper left corner of the visibleRect shifted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                // left by the visibleRect.width if ltr, or upper right shifted
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                // right by the visibleRect.width otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
                int x = visibleRect.x + (leftToRight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
                                         ? -visibleRect.width
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                                         : visibleRect.width - 1 + visibleRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                int first = locationToIndex(new Point(x, visibleRect.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                if (first != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                    Rectangle firstRect = getCellBounds(first,first);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                    if (firstRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                        // the right of the first cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                        int firstRight = firstRect.x + firstRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2724
                        if (leftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                            if ((firstRect.x < visibleRect.x - visibleRect.width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                                    && (firstRight < visibleRect.x)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                                inc = visibleRect.x - firstRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                                inc = visibleRect.x - firstRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                            int visibleRight = visibleRect.x + visibleRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                            if ((firstRight > visibleRight + visibleRect.width)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                                    && (firstRect.x > visibleRight)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                                inc = firstRect.x - visibleRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                                inc = firstRight - visibleRight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
            return inc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        return visibleRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
     * Returns {@code true} if this {@code JList} is displayed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     * {@code JViewport} and the viewport is wider than the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
     * preferred width, or if the layout orientation is {@code HORIZONTAL_WRAP}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
     * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
     * If {@code false}, then don't track the viewport's width. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
     * horizontal scrolling if the {@code JViewport} is itself embedded in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
     * {@code JScrollPane}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
     * @return whether or not an enclosing viewport should force the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
     *         width to match its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
     * @see Scrollable#getScrollableTracksViewportWidth
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2764
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    public boolean getScrollableTracksViewportWidth() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
        if (getLayoutOrientation() == HORIZONTAL_WRAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
                                      getVisibleRowCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
        }
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2770
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2771
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2772
            return parent.getWidth() > getPreferredSize().width;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
     * Returns {@code true} if this {@code JList} is displayed in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * {@code JViewport} and the viewport is taller than the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * preferred height, or if the layout orientation is {@code VERTICAL_WRAP}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * and {@code visibleRowCount <= 0}; otherwise returns {@code false}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * If {@code false}, then don't track the viewport's height. This allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     * vertical scrolling if the {@code JViewport} is itself embedded in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * {@code JScrollPane}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * @return whether or not an enclosing viewport should force the list's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     *         height to match its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     * @see Scrollable#getScrollableTracksViewportHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2791
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
    public boolean getScrollableTracksViewportHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
        if (getLayoutOrientation() == VERTICAL_WRAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                     getVisibleRowCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
        }
5449
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2797
        Container parent = SwingUtilities.getUnwrappedParent(this);
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2798
        if (parent instanceof JViewport) {
21bec46ee9e3 6899413: Fix for CR #6878399 should be refactored
alexp
parents: 4378
diff changeset
  2799
            return parent.getHeight() > getPreferredSize().height;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * See {@code readObject} and {@code writeObject} in {@code JComponent}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * for more information about serialization in Swing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
        if (getUIClassID().equals(uiClassID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
            byte count = JComponent.getWriteObjCounter(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
            JComponent.setWriteObjCounter(this, --count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
            if (count == 0 && ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                ui.installUI(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
     * Returns a {@code String} representation of this {@code JList}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
     * This method is intended to be used only for debugging purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
     * and the content and format of the returned {@code String} may vary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
     * between implementations. The returned {@code String} may be empty,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
     * but may not be {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
     * @return  a {@code String} representation of this {@code JList}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    protected String paramString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
        String selectionForegroundString = (selectionForeground != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
                                            selectionForeground.toString() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
                                            "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
        String selectionBackgroundString = (selectionBackground != null ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
                                            selectionBackground.toString() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
                                            "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
        return super.paramString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
        ",fixedCellHeight=" + fixedCellHeight +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
        ",fixedCellWidth=" + fixedCellWidth +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
        ",horizontalScrollIncrement=" + horizontalScrollIncrement +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
        ",selectionBackground=" + selectionBackgroundString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
        ",selectionForeground=" + selectionForegroundString +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
        ",visibleRowCount=" + visibleRowCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
        ",layoutOrientation=" + layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
     * --- Accessibility Support ---
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
     * Gets the {@code AccessibleContext} associated with this {@code JList}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
     * For {@code JList}, the {@code AccessibleContext} takes the form of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
     * {@code AccessibleJList}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
     * A new {@code AccessibleJList} instance is created if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
     * @return an {@code AccessibleJList} that serves as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
     *         {@code AccessibleContext} of this {@code JList}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
     */
33253
78e735319356 4763438: Replace uses of @beaninfo with meta facility in core j2se
serb
parents: 32865
diff changeset
  2863
    @BeanProperty(bound = false)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
    public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
        if (accessibleContext == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
            accessibleContext = new AccessibleJList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
        return accessibleContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
     * This class implements accessibility support for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
     * {@code JList} class. It provides an implementation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
     * Java Accessibility API appropriate to list user-interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
     * elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
     * the same version of Swing.  As of 1.4, support for long term storage
20458
f2423fb3fd19 8025840: Fix all the doclint warnings about trademark
cl
parents: 20455
diff changeset
  2882
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 21982
diff changeset
  2886
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2887
    protected class AccessibleJList extends AccessibleJComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
        implements AccessibleSelection, PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2889
        ListSelectionListener, ListDataListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2890
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2891
        int leadSelectionIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2892
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  2893
        /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  2894
         * Constructs an {@code AccessibleJList}.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  2895
         */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2896
        public AccessibleJList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2897
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2898
            JList.this.addPropertyChangeListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2899
            JList.this.getSelectionModel().addListSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2900
            JList.this.getModel().addListDataListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2901
            leadSelectionIndex = JList.this.getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2902
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2904
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2905
         * Property Change Listener change method. Used to track changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2906
         * to the DataModel and ListSelectionModel, in order to re-set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2907
         * listeners to those for reporting changes there via the Accessibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2908
         * PropertyChange mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2909
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2910
         * @param e PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2911
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2912
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2913
            String name = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2914
            Object oldValue = e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2915
            Object newValue = e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2917
                // re-set listData listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2918
            if (name.compareTo("model") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2919
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2920
                if (oldValue != null && oldValue instanceof ListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2921
                    ((ListModel) oldValue).removeListDataListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2922
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2923
                if (newValue != null && newValue instanceof ListModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2924
                    ((ListModel) newValue).addListDataListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2925
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2927
                // re-set listSelectionModel listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2928
            } else if (name.compareTo("selectionModel") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2929
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2930
                if (oldValue != null && oldValue instanceof ListSelectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2931
                    ((ListSelectionModel) oldValue).removeListSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2932
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2933
                if (newValue != null && newValue instanceof ListSelectionModel) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2934
                    ((ListSelectionModel) newValue).addListSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2935
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2936
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2937
                firePropertyChange(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2938
                    AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2939
                    Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2940
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2943
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2944
         * List Selection Listener value change method. Used to fire
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2945
         * the property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2946
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2947
         * @param e ListSelectionEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2948
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2949
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2950
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2951
            int oldLeadSelectionIndex = leadSelectionIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2952
            leadSelectionIndex = JList.this.getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2953
            if (oldLeadSelectionIndex != leadSelectionIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2954
                Accessible oldLS, newLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2955
                oldLS = (oldLeadSelectionIndex >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2956
                        ? getAccessibleChild(oldLeadSelectionIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2957
                        : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2958
                newLS = (leadSelectionIndex >= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2959
                        ? getAccessibleChild(leadSelectionIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2960
                        : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2961
                firePropertyChange(AccessibleContext.ACCESSIBLE_ACTIVE_DESCENDANT_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2962
                                   oldLS, newLS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2963
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2964
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2965
            firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2966
                               Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2967
            firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2968
                               Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2969
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2970
            // Process the State changes for Multiselectable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2971
            AccessibleStateSet s = getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2972
            ListSelectionModel lsm = JList.this.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2973
            if (lsm.getSelectionMode() != ListSelectionModel.SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2974
                if (!s.contains(AccessibleState.MULTISELECTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2975
                    s.add(AccessibleState.MULTISELECTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2976
                    firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2977
                                       null, AccessibleState.MULTISELECTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2978
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2979
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2980
                if (s.contains(AccessibleState.MULTISELECTABLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2981
                    s.remove(AccessibleState.MULTISELECTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2982
                    firePropertyChange(AccessibleContext.ACCESSIBLE_STATE_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2983
                                       AccessibleState.MULTISELECTABLE, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2984
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2986
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2988
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2989
         * List Data Listener interval added method. Used to fire the visible data property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2990
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2991
         * @param e ListDataEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2992
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2993
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2994
        public void intervalAdded(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2995
            firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2996
                               Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2999
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3000
         * List Data Listener interval removed method. Used to fire the visible data property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3001
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
         * @param e ListDataEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        public void intervalRemoved(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3006
            firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
                               Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3010
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3011
         * List Data Listener contents changed method. Used to fire the visible data property change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3012
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
         * @param e ListDataEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3015
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
         public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
             firePropertyChange(AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
                                Boolean.valueOf(false), Boolean.valueOf(true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3019
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3021
    // AccessibleContext methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
         * Get the state set of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3026
         * @return an instance of AccessibleState containing the current state
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
         * of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
         * @see AccessibleState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3030
        public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3031
            AccessibleStateSet states = super.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
            if (selectionModel.getSelectionMode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
                ListSelectionModel.SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3034
                states.add(AccessibleState.MULTISELECTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3035
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
            return states;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3039
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
         * Get the role of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
         * @return an instance of AccessibleRole describing the role of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
         * object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
         * @see AccessibleRole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
        public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
            return AccessibleRole.LIST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
         * Returns the <code>Accessible</code> child contained at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3052
         * the local coordinate <code>Point</code>, if one exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
         * Otherwise returns <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3055
         * @return the <code>Accessible</code> at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3056
         *    location, if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3057
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
        public Accessible getAccessibleAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3059
            int i = locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
            if (i >= 0) {
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3061
                return new ActionableAccessibleJListChild(JList.this, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3063
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3064
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3065
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3067
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3068
         * Returns the number of accessible children in the object.  If all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3069
         * of the children of this object implement Accessible, than this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3070
         * method should return the number of children of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3072
         * @return the number of accessible children in the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
        public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
            return getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
         * Return the nth Accessible child of the object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3080
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3081
         * @param i zero-based index of child
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3082
         * @return the nth Accessible child of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3083
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3084
        public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3085
            if (i >= getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3086
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3087
            } else {
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3088
                return new ActionableAccessibleJListChild(JList.this, i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3089
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3091
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
         * Get the AccessibleSelection associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3094
         * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
         * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
         * AccessibleSelection interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3097
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
         * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
        public AccessibleSelection getAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3101
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
    // AccessibleSelection methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3108
         * Returns the number of items currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3109
         * If no items are selected, the return value will be 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3110
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
         * @return the number of items currently selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
         public int getAccessibleSelectionCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
             return JList.this.getSelectedIndices().length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
         * Returns an Accessible representing the specified selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
         * in the object.  If there isn't a selection, or there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
         * fewer items selected than the integer passed in, the return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
         * value will be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3123
         * @param i the zero-based index of selected items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
         * @return an Accessible containing the selected item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
         public Accessible getAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3127
             int len = getAccessibleSelectionCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
             if (i < 0 || i >= len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3129
                 return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3130
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3131
                 return getAccessibleChild(JList.this.getSelectedIndices()[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3132
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3133
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3135
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3136
         * Returns true if the current child of this object is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3138
         * @param i the zero-based index of the child in this Accessible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3139
         * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3140
         * @see AccessibleContext#getAccessibleChild
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
        public boolean isAccessibleChildSelected(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3143
            return isSelectedIndex(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3146
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
         * Adds the specified selected item in the object to the object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3148
         * selection.  If the object supports multiple selections,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
         * the specified item is added to any existing selection, otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
         * it replaces any existing selection in the object.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
         * specified item is already selected, this method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3152
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
         * @param i the zero-based index of selectable items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3155
         public void addAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3156
             JList.this.addSelectionInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3157
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
         * Removes the specified selected item in the object from the object's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
         * selection.  If the specified item isn't currently selected, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3162
         * method has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3163
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3164
         * @param i the zero-based index of selectable items
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3165
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
         public void removeAccessibleSelection(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
             JList.this.removeSelectionInterval(i, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3170
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
         * Clears the selection in the object, so that nothing in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
         * object is selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3173
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3174
         public void clearAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3175
             JList.this.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3176
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3178
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3179
         * Causes every selected item in the object to be selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3180
         * if the object supports multiple selections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3182
         public void selectAllAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3183
             JList.this.addSelectionInterval(0, getAccessibleChildrenCount() -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3184
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3186
          /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3187
           * This class implements accessibility support appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3188
           * for list children.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3189
           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3190
        protected class AccessibleJListChild extends AccessibleContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3191
                implements Accessible, AccessibleComponent {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3192
            private JList<E>     parent = null;
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3193
            int indexInParent;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3194
            private Component component = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3195
            private AccessibleContext accessibleContext = null;
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3196
            private ListModel<E> listModel;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3197
            private ListCellRenderer<? super E> cellRenderer = null;
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3198
30464
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  3199
            /**
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  3200
             * Constructs an {@code AccessibleJListChild}.
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  3201
             * @param parent the parent
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  3202
             * @param indexInParent the index in the parent
929bafd0db6f 8075082: Fix missing doclint warnings in the javax.swing package
darcy
parents: 28236
diff changeset
  3203
             */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3204
            public AccessibleJListChild(JList<E> parent, int indexInParent) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3205
                this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3206
                this.setAccessibleParent(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3207
                this.indexInParent = indexInParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3208
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3209
                    listModel = parent.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
                    cellRenderer = parent.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3214
            private Component getCurrentComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                return getComponentAtIndex(indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3217
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3218
            AccessibleContext getCurrentAccessibleContext() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3219
                Component c = getComponentAtIndex(indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3220
                if (c instanceof Accessible) {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 466
diff changeset
  3221
                    return c.getAccessibleContext();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3226
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
            private Component getComponentAtIndex(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3228
                if (index < 0 || index >= listModel.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3229
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3230
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3231
                if ((parent != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                        && (listModel != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3233
                        && cellRenderer != null) {
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 4278
diff changeset
  3234
                    E value = listModel.getElementAt(index);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3235
                    boolean isSelected = parent.isSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                    boolean isFocussed = parent.isFocusOwner()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3237
                            && (index == parent.getLeadSelectionIndex());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                    return cellRenderer.getListCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                            parent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                            value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                            index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                            isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                            isFocussed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            // Accessible Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
           /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
            * Get the AccessibleContext for this object. In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
            * returns this object, which is its own AccessibleContext.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
            *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
            * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
            public AccessibleContext getAccessibleContext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3259
                return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
            // AccessibleContext methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
            public String getAccessibleName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
                    return ac.getAccessibleName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3270
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
            public void setAccessibleName(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3275
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3276
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
                    ac.setAccessibleName(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
            public String getAccessibleDescription() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
                    return ac.getAccessibleDescription();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
            public void setAccessibleDescription(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
                    ac.setAccessibleDescription(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
            public AccessibleRole getAccessibleRole() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3298
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3299
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3300
                    return ac.getAccessibleRole();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
            public AccessibleStateSet getAccessibleStateSet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
                AccessibleStateSet s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3310
                    s = ac.getAccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3312
                    s = new AccessibleStateSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3315
                s.add(AccessibleState.SELECTABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
                if (parent.isFocusOwner()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
                    && (indexInParent == parent.getLeadSelectionIndex())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3318
                    s.add(AccessibleState.ACTIVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3319
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3320
                if (parent.isSelectedIndex(indexInParent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
                    s.add(AccessibleState.SELECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
                if (this.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
                    s.add(AccessibleState.SHOWING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3325
                } else if (s.contains(AccessibleState.SHOWING)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
                    s.remove(AccessibleState.SHOWING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3328
                if (this.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3329
                    s.add(AccessibleState.VISIBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3330
                } else if (s.contains(AccessibleState.VISIBLE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
                    s.remove(AccessibleState.VISIBLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3332
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3333
                s.add(AccessibleState.TRANSIENT); // cell-rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
                return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
            public int getAccessibleIndexInParent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
                return indexInParent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
            public int getAccessibleChildrenCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3344
                    return ac.getAccessibleChildrenCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
            public Accessible getAccessibleChild(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3351
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3352
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3353
                    Accessible accessibleChild = ac.getAccessibleChild(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3354
                    ac.setAccessibleParent(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3355
                    return accessibleChild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3356
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            public Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
                    return ac.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3369
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3370
            public void addPropertyChangeListener(PropertyChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3371
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3372
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                    ac.addPropertyChangeListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
            public void removePropertyChangeListener(PropertyChangeListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                    ac.removePropertyChangeListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
           /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
            * Get the AccessibleComponent associated with this object.  In the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
            * implementation of the Java Accessibility API for this class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
            * return this object, which is responsible for implementing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
            * AccessibleComponent interface on behalf of itself.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
            *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
            * @return this object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
            public AccessibleComponent getAccessibleComponent() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                return this; // to override getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            public AccessibleSelection getAccessibleSelection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                return getCurrentAccessibleContext().getAccessibleSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
            public AccessibleText getAccessibleText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
                return getCurrentAccessibleContext().getAccessibleText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3403
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
            public AccessibleValue getAccessibleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
                return getCurrentAccessibleContext().getAccessibleValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
            // AccessibleComponent methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
            public Color getBackground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3412
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3414
                    return ((AccessibleComponent) ac).getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3415
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3416
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3417
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3418
                        return c.getBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3419
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
            public void setBackground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3426
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3427
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
                    ((AccessibleComponent) ac).setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
                    Component cp = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
                    if (cp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
                        cp.setBackground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
            public Color getForeground() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3440
                    return ((AccessibleComponent) ac).getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3441
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3442
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3443
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
                        return c.getForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3451
            public void setForeground(Color c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
                    ((AccessibleComponent) ac).setForeground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                    Component cp = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
                    if (cp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
                        cp.setForeground(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3461
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3462
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3463
            public Cursor getCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3464
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3465
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3466
                    return ((AccessibleComponent) ac).getCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3467
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3468
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3470
                        return c.getCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3471
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3472
                        Accessible ap = getAccessibleParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3473
                        if (ap instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3474
                            return ((AccessibleComponent) ap).getCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3475
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3476
                            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
            public void setCursor(Cursor c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3483
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3485
                    ((AccessibleComponent) ac).setCursor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3486
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3487
                    Component cp = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3488
                    if (cp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3489
                        cp.setCursor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3490
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
            public Font getFont() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
                    return ((AccessibleComponent) ac).getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3498
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3499
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3500
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
                        return c.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3508
            public void setFont(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3509
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3510
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3511
                    ((AccessibleComponent) ac).setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3512
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3513
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3514
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3515
                        c.setFont(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3518
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3519
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3520
            public FontMetrics getFontMetrics(Font f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3521
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3522
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3523
                    return ((AccessibleComponent) ac).getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3524
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3525
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
                        return c.getFontMetrics(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
            public boolean isEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
                    return ((AccessibleComponent) ac).isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                        return c.isEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
            public void setEnabled(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
                    ((AccessibleComponent) ac).setEnabled(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
                        c.setEnabled(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
            public boolean isVisible() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
                int fi = parent.getFirstVisibleIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
                int li = parent.getLastVisibleIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                // The UI incorrectly returns a -1 for the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
                // visible index if the list is smaller than the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
                // viewport size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
                if (li == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
                    li = parent.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3568
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3569
                return ((indexInParent >= fi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3570
                        && (indexInParent <= li));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3571
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3573
            public void setVisible(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3574
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3576
            public boolean isShowing() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3577
                return (parent.isShowing() && isVisible());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3578
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3580
            public boolean contains(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3581
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3582
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3583
                    Rectangle r = ((AccessibleComponent) ac).getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3584
                    return r.contains(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3585
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3586
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3587
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3588
                        Rectangle r = c.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3589
                        return r.contains(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3590
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3591
                        return getBounds().contains(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3592
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3593
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3594
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3596
            public Point getLocationOnScreen() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3597
                if (parent != null) {
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3598
                    Point listLocation;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3599
                    try {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3600
                        listLocation = parent.getLocationOnScreen();
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3601
                    } catch (IllegalComponentStateException e) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3602
                        // This can happen if the component isn't visisble
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3603
                        return null;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3604
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3605
                    Point componentLocation = parent.indexToLocation(indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3606
                    if (componentLocation != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3607
                        componentLocation.translate(listLocation.x, listLocation.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3608
                        return componentLocation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3609
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3610
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3611
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3612
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3613
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3614
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3617
            public Point getLocation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3618
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3619
                    return parent.indexToLocation(indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3620
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3621
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3622
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3623
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3625
            public void setLocation(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3626
                if ((parent != null)  && (parent.contains(p))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3627
                    ensureIndexIsVisible(indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3628
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3629
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3630
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3631
            public Rectangle getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3632
                if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3633
                    return parent.getCellBounds(indexInParent,indexInParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3634
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3635
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3636
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3639
            public void setBounds(Rectangle r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3640
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3641
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3642
                    ((AccessibleComponent) ac).setBounds(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3646
            public Dimension getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3647
                Rectangle cellBounds = this.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3648
                if (cellBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3649
                    return cellBounds.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3650
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3651
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3652
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3654
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3655
            public void setSize (Dimension d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3656
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3657
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3658
                    ((AccessibleComponent) ac).setSize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3659
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3660
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3661
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3662
                        c.setSize(d);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3663
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3664
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3667
            public Accessible getAccessibleAt(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3668
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3669
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3670
                    return ((AccessibleComponent) ac).getAccessibleAt(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3671
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3672
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3673
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3675
28231
b608ffcaed74 8066621: Suppress deprecation warnings in java.desktop module
darcy
parents: 25859
diff changeset
  3676
            @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3677
            public boolean isFocusTraversable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3678
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3679
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3680
                    return ((AccessibleComponent) ac).isFocusTraversable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3681
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3682
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3683
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3684
                        return c.isFocusTraversable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3685
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3686
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3687
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3688
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3691
            public void requestFocus() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3692
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3693
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3694
                    ((AccessibleComponent) ac).requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3695
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3696
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3697
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3698
                        c.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3699
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3700
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3701
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3703
            public void addFocusListener(FocusListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3704
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3705
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3706
                    ((AccessibleComponent) ac).addFocusListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3707
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3708
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3709
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3710
                        c.addFocusListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3711
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3715
            public void removeFocusListener(FocusListener l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3716
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3717
                if (ac instanceof AccessibleComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3718
                    ((AccessibleComponent) ac).removeFocusListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3719
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3720
                    Component c = getCurrentComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3721
                    if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3722
                        c.removeFocusListener(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3723
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3724
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3725
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3726
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3727
            // TIGER - 4733624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3728
            /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3729
             * Returns the icon for the element renderer, as the only item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3730
             * of an array of <code>AccessibleIcon</code>s or a <code>null</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3731
             * if the renderer component contains no icons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3732
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3733
             * @return an array containing the accessible icon
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3734
             *         or a <code>null</code> array if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3735
             * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3736
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
            public AccessibleIcon [] getAccessibleIcon() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3738
                AccessibleContext ac = getCurrentAccessibleContext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3739
                if (ac != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3740
                    return ac.getAccessibleIcon();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3741
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3742
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3743
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3744
            }
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3745
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3746
        } // inner class AccessibleJListChild
39873
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3747
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3748
        private class ActionableAccessibleJListChild
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3749
            extends AccessibleJListChild
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3750
            implements AccessibleAction {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3751
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3752
            ActionableAccessibleJListChild(JList<E> parent, int indexInParent) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3753
                super(parent, indexInParent);
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3754
            }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3755
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3756
            @Override
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3757
            public AccessibleAction getAccessibleAction() {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3758
                AccessibleContext ac = getCurrentAccessibleContext();
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3759
                if (ac == null) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3760
                    return null;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3761
                } else {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3762
                    AccessibleAction aa = ac.getAccessibleAction();
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3763
                    if (aa != null) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3764
                        return aa;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3765
                    } else {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3766
                        return this;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3767
                    }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3768
                }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3769
            }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3770
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3771
            @Override
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3772
            public boolean doAccessibleAction(int i) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3773
                if (i == 0) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3774
                    JList.this.setSelectedIndex(indexInParent);
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3775
                    return true;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3776
                } else {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3777
                    return false;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3778
                }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3779
            }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3780
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3781
            @Override
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3782
            public String getAccessibleActionDescription(int i) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3783
                if (i == 0) {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3784
                    return UIManager.getString("AbstractButton.clickText");
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3785
                } else {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3786
                    return null;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3787
                }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3788
            }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3789
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3790
            @Override
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3791
            public int getAccessibleActionCount() {
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3792
                return 1;
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3793
            }
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3794
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3795
        } // inner class ActionableAccessibleJListChild
76907ae1b680 8145207: [macosx] JList, VO can't access non-visible list items
ptbrunet
parents: 39858
diff changeset
  3796
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3797
    } // inner class AccessibleJList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3798
}