jdk/src/share/classes/javax/swing/plaf/basic/BasicListUI.java
author darcy
Sun, 23 Mar 2014 13:49:48 -0700
changeset 23697 e556a715949f
parent 22574 7f8ce0c8c20a
child 24969 afa6934dd8e8
permissions -rw-r--r--
8034169: Fix serial lint warnings in javax.swing Reviewed-by: alanb, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 4394
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: 4394
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: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.text.Position;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.awt.datatransfer.Transferable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * An extensible implementation of {@code ListUI}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * {@code BasicListUI} instances cannot be shared between multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * lists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @author Shannon Hickey (drag and drop)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
public class BasicListUI extends ListUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final StringBuilder BASELINE_COMPONENT_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        new StringBuilder("List.baselineComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected JList list = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected CellRendererPane rendererPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Listeners that this UI attaches to the JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected FocusListener focusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected MouseInputListener mouseInputListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected ListSelectionListener listSelectionListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    protected ListDataListener listDataListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected PropertyChangeListener propertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected int[] cellHeights = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected int cellHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    protected int cellWidth = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected int updateLayoutStateNeeded = modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Height of the list. When asked to paint, if the current size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * the list differs, this will update the layout state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private int listHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Width of the list. When asked to paint, if the current size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * the list differs, this will update the layout state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private int listWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * The layout orientation of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private int layoutOrientation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    // Following ivars are used if the list is laying out horizontally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Number of columns to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private int columnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Preferred height to make the list, this is only used if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * the list is layed out horizontally.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private int preferredHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Number of rows per column. This is only used if the row height is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * The time factor to treate the series of typed alphanumeric key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * as prefix for first letter navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private long timeFactor = 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Local cache of JList's client property "List.isFileList"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private boolean isFileList = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Local cache of JList's component orientation property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private boolean isLeftToRight = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* The bits below define JList property changes that affect layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * When one of these properties changes we set a bit in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * updateLayoutStateNeeded.  The change is dealt with lazily, see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * maybeUpdateLayoutState.  Changes to the JLists model, e.g. the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * models length changed, are handled similarly, see DataListener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    protected final static int modelChanged = 1 << 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected final static int selectionModelChanged = 1 << 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected final static int fontChanged = 1 << 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    protected final static int fixedCellWidthChanged = 1 << 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    protected final static int fixedCellHeightChanged = 1 << 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected final static int prototypeCellValueChanged = 1 << 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected final static int cellRendererChanged = 1 << 6;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private final static int layoutOrientationChanged = 1 << 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private final static int heightChanged = 1 << 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private final static int widthChanged = 1 << 9;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private final static int componentOrientationChanged = 1 << 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static final int DROP_LINE_THICKNESS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        map.put(new Actions(Actions.SELECT_PREVIOUS_COLUMN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        map.put(new Actions(Actions.SELECT_PREVIOUS_COLUMN_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        map.put(new Actions(Actions.SELECT_PREVIOUS_COLUMN_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        map.put(new Actions(Actions.SELECT_NEXT_COLUMN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        map.put(new Actions(Actions.SELECT_NEXT_COLUMN_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        map.put(new Actions(Actions.SELECT_NEXT_COLUMN_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        map.put(new Actions(Actions.SELECT_PREVIOUS_ROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        map.put(new Actions(Actions.SELECT_PREVIOUS_ROW_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        map.put(new Actions(Actions.SELECT_PREVIOUS_ROW_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        map.put(new Actions(Actions.SELECT_NEXT_ROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        map.put(new Actions(Actions.SELECT_NEXT_ROW_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        map.put(new Actions(Actions.SELECT_NEXT_ROW_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        map.put(new Actions(Actions.SELECT_FIRST_ROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        map.put(new Actions(Actions.SELECT_FIRST_ROW_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        map.put(new Actions(Actions.SELECT_FIRST_ROW_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        map.put(new Actions(Actions.SELECT_LAST_ROW));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        map.put(new Actions(Actions.SELECT_LAST_ROW_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        map.put(new Actions(Actions.SELECT_LAST_ROW_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        map.put(new Actions(Actions.SCROLL_UP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        map.put(new Actions(Actions.SCROLL_UP_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        map.put(new Actions(Actions.SCROLL_UP_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        map.put(new Actions(Actions.SCROLL_DOWN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        map.put(new Actions(Actions.SCROLL_DOWN_EXTEND));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_LEAD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        map.put(new Actions(Actions.SELECT_ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        map.put(new Actions(Actions.CLEAR_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        map.put(new Actions(Actions.ADD_TO_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        map.put(new Actions(Actions.EXTEND_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        map.put(new Actions(Actions.MOVE_SELECTION_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        map.put(TransferHandler.getCutAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Paint one List cell: compute the relevant state, get the "rubber stamp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * cell renderer component, and then use the CellRendererPane to paint it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Subclasses may want to override this method rather than paint().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @see #paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected void paintCell(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        Rectangle rowBounds,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        ListCellRenderer cellRenderer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        ListModel dataModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        ListSelectionModel selModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int leadIndex)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Object value = dataModel.getElementAt(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        boolean cellHasFocus = list.hasFocus() && (row == leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        boolean isSelected = selModel.isSelectedIndex(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        Component rendererComponent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            cellRenderer.getListCellRendererComponent(list, value, row, isSelected, cellHasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int cx = rowBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int cy = rowBounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        int cw = rowBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int ch = rowBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            // Shrink renderer to preferred size. This is mostly used on Windows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            // where selection is only shown around the file name, instead of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            // across the whole list cell.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            int w = Math.min(cw, rendererComponent.getPreferredSize().width + 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if (!isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                cx += (cw - w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            cw = w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        rendererPane.paintComponent(g, rendererComponent, list, cx, cy, cw, ch, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Paint the rows that intersect the Graphics objects clipRect.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * method calls paintCell as necessary.  Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * may want to override these methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @see #paintCell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Shape clip = g.getClip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        paintImpl(g, c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        g.setClip(clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        paintDropLine(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private void paintImpl(Graphics g, JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        switch (layoutOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case JList.VERTICAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            if (list.getHeight() != listHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                updateLayoutStateNeeded |= heightChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        case JList.HORIZONTAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (list.getWidth() != listWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                updateLayoutStateNeeded |= widthChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        ListCellRenderer renderer = list.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        ListModel dataModel = list.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        ListSelectionModel selModel = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        if ((renderer == null) || (size = dataModel.getSize()) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        // Determine how many columns we need to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        Rectangle paintBounds = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        int startColumn, endColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (c.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            startColumn = convertLocationToColumn(paintBounds.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                                                  paintBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            endColumn = convertLocationToColumn(paintBounds.x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                                                paintBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                                                paintBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            startColumn = convertLocationToColumn(paintBounds.x +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                                                paintBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                                paintBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            endColumn = convertLocationToColumn(paintBounds.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                                                  paintBounds.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int maxY = paintBounds.y + paintBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int leadIndex = adjustIndex(list.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        int rowIncrement = (layoutOrientation == JList.HORIZONTAL_WRAP) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                           columnCount : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        for (int colCounter = startColumn; colCounter <= endColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
             colCounter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // And then how many rows in this columnn
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            int row = convertLocationToRowInColumn(paintBounds.y, colCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            int rowCount = getRowCount(colCounter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            int index = getModelIndex(colCounter, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            Rectangle rowBounds = getCellBounds(list, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (rowBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                // Not valid, bail!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            while (row < rowCount && rowBounds.y < maxY &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                   index < size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                rowBounds.height = getHeight(colCounter, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                g.setClip(rowBounds.x, rowBounds.y, rowBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                          rowBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                g.clipRect(paintBounds.x, paintBounds.y, paintBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                           paintBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                paintCell(g, index, rowBounds, renderer, dataModel, selModel,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                          leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                rowBounds.y += rowBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                index += rowIncrement;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                row++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        // Empty out the renderer pane, allowing renderers to be gc'ed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        rendererPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    private void paintDropLine(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        JList.DropLocation loc = list.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (loc == null || !loc.isInsert()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        Color c = DefaultLookup.getColor(list, this, "List.dropLineColor", null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (c != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            g.setColor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            Rectangle rect = getDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private Rectangle getDropLineRect(JList.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (size == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            if (layoutOrientation == JList.HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    return new Rectangle(insets.left, insets.top, DROP_LINE_THICKNESS, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    return new Rectangle(list.getWidth() - DROP_LINE_THICKNESS - insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                         insets.top, DROP_LINE_THICKNESS, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                return new Rectangle(insets.left, insets.top,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                     list.getWidth() - insets.left - insets.right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                                     DROP_LINE_THICKNESS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        Rectangle rect = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        int index = loc.getIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        boolean decr = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (layoutOrientation == JList.HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            if (index == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                decr = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            } else if (index != 0 && convertModelToRow(index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                         != convertModelToRow(index - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                Rectangle prev = getCellBounds(list, index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                Rectangle me = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                Point p = loc.getDropPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    decr = Point2D.distance(prev.x + prev.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                                            prev.y + (int)(prev.height / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                            p.x, p.y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                           < Point2D.distance(me.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                                              me.y + (int)(me.height / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                                              p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    decr = Point2D.distance(prev.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                                            prev.y + (int)(prev.height / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                                            p.x, p.y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                           < Point2D.distance(me.x + me.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                                              me.y + (int)(prev.height / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                                              p.x, p.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (decr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    rect.x += rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    rect.x -= DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                if (!isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    rect.x += rect.width - DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (rect.x >= list.getWidth()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                rect.x = list.getWidth() - DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            } else if (rect.x < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                rect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            rect.width = DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        } else if (layoutOrientation == JList.VERTICAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (index == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                rect.y += rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            } else if (index != 0 && convertModelToColumn(index)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                         != convertModelToColumn(index - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                Rectangle prev = getCellBounds(list, index - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                Rectangle me = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                Point p = loc.getDropPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                if (Point2D.distance(prev.x + (int)(prev.width / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                     prev.y + prev.height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                     p.x, p.y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                        < Point2D.distance(me.x + (int)(me.width / 2.0),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                                           me.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                                           p.x, p.y)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                    rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    rect.y += rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            if (rect.y >= list.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                rect.y = list.getHeight() - DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            rect.height = DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            if (index == size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                index--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                rect.y += rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                rect = getCellBounds(list, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            if (rect.y >= list.getHeight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                rect.y = list.getHeight() - DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            rect.height = DROP_LINE_THICKNESS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        return rect;
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
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        int rowHeight = list.getFixedCellHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        Component renderer = (Component)lafDefaults.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                BASELINE_COMPONENT_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (renderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            ListCellRenderer lcr = (ListCellRenderer)UIManager.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    "List.cellRenderer");
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   483
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   484
            // fix for 6711072 some LAFs like Nimbus do not provide this
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   485
            // UIManager key and we should not through a NPE here because of it
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   486
            if (lcr == null) {
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   487
                lcr = new DefaultListCellRenderer();
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
   488
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            renderer = lcr.getListCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    list, "a", -1, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        renderer.setFont(list.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        // JList actually has much more complex behavior here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        // If rowHeight != -1 the rowHeight is either the max of all cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        // heights (layout orientation != VERTICAL), or is variable depending
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        // upon the cell.  We assume a default size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // We could theoretically query the real renderer, but that would
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        // not work for an empty model and the results may vary with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // the content.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        if (rowHeight == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            rowHeight = renderer.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        return renderer.getBaseline(Integer.MAX_VALUE, rowHeight) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                list.getInsets().top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * The preferredSize of the list depends upon the layout orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * <table summary="Describes the preferred size for each layout orientation">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * <tr><th>Layout Orientation</th><th>Preferred Size</th></tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *   <td>JList.VERTICAL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *   <td>The preferredSize of the list is total height of the rows
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *       and the maximum width of the cells.  If JList.fixedCellHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *       is specified then the total height of the rows is just
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *       (cellVerticalMargins + fixedCellHeight) * model.getSize() where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *       rowVerticalMargins is the space we allocate for drawing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *       the yellow focus outline.  Similarly if fixedCellWidth is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *       specified then we just use that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     *   </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     *   <td>JList.VERTICAL_WRAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     *   <td>If the visible row count is greater than zero, the preferredHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     *       is the maximum cell height * visibleRowCount. If the visible row
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 9230
diff changeset
   540
     *       count is &lt;= 0, the preferred height is either the current height
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     *       of the list, or the maximum cell height, whichever is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *       bigger. The preferred width is than the maximum cell width *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *       number of columns needed. Where the number of columns needs is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *       list.height / max cell height. Max cell height is either the fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *       cell height, or is determined by iterating through all the cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *       to find the maximum height from the ListCellRenderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *   <td>JList.HORIZONTAL_WRAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     *   <td>If the visible row count is greater than zero, the preferredHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     *       is the maximum cell height * adjustedRowCount.  Where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     *       visibleRowCount is used to determine the number of columns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *       Because this lays out horizontally the number of rows is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *       then determined from the column count.  For example, lets say
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     *       you have a model with 10 items and the visible row count is 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *       The number of columns needed to display this is 2, but you no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *       longer need 8 rows to display this, you only need 5, thus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *       the adjustedRowCount is 5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *       <p>If the visible row
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 9230
diff changeset
   559
     *       count is &lt;= 0, the preferred height is dictated by the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     *       number of columns, which will be as many as can fit in the width
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *       of the <code>JList</code> (width / max cell width), with at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     *       least one column.  The preferred height then becomes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *       model size / number of columns * maximum cell height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *       Max cell height is either the fixed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *       cell height, or is determined by iterating through all the cells
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *       to find the maximum height from the ListCellRenderer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * The above specifies the raw preferred width and height. The resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * preferred width is the above width + insets.left + insets.right and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * the resulting preferred height is the above height + insets.top +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * insets.bottom. Where the <code>Insets</code> are determined from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <code>list.getInsets()</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * @param c The JList component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * @return The total size of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        int lastRow = list.getModel().getSize() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        if (lastRow < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            return new Dimension(0, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        int width = cellWidth * columnCount + insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        if (layoutOrientation != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            height = preferredHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            Rectangle bounds = getCellBounds(list, lastRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                height = bounds.y + bounds.height + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return new Dimension(width, height);
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
     * Selected the previous row and force it to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @see JList#ensureIndexIsVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    protected void selectPreviousIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        int s = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if(s > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            s -= 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            list.setSelectedIndex(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            list.ensureIndexIsVisible(s);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * Selected the previous row and force it to be visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @see JList#ensureIndexIsVisible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    protected void selectNextIndex()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        int s = list.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        if((s + 1) < list.getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            s += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            list.setSelectedIndex(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            list.ensureIndexIsVisible(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Registers the keyboard bindings on the <code>JList</code> that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * <code>BasicListUI</code> is associated with. This method is called at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * installUI() time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                                           inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        LazyActionMap.installLazyActionMap(list, BasicListUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                                           "List.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        if (condition == JComponent.WHEN_FOCUSED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            InputMap keyMap = (InputMap)DefaultLookup.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                             list, this, "List.focusInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            if (isLeftToRight ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                ((rtlKeyMap = (InputMap)DefaultLookup.get(list, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                              "List.focusInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                    return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Unregisters keyboard actions installed from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * <code>installKeyboardActions</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * This method is called at uninstallUI() time - subclassess should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * ensure that all of the keyboard actions registered at installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * time are removed here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        SwingUtilities.replaceUIActionMap(list, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   688
     * Creates and installs the listeners for the JList, its model, and its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * selectionModel.  This method is called at installUI() time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @see #uninstallListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    protected void installListeners()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        TransferHandler th = list.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            list.setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            // default TransferHandler doesn't support drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // so we don't want drop handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            if (list.getDropTarget() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                list.setDropTarget(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        focusListener = createFocusListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        mouseInputListener = createMouseInputListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        propertyChangeListener = createPropertyChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        listSelectionListener = createListSelectionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        listDataListener = createListDataListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        list.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        list.addMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        list.addMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        list.addPropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        list.addKeyListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        ListModel model = list.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            model.addListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        ListSelectionModel selectionModel = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        if (selectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
            selectionModel.addListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   731
     * Removes the listeners from the JList, its model, and its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * selectionModel.  All of the listener fields, are reset to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * null here.  This method is called at uninstallUI() time,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * it should be kept in sync with installListeners.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    protected void uninstallListeners()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        list.removeFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        list.removeMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        list.removeMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        list.removePropertyChangeListener(propertyChangeListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        list.removeKeyListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        ListModel model = list.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        if (model != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            model.removeListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        ListSelectionModel selectionModel = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        if (selectionModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            selectionModel.removeListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        focusListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        mouseInputListener  = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        listSelectionListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        listDataListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        propertyChangeListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   767
     * Initializes list properties such as font, foreground, and background,
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   768
     * and adds the CellRendererPane. The font, foreground, and background
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * properties are only set if their current value is either null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * or a UIResource, other properties are set if the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * @see #uninstallDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @see CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    protected void installDefaults()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        list.setLayout(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        LookAndFeel.installBorder(list, "List.border");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        LookAndFeel.installColorsAndFont(list, "List.background", "List.foreground", "List.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        LookAndFeel.installProperty(list, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        if (list.getCellRenderer() == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            list.setCellRenderer((ListCellRenderer)(UIManager.get("List.cellRenderer")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        Color sbg = list.getSelectionBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        if (sbg == null || sbg instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            list.setSelectionBackground(UIManager.getColor("List.selectionBackground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        Color sfg = list.getSelectionForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        if (sfg == null || sfg instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            list.setSelectionForeground(UIManager.getColor("List.selectionForeground"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        Long l = (Long)UIManager.get("List.timeFactor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        timeFactor = (l!=null) ? l.longValue() : 1000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        updateIsFileList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    private void updateIsFileList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        boolean b = Boolean.TRUE.equals(list.getClientProperty("List.isFileList"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        if (b != isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            isFileList = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            Font oldFont = list.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (oldFont == null || oldFont instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                Font newFont = UIManager.getFont(b ? "FileChooser.listFont" : "List.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                if (newFont != null && newFont != oldFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                    list.setFont(newFont);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   823
     * Sets the list properties that have not been explicitly overridden to
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   824
     * {@code null}. A property is considered overridden if its current value
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 3977
diff changeset
   825
     * is not a {@code UIResource}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @see #uninstallUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     * @see CellRendererPane
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    protected void uninstallDefaults()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        LookAndFeel.uninstallBorder(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (list.getFont() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            list.setFont(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if (list.getForeground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            list.setForeground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        if (list.getBackground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
            list.setBackground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        if (list.getSelectionBackground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            list.setSelectionBackground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        if (list.getSelectionForeground() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            list.setSelectionForeground(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        if (list.getCellRenderer() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            list.setCellRenderer(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
        if (list.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            list.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * Initializes <code>this.list</code> by calling <code>installDefaults()</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * <code>installListeners()</code>, and <code>installKeyboardActions()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * in order.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @see #installDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @see #installListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @see #installKeyboardActions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public void installUI(JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        list = (JList)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        layoutOrientation = list.getLayoutOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
        rendererPane = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        list.add(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        columnCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        updateLayoutStateNeeded = modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        isLeftToRight = list.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
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
     * Uninitializes <code>this.list</code> by calling <code>uninstallListeners()</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * <code>uninstallKeyboardActions()</code>, and <code>uninstallDefaults()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * in order.  Sets this.list to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * @see #uninstallListeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * @see #uninstallKeyboardActions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @see #uninstallDefaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    public void uninstallUI(JComponent c)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        cellWidth = cellHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        cellHeights = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        listWidth = listHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        list.remove(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        rendererPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        list = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * Returns a new instance of BasicListUI.  BasicListUI delegates are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * allocated one per JList.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * @return A new ListUI implementation for the Windows look and feel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    public static ComponentUI createUI(JComponent list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        return new BasicListUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    public int locationToIndex(JList list, Point location) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        return convertLocationToModel(location.x, location.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
    public Point indexToLocation(JList list, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        Rectangle rect = getCellBounds(list, index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            return new Point(rect.x, rect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     * {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    public Rectangle getCellBounds(JList list, int index1, int index2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        int minIndex = Math.min(index1, index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        int maxIndex = Math.max(index1, index2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        if (minIndex >= list.getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        Rectangle minBounds = getCellBounds(list, minIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        if (minBounds == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        if (minIndex == maxIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            return minBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        Rectangle maxBounds = getCellBounds(list, maxIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        if (maxBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (layoutOrientation == JList.HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                int minRow = convertModelToRow(minIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                int maxRow = convertModelToRow(maxIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                if (minRow != maxRow) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                    minBounds.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    minBounds.width = list.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            else if (minBounds.x != maxBounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                // Different columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
                minBounds.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                minBounds.height = list.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            minBounds.add(maxBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        return minBounds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * Gets the bounds of the specified model index, returning the resulting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * bounds, or null if <code>index</code> is not valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
    private Rectangle getCellBounds(JList list, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
        maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        int row = convertModelToRow(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
        int column = convertModelToColumn(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        if (row == -1 || column == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        int w = cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        int h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
        switch (layoutOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
        case JList.VERTICAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
        case JList.HORIZONTAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                x = insets.left + column * cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                x = list.getWidth() - insets.right - (column+1) * cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            y += cellHeight * row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            h = cellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
            x = insets.left;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            if (cellHeights == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                y += (cellHeight * row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
            else if (row >= cellHeights.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                for(int i = 0; i < row; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                    y += cellHeights[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
            w = list.getWidth() - (insets.left + insets.right);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
            h = getRowHeight(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
        return new Rectangle(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * Returns the height of the specified row based on the current layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * @return The specified row height or -1 if row isn't valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     * @see #convertYToRow
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @see #convertRowToY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     * @see #updateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    protected int getRowHeight(int row)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
        return getHeight(0, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * Convert the JList relative coordinate to the row that contains it,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * based on the current layout.  If y0 doesn't fall within any row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * return -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     * @return The row that contains y0, or -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
     * @see #getRowHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
     * @see #updateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
    protected int convertYToRow(int y0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        return convertLocationToRow(0, y0, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     * Return the JList relative Y coordinate of the origin of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * row or -1 if row isn't valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @return The Y coordinate of the origin of row, or -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * @see #getRowHeight
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @see #updateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    protected int convertRowToY(int row)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        if (row >= getRowCount(0) || row < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        Rectangle bounds = getCellBounds(list, row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        return bounds.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * Returns the height of the cell at the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    private int getHeight(int column, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        if (column < 0 || column > columnCount || row < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        if (layoutOrientation != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            return cellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        if (row >= list.getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        return (cellHeights == null) ? cellHeight :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                           ((row < cellHeights.length) ? cellHeights[row] : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
     * Returns the row at location x/y.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
     * @param closest If true and the location doesn't exactly match a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     *                particular location, this will return the closest row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    private int convertLocationToRow(int x, int y0, boolean closest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        if (cellHeights == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            int row = (cellHeight == 0) ? 0 :
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                           ((y0 - insets.top) / cellHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            if (closest) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                if (row < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
                    row = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                else if (row >= size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                    row = size - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
        else if (size > cellHeights.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            int y = insets.top;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
            int row = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            if (closest && y0 < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
            for (i = 0; i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                if ((y0 >= y) && (y0 < y + cellHeights[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                    return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                y += cellHeights[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                row += 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            return i - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     * Returns the closest row that starts at the specified y-location
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * in the passed in column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
    private int convertLocationToRowInColumn(int y, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        int x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (layoutOrientation != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                x = column * cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                x = list.getWidth() - (column+1)*cellWidth - list.getInsets().right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        return convertLocationToRow(x, y, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * Returns the closest location to the model index of the passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    private int convertLocationToModel(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        int row = convertLocationToRow(x, y, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        int column = convertLocationToColumn(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        if (row >= 0 && column >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            return getModelIndex(column, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * Returns the number of rows in the given column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
    private int getRowCount(int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        if (column < 0 || column >= columnCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        if (layoutOrientation == JList.VERTICAL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                  (column == 0 && columnCount == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            return list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        if (column >= columnCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        if (layoutOrientation == JList.VERTICAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            if (column < (columnCount - 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                return rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            return list.getModel().getSize() - (columnCount - 1) *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                        rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        // JList.HORIZONTAL_WRAP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        int diff = columnCount - (columnCount * rowsPerColumn -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                                  list.getModel().getSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if (column >= diff) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            return Math.max(0, rowsPerColumn - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Returns the model index for the specified display location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * If <code>column</code>x<code>row</code> is beyond the length of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * model, this will return the model size - 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
    private int getModelIndex(int column, int row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        switch (layoutOrientation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        case JList.VERTICAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            return Math.min(list.getModel().getSize() - 1, rowsPerColumn *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
                            column + Math.min(row, rowsPerColumn-1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        case JList.HORIZONTAL_WRAP:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            return Math.min(list.getModel().getSize() - 1, row * columnCount +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                            column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return row;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * Returns the closest column to the passed in location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
    private int convertLocationToColumn(int x, int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        if (cellWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            if (layoutOrientation == JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            int col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            if (isLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                col = (x - insets.left) / cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                col = (list.getWidth() - x - insets.right - 1) / cellWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if (col < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            else if (col >= columnCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
                return columnCount - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            return col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * Returns the row that the model index <code>index</code> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     * displayed in..
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
    private int convertModelToRow(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        if ((index < 0) || (index >= size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        if (layoutOrientation != JList.VERTICAL && columnCount > 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                                                   rowsPerColumn > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
            if (layoutOrientation == JList.VERTICAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                return index % rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            return index / columnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * Returns the column that the model index <code>index</code> will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     * displayed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    private int convertModelToColumn(int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
        int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
        if ((index < 0) || (index >= size)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
        if (layoutOrientation != JList.VERTICAL && rowsPerColumn > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                                                   columnCount > 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            if (layoutOrientation == JList.VERTICAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                return index / rowsPerColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            return index % columnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * If updateLayoutStateNeeded is non zero, call updateLayoutState() and reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * updateLayoutStateNeeded.  This method should be called by methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * before doing any computation based on the geometry of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * For example it's the first call in paint() and getPreferredSize().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * @see #updateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    protected void maybeUpdateLayoutState()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        if (updateLayoutStateNeeded != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            updateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            updateLayoutStateNeeded = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * Recompute the value of cellHeight or cellHeights based
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * and cellWidth, based on the current font and the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * values of fixedCellWidth, fixedCellHeight, and prototypeCellValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * @see #maybeUpdateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
    protected void updateLayoutState()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        /* If both JList fixedCellWidth and fixedCellHeight have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
         * set, then initialize cellWidth and cellHeight, and set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
         * cellHeights to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        int fixedCellHeight = list.getFixedCellHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
        int fixedCellWidth = list.getFixedCellWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        cellWidth = (fixedCellWidth != -1) ? fixedCellWidth : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if (fixedCellHeight != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            cellHeight = fixedCellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
            cellHeights = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
            cellHeight = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
            cellHeights = new int[list.getModel().getSize()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        /* If either of  JList fixedCellWidth and fixedCellHeight haven't
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
         * been set, then initialize cellWidth and cellHeights by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
         * scanning through the entire model.  Note: if the renderer is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
         * null, we just set cellWidth and cellHeights[*] to zero,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
         * if they're not set already.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
        if ((fixedCellWidth == -1) || (fixedCellHeight == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            ListModel dataModel = list.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
            int dataModelSize = dataModel.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            ListCellRenderer renderer = list.getCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            if (renderer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
                for(int index = 0; index < dataModelSize; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
                    Object value = dataModel.getElementAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
                    Component c = renderer.getListCellRendererComponent(list, value, index, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
                    rendererPane.add(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
                    Dimension cellSize = c.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
                    if (fixedCellWidth == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
                        cellWidth = Math.max(cellSize.width, cellWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
                    if (fixedCellHeight == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
                        cellHeights[index] = cellSize.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
                if (cellWidth == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
                    cellWidth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
                if (cellHeights == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                    cellHeights = new int[dataModelSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
                for(int index = 0; index < dataModelSize; index++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                    cellHeights[index] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        columnCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        if (layoutOrientation != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            updateHorizontalLayoutState(fixedCellWidth, fixedCellHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * Invoked when the list is layed out horizontally to determine how
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * many columns to create.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * This updates the <code>rowsPerColumn, </code><code>columnCount</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * <code>preferredHeight</code> and potentially <code>cellHeight</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * instance variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    private void updateHorizontalLayoutState(int fixedCellWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
                                             int fixedCellHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        int visRows = list.getVisibleRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        int dataModelSize = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        Insets insets = list.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        listHeight = list.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        listWidth = list.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        if (dataModelSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            rowsPerColumn = columnCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
            preferredHeight = insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        int height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if (fixedCellHeight != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            height = fixedCellHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            // Determine the max of the renderer heights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
            int maxHeight = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
            if (cellHeights.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
                maxHeight = cellHeights[cellHeights.length - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
                for (int counter = cellHeights.length - 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
                     counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
                    maxHeight = Math.max(maxHeight, cellHeights[counter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            height = cellHeight = maxHeight;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
            cellHeights = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
        // The number of rows is either determined by the visible row
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        // count, or by the height of the list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        rowsPerColumn = dataModelSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if (visRows > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            rowsPerColumn = visRows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            columnCount = Math.max(1, dataModelSize / rowsPerColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
            if (dataModelSize > 0 && dataModelSize > rowsPerColumn &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
                dataModelSize % rowsPerColumn != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
                columnCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
            if (layoutOrientation == JList.HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
                // Because HORIZONTAL_WRAP flows differently, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
                // rowsPerColumn needs to be adjusted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
                rowsPerColumn = (dataModelSize / columnCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
                if (dataModelSize % columnCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                    rowsPerColumn++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
        else if (layoutOrientation == JList.VERTICAL_WRAP && height != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
            rowsPerColumn = Math.max(1, (listHeight - insets.top -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
                                         insets.bottom) / height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
            columnCount = Math.max(1, dataModelSize / rowsPerColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
            if (dataModelSize > 0 && dataModelSize > rowsPerColumn &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
                dataModelSize % rowsPerColumn != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
                columnCount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        else if (layoutOrientation == JList.HORIZONTAL_WRAP && cellWidth > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                 listWidth > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
            columnCount = Math.max(1, (listWidth - insets.left -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                                       insets.right) / cellWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            rowsPerColumn = dataModelSize / columnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
            if (dataModelSize % columnCount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                rowsPerColumn++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        preferredHeight = rowsPerColumn * cellHeight + insets.top +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
                              insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * Mouse input, and focus handling for JList.  An instance of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * class is added to the appropriate java.awt.Component lists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * at installUI() time.  Note keyboard input is handled with JComponent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * KeyboardActions, see installKeyboardActions().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * 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: 20169
diff changeset
  1491
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     * @see #createMouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @see #installKeyboardActions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
  1499
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    public class MouseInputHandler implements MouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            getHandler().mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
            getHandler().mouseEntered(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            getHandler().mouseExited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            getHandler().mouseDragged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            getHandler().mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     * Creates a delegate that implements MouseInputListener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     * The delegate is added to the corresponding java.awt.Component listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
     * lists at installUI() time. Subclasses can override this method to return
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * a custom MouseInputListener, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     * class MyListUI extends BasicListUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
     *    protected MouseInputListener <b>createMouseInputListener</b>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
     *        return new MyMouseInputHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     *    public class MyMouseInputHandler extends MouseInputHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
     *        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     *            // do some extra work when the mouse moves
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     *            super.mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     * @see MouseInputHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    protected MouseInputListener createMouseInputListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
        return getHandler();
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
     * This class should be treated as a &quot;protected&quot; inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 5506
diff changeset
  1560
     * Instantiate it only within subclasses of {@code BasicListUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
    public class FocusHandler implements FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        protected void repaintCellFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
            getHandler().repaintCellFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
        /* The focusGained() focusLost() methods run when the JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
         * focus changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    protected FocusListener createFocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        return getHandler();
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
     * The ListSelectionListener that's added to the JLists selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * model at installUI time, and whenever the JList.selectionModel property
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * changes.  When the selection changes we repaint the affected rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * 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: 20169
diff changeset
  1596
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     * @see #createListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     * @see #getCellBounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
  1604
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    public class ListSelectionHandler implements ListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
        public void valueChanged(ListSelectionEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
            getHandler().valueChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * Creates an instance of ListSelectionHandler that's added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * the JLists by selectionModel as needed.  Subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * this method to return a custom ListSelectionListener, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
     * class MyListUI extends BasicListUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     *    protected ListSelectionListener <b>createListSelectionListener</b>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     *        return new MySelectionListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     *    public class MySelectionListener extends ListSelectionHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     *        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *            // do some extra work when the selection changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     *            super.valueChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * @see ListSelectionHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    protected ListSelectionListener createListSelectionListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
    private void redrawList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        list.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        list.repaint();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * The ListDataListener that's added to the JLists model at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * installUI time, and whenever the JList.model property changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * 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: 20169
diff changeset
  1655
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * @see JList#getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * @see #maybeUpdateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * @see #createListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
  1664
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    public class ListDataHandler implements ListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        public void intervalAdded(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            getHandler().intervalAdded(e);
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
        public void intervalRemoved(ListDataEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
            getHandler().intervalRemoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
        public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
            getHandler().contentsChanged(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
     * Creates an instance of ListDataListener that's added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * the JLists by model as needed.  Subclasses can override
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     * this method to return a custom ListDataListener, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     * class MyListUI extends BasicListUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
     *    protected ListDataListener <b>createListDataListener</b>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
     *        return new MyListDataListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
     *    public class MyListDataListener extends ListDataHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
     *        public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
     *            // do some extra work when the models contents change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
     *            super.contentsChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * @see ListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * @see JList#getModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    protected ListDataListener createListDataListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * The PropertyChangeListener that's added to the JList at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     * installUI time.  When the value of a JList property that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
     * affects layout changes, we set a bit in updateLayoutStateNeeded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
     * If the JLists model changes we additionally remove our listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * from the old model.  Likewise for the JList selectionModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     * <strong>Warning:</strong>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * Serialized objects of this class will not be compatible with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     * future Swing releases. The current serialization support is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
     * appropriate for short term storage or RMI between applications running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * 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: 20169
diff changeset
  1723
     * of all JavaBeans&trade;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * has been added to the <code>java.beans</code> package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     * Please see {@link java.beans.XMLEncoder}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * @see #maybeUpdateLayoutState
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * @see #createPropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     */
22574
7f8ce0c8c20a 8032627: Add @SuppressWarnings("serial") to appropriate javax.swing classes
darcy
parents: 20458
diff changeset
  1731
    @SuppressWarnings("serial") // Same-version serialization only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
    public class PropertyChangeHandler implements PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
        public void propertyChange(PropertyChangeEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            getHandler().propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     * Creates an instance of PropertyChangeHandler that's added to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * the JList by installUI().  Subclasses can override this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     * to return a custom PropertyChangeListener, e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
     * class MyListUI extends BasicListUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
     *    protected PropertyChangeListener <b>createPropertyChangeListener</b>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
     *        return new MyPropertyChangeListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     *    public class MyPropertyChangeListener extends PropertyChangeHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     *        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     *            if (e.getPropertyName().equals("model")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *                // do some extra work when the model changes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     *            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     *            super.propertyChange(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     *        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     *    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
     * @see PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    protected PropertyChangeListener createPropertyChangeListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /** Used by IncrementLeadSelectionAction. Indicates the action should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * change the lead, and not select it. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    private static final int CHANGE_LEAD = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    /** Used by IncrementLeadSelectionAction. Indicates the action should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     * change the selection and lead. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
    private static final int CHANGE_SELECTION = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    /** Used by IncrementLeadSelectionAction. Indicates the action should
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * extend the selection from the anchor to the next index. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    private static final int EXTEND_SELECTION = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
        private static final String SELECT_PREVIOUS_COLUMN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                                    "selectPreviousColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        private static final String SELECT_PREVIOUS_COLUMN_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
                                    "selectPreviousColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        private static final String SELECT_PREVIOUS_COLUMN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
                                    "selectPreviousColumnChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
        private static final String SELECT_NEXT_COLUMN = "selectNextColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        private static final String SELECT_NEXT_COLUMN_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                                    "selectNextColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        private static final String SELECT_NEXT_COLUMN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
                                    "selectNextColumnChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        private static final String SELECT_PREVIOUS_ROW = "selectPreviousRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        private static final String SELECT_PREVIOUS_ROW_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
                                     "selectPreviousRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        private static final String SELECT_PREVIOUS_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                                     "selectPreviousRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        private static final String SELECT_NEXT_ROW = "selectNextRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        private static final String SELECT_NEXT_ROW_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
                                     "selectNextRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        private static final String SELECT_NEXT_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
                                     "selectNextRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        private static final String SELECT_FIRST_ROW = "selectFirstRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        private static final String SELECT_FIRST_ROW_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
                                     "selectFirstRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        private static final String SELECT_FIRST_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                                     "selectFirstRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        private static final String SELECT_LAST_ROW = "selectLastRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        private static final String SELECT_LAST_ROW_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
                                     "selectLastRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        private static final String SELECT_LAST_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                                     "selectLastRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        private static final String SCROLL_UP = "scrollUp";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        private static final String SCROLL_UP_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
                                     "scrollUpExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        private static final String SCROLL_UP_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
                                     "scrollUpChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
        private static final String SCROLL_DOWN = "scrollDown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
        private static final String SCROLL_DOWN_EXTEND =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
                                     "scrollDownExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        private static final String SCROLL_DOWN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
                                     "scrollDownChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        private static final String SELECT_ALL = "selectAll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
        private static final String CLEAR_SELECTION = "clearSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        // add the lead item to the selection without changing lead or anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        private static final String ADD_TO_SELECTION = "addToSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        // toggle the selected state of the lead item and move the anchor to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
        private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
        // extend the selection to the lead item
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        private static final String EXTEND_TO = "extendTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
        // move the anchor to the lead and ensure only that item is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
        private static final String MOVE_SELECTION_TO = "moveSelectionTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
        Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            String name = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
            JList list = (JList)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
            BasicListUI ui = (BasicListUI)BasicLookAndFeel.getUIOfType(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
                     list.getUI(), BasicListUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
            if (name == SELECT_PREVIOUS_COLUMN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
                                getNextColumnIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            else if (name == SELECT_PREVIOUS_COLUMN_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                                getNextColumnIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
            else if (name == SELECT_PREVIOUS_COLUMN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
                                getNextColumnIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
            else if (name == SELECT_NEXT_COLUMN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
                                getNextColumnIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
            else if (name == SELECT_NEXT_COLUMN_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
                                getNextColumnIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
            else if (name == SELECT_NEXT_COLUMN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
                                getNextColumnIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
            else if (name == SELECT_PREVIOUS_ROW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
                                getNextIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
            else if (name == SELECT_PREVIOUS_ROW_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
                                getNextIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            else if (name == SELECT_PREVIOUS_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
                                getNextIndex(list, ui, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
            else if (name == SELECT_NEXT_ROW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
                                getNextIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
            else if (name == SELECT_NEXT_ROW_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
                                getNextIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            else if (name == SELECT_NEXT_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
                                getNextIndex(list, ui, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
            else if (name == SELECT_FIRST_ROW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
                changeSelection(list, CHANGE_SELECTION, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            else if (name == SELECT_FIRST_ROW_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
                changeSelection(list, EXTEND_SELECTION, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            else if (name == SELECT_FIRST_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
                changeSelection(list, CHANGE_LEAD, 0, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            else if (name == SELECT_LAST_ROW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
                                list.getModel().getSize() - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            else if (name == SELECT_LAST_ROW_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
                                list.getModel().getSize() - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            else if (name == SELECT_LAST_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
                                list.getModel().getSize() - 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            else if (name == SCROLL_UP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
                                getNextPageIndex(list, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
            else if (name == SCROLL_UP_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
                                getNextPageIndex(list, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
            else if (name == SCROLL_UP_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                                getNextPageIndex(list, -1), -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
            else if (name == SCROLL_DOWN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
                changeSelection(list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
                                getNextPageIndex(list, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            else if (name == SCROLL_DOWN_EXTEND) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
                changeSelection(list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
                                getNextPageIndex(list, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            else if (name == SCROLL_DOWN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                changeSelection(list, CHANGE_LEAD,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
                                getNextPageIndex(list, 1), 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            else if (name == SELECT_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
                selectAll(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            else if (name == CLEAR_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
                clearSelection(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
            else if (name == ADD_TO_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
                int index = adjustIndex(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
                    list.getSelectionModel().getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
                if (!list.isSelectedIndex(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
                    int oldAnchor = list.getSelectionModel().getAnchorSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
                    list.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
                    list.addSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
                    list.getSelectionModel().setAnchorSelectionIndex(oldAnchor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
                    list.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
            else if (name == TOGGLE_AND_ANCHOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
                int index = adjustIndex(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
                    list.getSelectionModel().getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
                if (list.isSelectedIndex(index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
                    list.removeSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                    list.addSelectionInterval(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
            else if (name == EXTEND_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
                changeSelection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
                    list, EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
                    adjustIndex(list.getSelectionModel().getLeadSelectionIndex(), list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
                    0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
            else if (name == MOVE_SELECTION_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                changeSelection(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    list, CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    adjustIndex(list.getSelectionModel().getLeadSelectionIndex(), list),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                    0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        public boolean isEnabled(Object c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
            Object name = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
            if (name == SELECT_PREVIOUS_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                    name == SELECT_NEXT_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
                    name == SELECT_PREVIOUS_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
                    name == SELECT_NEXT_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
                    name == SELECT_FIRST_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
                    name == SELECT_LAST_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
                    name == SCROLL_UP_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
                    name == SCROLL_DOWN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
                // discontinuous selection actions are only enabled for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                // DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
                return c != null && ((JList)c).getSelectionModel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                                        instanceof DefaultListSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        private void clearSelection(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
        private void selectAll(JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
            int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
            if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                ListSelectionModel lsm = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                int lead = adjustIndex(lsm.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                if (lsm.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                    if (lead == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                        int min = adjustIndex(list.getMinSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                        lead = (min == -1 ? 0 : min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                    list.setSelectionInterval(lead, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                    list.ensureIndexIsVisible(lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                    list.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
                    int anchor = adjustIndex(lsm.getAnchorSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                    list.setSelectionInterval(0, size - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                    // this is done to restore the anchor and lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                    SwingUtilities2.setLeadAnchorWithoutSelection(lsm, anchor, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                    list.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        private int getNextPageIndex(JList list, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            if (list.getModel().getSize() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            int index = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            Rectangle visRect = list.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
            ListSelectionModel lsm = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            int lead = adjustIndex(lsm.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            Rectangle leadRect =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
                (lead==-1) ? new Rectangle() : list.getCellBounds(lead, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            if (list.getLayoutOrientation() == JList.VERTICAL_WRAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
                list.getVisibleRowCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
                if (!list.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
                    direction = -direction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
                // apply for horizontal scrolling: the step for next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                // page index is number of visible columns
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
                if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                    // left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                    visRect.x = leadRect.x + leadRect.width - visRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                    Point p = new Point(visRect.x - 1, leadRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                    index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                    Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                    if (visRect.intersects(cellBounds)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                        p.x = cellBounds.x - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                        cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                    // this is necessary for right-to-left orientation only
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                    if (cellBounds.y != leadRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                        p.x = cellBounds.x + cellBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                    // right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
                    visRect.x = leadRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
                    Point p = new Point(visRect.x + visRect.width, leadRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
                    index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
                    Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
                    if (visRect.intersects(cellBounds)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
                        p.x = cellBounds.x + cellBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
                        cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
                    if (cellBounds.y != leadRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
                        p.x = cellBounds.x - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
                if (direction < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
                    // up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
                    // go to the first visible cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
                    Point p = new Point(leadRect.x, visRect.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
                    index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
                    if (lead <= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                        // if lead is the first visible cell (or above it)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                        // adjust the visible rect up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                        visRect.y = leadRect.y + leadRect.height - visRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                        p.y = visRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                        Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                        // go one cell down if first visible cell doesn't fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                        // into adjasted visible rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
                        if (cellBounds.y < visRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
                            p.y = cellBounds.y + cellBounds.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
                            index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
                            cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
                        // if index isn't less then lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                        // try to go to cell previous to lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
                        if (cellBounds.y >= leadRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
                            p.y = leadRect.y - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
                            index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
                    // down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                    // go to the last completely visible cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
                    Point p = new Point(leadRect.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
                                        visRect.y + visRect.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
                    index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
                    Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                    // go up one cell if last visible cell doesn't fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                    // into visible rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
                    if (cellBounds.y + cellBounds.height >
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
                        visRect.y + visRect.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
                        p.y = cellBounds.y - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                        cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                        index = Math.max(index, lead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                    if (lead >= index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                        // if lead is the last completely visible index
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                        // (or below it) adjust the visible rect down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        visRect.y = leadRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                        p.y = visRect.y + visRect.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                        index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                        cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
                        // go one cell up if last visible cell doesn't fit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
                        // into adjasted visible rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
                        if (cellBounds.y + cellBounds.height >
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
                            visRect.y + visRect.height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
                            p.y = cellBounds.y - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
                            index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
                            cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                        // if index isn't greater then lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                        // try to go to cell next after lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                        if (cellBounds.y <= leadRect.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                            p.y = leadRect.y + leadRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                            index = list.locationToIndex(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                        }
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
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
        private void changeSelection(JList list, int type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                                     int index, int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
            if (index >= 0 && index < list.getModel().getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                ListSelectionModel lsm = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                // CHANGE_LEAD is only valid with multiple interval selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                if (type == CHANGE_LEAD &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                        list.getSelectionMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                            != ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
                    type = CHANGE_SELECTION;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                // IMPORTANT - This needs to happen before the index is changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                // This is because JFileChooser, which uses JList, also scrolls
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                // the selected item into view. If that happens first, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                // this method becomes a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                adjustScrollPositionIfNecessary(list, index, direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                if (type == EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                    int anchor = adjustIndex(lsm.getAnchorSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
                    if (anchor == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                        anchor = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                    list.setSelectionInterval(anchor, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                else if (type == CHANGE_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
                    list.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                    // casting should be safe since the action is only enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    // for DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    ((DefaultListSelectionModel)lsm).moveLeadSelectionIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
         * When scroll down makes selected index the last completely visible
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
         * index. When scroll up makes selected index the first visible index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
         * Adjust visible rectangle respect to list's component orientation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
        private void adjustScrollPositionIfNecessary(JList list, int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                                                     int direction) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
            if (direction == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
            Rectangle cellBounds = list.getCellBounds(index, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            Rectangle visRect = list.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
            if (cellBounds != null && !visRect.contains(cellBounds)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                if (list.getLayoutOrientation() == JList.VERTICAL_WRAP &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
                    list.getVisibleRowCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
                    // horizontal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
                    if (list.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
                        if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
                            // right for left-to-right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
                            int x =Math.max(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
                                cellBounds.x + cellBounds.width - visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                            int startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
                                list.locationToIndex(new Point(x, cellBounds.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
                            Rectangle startRect = list.getCellBounds(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
                                                                     startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
                            if (startRect.x < x && startRect.x < cellBounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
                                startRect.x += startRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
                                startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
                                    list.locationToIndex(startRect.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
                                startRect = list.getCellBounds(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
                                                               startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
                            cellBounds = startRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
                        cellBounds.width = visRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
                        if (direction > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
                            // left for right-to-left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
                            int x = cellBounds.x + visRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2236
                            int rightIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2237
                                list.locationToIndex(new Point(x, cellBounds.y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
                            Rectangle rightRect = list.getCellBounds(rightIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
                                                                     rightIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2240
                            if (rightRect.x + rightRect.width > x &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
                                rightRect.x > cellBounds.x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
                                rightRect.width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2244
                            cellBounds.x = Math.max(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2245
                                rightRect.x + rightRect.width - visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
                            cellBounds.width = visRect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
                        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
                            cellBounds.x += Math.max(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
                                cellBounds.width - visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
                            // adjust width to fit into visible rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
                            cellBounds.width = Math.min(cellBounds.width,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
                                                        visRect.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
                    // vertical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
                    if (direction > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
                            (cellBounds.y < visRect.y ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
                                    cellBounds.y + cellBounds.height
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
                                            > visRect.y + visRect.height)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
                        //down
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
                        int y = Math.max(0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
                            cellBounds.y + cellBounds.height - visRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
                        int startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
                            list.locationToIndex(new Point(cellBounds.x, y));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                        Rectangle startRect = list.getCellBounds(startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                                                                 startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
                        if (startRect.y < y && startRect.y < cellBounds.y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
                            startRect.y += startRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
                            startIndex =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
                                list.locationToIndex(startRect.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                            startRect =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                                list.getCellBounds(startIndex, startIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
                        cellBounds = startRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                        cellBounds.height = visRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                        // adjust height to fit into visible rectangle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                        cellBounds.height = Math.min(cellBounds.height, visRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                list.scrollRectToVisible(cellBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
        private int getNextColumnIndex(JList list, BasicListUI ui,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                                       int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
            if (list.getLayoutOrientation() != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2292
                int index = adjustIndex(list.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2293
                int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2295
                if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2296
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
                } else if (size == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
                    // there's only one item so we should select it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
                    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
                } else if (ui == null || ui.columnCount <= 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
                int column = ui.convertModelToColumn(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
                int row = ui.convertModelToRow(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
                column += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
                if (column >= ui.columnCount || column < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
                    // No wrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
                int maxRowCount = ui.getRowCount(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
                if (row >= maxRowCount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
                return ui.getModelIndex(column, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
            // Won't change the selection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        private int getNextIndex(JList list, BasicListUI ui, int amount) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            int index = adjustIndex(list.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            if (index == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
                if (size > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
                    if (amount > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                        index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                        index = size - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
            } else if (size == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                // there's only one item so we should select it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
            } else if (list.getLayoutOrientation() == JList.HORIZONTAL_WRAP) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                    index += ui.columnCount * amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
                index += amount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
            return index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
    private class Handler implements FocusListener, KeyListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                          ListDataListener, ListSelectionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                          MouseInputListener, PropertyChangeListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                          BeforeDrag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
        // KeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
        private String prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
        private String typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        private long lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
         * Invoked when a key has been typed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
         * Moves the keyboard focus to the first element whose prefix matches the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
         * sequence of alphanumeric keys pressed by the user with delay less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
         * than value of <code>timeFactor</code> property (or 1000 milliseconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
         * if it is not defined). Subsequent same key presses move the keyboard
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
         * focus to the next object that starts with the same letter until another
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
         * key is pressed, then it is treated as the prefix with appropriate number
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
         * of the same letters followed by first typed another letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
            JList src = (JList)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            ListModel model = src.getModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2377
            if (model.getSize() == 0 || e.isAltDown() ||
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2378
                    BasicGraphicsUtils.isMenuShortcutKeyDown(e) ||
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2379
                    isNavigationKey(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
                // Nothing to select
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            boolean startingFromSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            char c = e.getKeyChar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            long time = e.getWhen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
            int startIndex = adjustIndex(src.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
            if (time - lastTime < timeFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                typedString += c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                if((prefix.length() == 1) && (c == prefix.charAt(0))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                    // Subsequent same key presses move the keyboard focus to the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                    // object that starts with the same letter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                    startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                    prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                startIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                typedString = "" + c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                prefix = typedString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
            lastTime = time;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            if (startIndex < 0 || startIndex >= model.getSize()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
                startingFromSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
                startIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
            int index = src.getNextMatch(prefix, startIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                                         Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
            if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                src.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
                src.ensureIndexIsVisible(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            } else if (startingFromSelection) { // wrap
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                index = src.getNextMatch(prefix, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                                         Position.Bias.Forward);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                    src.setSelectedIndex(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                    src.ensureIndexIsVisible(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
         * Invoked when a key has been pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2427
         * Checks to see if the key event is a navigation key to prevent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
         * dispatching these keys for the first letter navigation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
            if ( isNavigationKey(e) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
                prefix = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
                typedString = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
                lastTime = 0L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
         * Invoked when a key has been released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
         * See the class description for {@link KeyEvent} for a definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2441
         * a key released event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2442
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
        public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
         * Returns whether or not the supplied key event maps to a key that is used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
         * navigation.  This is used for optimizing key input by only passing non-
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
         * navigation keys to the first letter navigation mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        private boolean isNavigationKey(KeyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
            InputMap inputMap = list.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
            KeyStroke key = KeyStroke.getKeyStrokeForEvent(event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
            if (inputMap != null && inputMap.get(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
        public void propertyChange(PropertyChangeEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
            String propertyName = e.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
            /* If the JList.model property changes, remove our listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
             * listDataListener from the old model and add it to the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
            if (propertyName == "model") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
                ListModel oldModel = (ListModel)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
                ListModel newModel = (ListModel)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
                if (oldModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
                    oldModel.removeListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
                if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
                    newModel.addListDataListener(listDataListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
                updateLayoutStateNeeded |= modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
            /* If the JList.selectionModel property changes, remove our listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
             * listSelectionListener from the old selectionModel and add it to the new one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
            else if (propertyName == "selectionModel") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
                ListSelectionModel oldModel = (ListSelectionModel)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
                ListSelectionModel newModel = (ListSelectionModel)e.getNewValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
                if (oldModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
                    oldModel.removeListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
                if (newModel != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
                    newModel.addListSelectionListener(listSelectionListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
                updateLayoutStateNeeded |= modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
            else if (propertyName == "cellRenderer") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
                updateLayoutStateNeeded |= cellRendererChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
            else if (propertyName == "font") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
                updateLayoutStateNeeded |= fontChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
            else if (propertyName == "prototypeCellValue") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
                updateLayoutStateNeeded |= prototypeCellValueChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
            else if (propertyName == "fixedCellHeight") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
                updateLayoutStateNeeded |= fixedCellHeightChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
            else if (propertyName == "fixedCellWidth") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
                updateLayoutStateNeeded |= fixedCellWidthChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
            else if (propertyName == "selectionForeground") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2519
                list.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2520
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
            else if (propertyName == "selectionBackground") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
                list.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
            else if ("layoutOrientation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
                updateLayoutStateNeeded |= layoutOrientationChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
                layoutOrientation = list.getLayoutOrientation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2527
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
            else if ("visibleRowCount" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
                if (layoutOrientation != JList.VERTICAL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
                    updateLayoutStateNeeded |= layoutOrientationChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2532
                    redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2533
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
            else if ("componentOrientation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
                isLeftToRight = list.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
                updateLayoutStateNeeded |= componentOrientationChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
                InputMap inputMap = getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
                SwingUtilities.replaceUIInputMap(list, JComponent.WHEN_FOCUSED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
                                                 inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
            } else if ("List.isFileList" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
                updateIsFileList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
                redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
            } else if ("dropLocation" == propertyName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                JList.DropLocation oldValue = (JList.DropLocation)e.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
                repaintDropLocation(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
                repaintDropLocation(list.getDropLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
        private void repaintDropLocation(JList.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
            if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
            Rectangle r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
            if (loc.isInsert()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
                r = getDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
                r = getCellBounds(list, loc.getIndex());
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 (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
                list.repaint(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        // ListDataListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        public void intervalAdded(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
            updateLayoutStateNeeded = modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
            int minIndex = Math.min(e.getIndex0(), e.getIndex1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
            int maxIndex = Math.max(e.getIndex0(), e.getIndex1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
            /* Sync the SelectionModel with the DataModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
            ListSelectionModel sm = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
                sm.insertIndexInterval(minIndex, maxIndex - minIndex+1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
            /* Repaint the entire list, from the origin of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
             * the first added cell, to the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
             * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
            redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
        public void intervalRemoved(ListDataEvent e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            updateLayoutStateNeeded = modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
            /* Sync the SelectionModel with the DataModel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2601
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
            ListSelectionModel sm = list.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
            if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
                sm.removeIndexInterval(e.getIndex0(), e.getIndex1());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            /* Repaint the entire list, from the origin of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
             * the first removed cell, to the bottom of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
             * component.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        public void contentsChanged(ListDataEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
            updateLayoutStateNeeded = modelChanged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
            redrawList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
        // ListSelectionListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2627
            maybeUpdateLayoutState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
            int size = list.getModel().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
            int firstIndex = Math.min(size - 1, Math.max(e.getFirstIndex(), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
            int lastIndex = Math.min(size - 1, Math.max(e.getLastIndex(), 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2633
            Rectangle bounds = getCellBounds(list, firstIndex, lastIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2635
            if (bounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2636
                list.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        // MouseListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        // Whether or not the mouse press (which is being considered as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
        // of a drag sequence) also caused the selection change to be fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        // processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        private boolean dragPressDidSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
            if (SwingUtilities2.shouldIgnore(e, list)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
            boolean dragEnabled = list.getDragEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
            boolean grabFocus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
            // different behavior if drag is enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
            if (dragEnabled) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
                int row = SwingUtilities2.loc2IndexFileList(list, e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
                // if we have a valid row and this is a drag initiating event
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
                if (row != -1 && DragRecognitionSupport.mousePressed(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
                    dragPressDidSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2672
                    if (BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
                        // do nothing for control - will be handled on release
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
                        // or when drag starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
                    } else if (!e.isShiftDown() && list.isSelectedIndex(row)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
                        // clicking on something that's already selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
                        // and need to make it the lead now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
                        list.addSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2680
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
                    // could be a drag initiating event - don't grab focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
                    grabFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
                    dragPressDidSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                // When drag is enabled mouse drags won't change the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                // in the list, so we only set the isAdjusting flag when it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
                // not enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
                list.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
            if (grabFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2696
                SwingUtilities2.adjustFocus(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
            adjustSelection(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
        private void adjustSelection(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
            int row = SwingUtilities2.loc2IndexFileList(list, e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
            if (row < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
                // If shift is down in multi-select, we should do nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
                // For single select or non-shift-click, clear the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
                if (isFileList &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
                    e.getID() == MouseEvent.MOUSE_PRESSED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
                    (!e.isShiftDown() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
                     list.getSelectionMode() == ListSelectionModel.SINGLE_SELECTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
                    list.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
                int anchorIndex = adjustIndex(list.getAnchorSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
                boolean anchorSelected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
                if (anchorIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
                    anchorIndex = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
                    anchorSelected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
                    anchorSelected = list.isSelectedIndex(anchorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2722
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2724
                if (BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
                    if (e.isShiftDown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
                        if (anchorSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
                            list.addSelectionInterval(anchorIndex, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
                            list.removeSelectionInterval(anchorIndex, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
                            if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
                                list.addSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
                                list.getSelectionModel().setAnchorSelectionIndex(anchorIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2734
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
                    } else if (list.isSelectedIndex(row)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
                        list.removeSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
                        list.addSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2739
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2740
                } else if (e.isShiftDown()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2741
                    list.setSelectionInterval(anchorIndex, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2742
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2743
                    list.setSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        public void dragStarting(MouseEvent me) {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2749
            if (BasicGraphicsUtils.isMenuShortcutKeyDown(me)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
                int row = SwingUtilities2.loc2IndexFileList(list, me.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
                list.addSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
            if (SwingUtilities2.shouldIgnore(e, list)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
            if (list.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2761
                DragRecognitionSupport.mouseDragged(e, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2763
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  2765
            if (e.isShiftDown() || BasicGraphicsUtils.isMenuShortcutKeyDown(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
            int row = locationToIndex(list, e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
            if (row != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
                // 4835633.  Dragging onto a File should not select it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2772
                if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
                Rectangle cellBounds = getCellBounds(list, row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
                if (cellBounds != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
                    list.scrollRectToVisible(cellBounds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2778
                    list.setSelectionInterval(row, row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
            if (SwingUtilities2.shouldIgnore(e, list)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
            if (list.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
                MouseEvent me = DragRecognitionSupport.mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
                if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
                    SwingUtilities2.adjustFocus(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
                    if (!dragPressDidSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2796
                        adjustSelection(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2798
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2799
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
                list.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2801
            }
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
        // FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
        protected void repaintCellFocus()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
            int leadIndex = adjustIndex(list.getLeadSelectionIndex(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
            if (leadIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
                Rectangle r = getCellBounds(list, leadIndex, leadIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
                if (r != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
                    list.repaint(r.x, r.y, r.width, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
        /* The focusGained() focusLost() methods run when the JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
         * focus changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
            repaintCellFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
            repaintCellFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
    private static int adjustIndex(int index, JList list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
        return index < list.getModel().getSize() ? index : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
    private static final TransferHandler defaultTransferHandler = new ListTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 22574
diff changeset
  2837
    @SuppressWarnings("serial") // Superclass is a JDK-implementation class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    static class ListTransferHandler extends TransferHandler implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
         * Create a Transferable to use as the source for a data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
         * @param c  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
         * @return  The representation of the data to be transfered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
        protected Transferable createTransferable(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
            if (c instanceof JList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
                JList list = (JList) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
                Object[] values = list.getSelectedValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
                if (values == null || values.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
                StringBuffer plainBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
                StringBuffer htmlBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
                htmlBuf.append("<html>\n<body>\n<ul>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
                for (int i = 0; i < values.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
                    Object obj = values[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
                    String val = ((obj == null) ? "" : obj.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
                    plainBuf.append(val + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
                    htmlBuf.append("  <li>" + val + "\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2870
                // remove the last newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2871
                plainBuf.deleteCharAt(plainBuf.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
                htmlBuf.append("</ul>\n</body>\n</html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
                return new BasicTransferable(plainBuf.toString(), htmlBuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2877
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2879
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2880
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2881
            return COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2882
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2885
}