jdk/src/share/classes/javax/swing/plaf/basic/BasicTableUI.java
author malenkov
Tue, 29 Oct 2013 17:01:06 +0400
changeset 21278 ef8a3a2a72f2
parent 17897 fb367e67916d
child 23697 e556a715949f
permissions -rw-r--r--
8022746: List of spelling errors in API doc Reviewed-by: alexsch, smarks
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17897
fb367e67916d 7068740: If you wrap a JTable in a JLayer you can't use the page up and page down cmds
vkarnauk
parents: 9230
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 3977
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: 3977
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: 3977
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
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 java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.datatransfer.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.dnd.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.EventObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.TooManyListenersException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.swing.plaf.basic.DragRecognitionSupport.BeforeDrag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.beans.PropertyChangeEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.beans.PropertyChangeListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.swing.DefaultLookup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import sun.swing.UIAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * BasicTableUI implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
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 BasicTableUI extends TableUI
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("Table.baselineComponent");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
// Instance Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // The JTable that is delegating the painting to this UI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    protected JTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected CellRendererPane rendererPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Listeners that are attached to the JTable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected KeyListener keyListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    protected FocusListener focusListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected MouseInputListener mouseInputListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private Handler handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Local cache of Table's client property "Table.isFileList"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private boolean isFileList = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
//  Helper class for keyboard actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        private static final String CANCEL_EDITING = "cancel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        private static final String SELECT_ALL = "selectAll";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        private static final String CLEAR_SELECTION = "clearSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        private static final String START_EDITING = "startEditing";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        private static final String NEXT_ROW = "selectNextRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        private static final String NEXT_ROW_CELL = "selectNextRowCell";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        private static final String NEXT_ROW_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                "selectNextRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        private static final String NEXT_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "selectNextRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        private static final String PREVIOUS_ROW = "selectPreviousRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private static final String PREVIOUS_ROW_CELL = "selectPreviousRowCell";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        private static final String PREVIOUS_ROW_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                "selectPreviousRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        private static final String PREVIOUS_ROW_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                "selectPreviousRowChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private static final String NEXT_COLUMN = "selectNextColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        private static final String NEXT_COLUMN_CELL = "selectNextColumnCell";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        private static final String NEXT_COLUMN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                "selectNextColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        private static final String NEXT_COLUMN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                "selectNextColumnChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private static final String PREVIOUS_COLUMN = "selectPreviousColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private static final String PREVIOUS_COLUMN_CELL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                "selectPreviousColumnCell";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        private static final String PREVIOUS_COLUMN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                "selectPreviousColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        private static final String PREVIOUS_COLUMN_CHANGE_LEAD =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                "selectPreviousColumnChangeLead";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        private static final String SCROLL_LEFT_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                "scrollLeftChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        private static final String SCROLL_LEFT_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                "scrollLeftExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        private static final String SCROLL_RIGHT_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                "scrollRightChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        private static final String SCROLL_RIGHT_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                "scrollRightExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        private static final String SCROLL_UP_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                "scrollUpChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        private static final String SCROLL_UP_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                "scrollUpExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        private static final String SCROLL_DOWN_CHANGE_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                "scrollDownChangeSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        private static final String SCROLL_DOWN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                "scrollDownExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        private static final String FIRST_COLUMN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                "selectFirstColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        private static final String FIRST_COLUMN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                "selectFirstColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        private static final String LAST_COLUMN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                "selectLastColumn";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        private static final String LAST_COLUMN_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                "selectLastColumnExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        private static final String FIRST_ROW =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                "selectFirstRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        private static final String FIRST_ROW_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                "selectFirstRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        private static final String LAST_ROW =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "selectLastRow";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        private static final String LAST_ROW_EXTEND_SELECTION =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                "selectLastRowExtendSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // add the lead item to the selection without changing lead or anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        private static final String ADD_TO_SELECTION = "addToSelection";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // toggle the selected state of the lead item and move the anchor to it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        private static final String TOGGLE_AND_ANCHOR = "toggleAndAnchor";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // extend the selection to the lead item
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        private static final String EXTEND_TO = "extendTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        // move the anchor to the lead and ensure only that item is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        private static final String MOVE_SELECTION_TO = "moveSelectionTo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // give focus to the JTableHeader, if one exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        private static final String FOCUS_HEADER = "focusHeader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        protected int dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        protected int dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        protected boolean extend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        protected boolean inSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        // horizontally, forwards always means right,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // regardless of component orientation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        protected boolean forwards;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        protected boolean vertically;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        protected boolean toLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        protected int leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        protected int leadColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        Actions(String name, int dx, int dy, boolean extend,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                boolean inSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            // Actions spcifying true for "inSelection" are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            // fairly sensitive to bad parameter values. They require
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            // that one of dx and dy be 0 and the other be -1 or 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            // Bogus parameter values could cause an infinite loop.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            // To prevent any problems we massage the params here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            // and complain if we get something we can't deal with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (inSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                this.inSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                // look at the sign of dx and dy only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                dx = sign(dx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                dy = sign(dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // make sure one is zero, but not both
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                assert (dx == 0 || dy == 0) && !(dx == 0 && dy == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            this.dx = dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            this.dy = dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            this.extend = extend;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Actions(String name, boolean extend, boolean forwards,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                boolean vertically, boolean toLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            this(name, 0, 0, extend, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            this.forwards = forwards;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            this.vertically = vertically;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            this.toLimit = toLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        private static int clipToRange(int i, int a, int b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            return Math.min(Math.max(i, a), b-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        private void moveWithinTableRange(JTable table, int dx, int dy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            leadRow = clipToRange(leadRow+dy, 0, table.getRowCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            leadColumn = clipToRange(leadColumn+dx, 0, table.getColumnCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        private static int sign(int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
         * Called to move within the selected range of the given JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * This method uses the table's notion of selection, which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         * important to allow the user to navigate between items visually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
         * selected on screen. This notion may or may not be the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
         * what could be determined by directly querying the selection models.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
         * It depends on certain table properties (such as whether or not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * row or column selection is allowed). When performing modifications,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * it is recommended that caution be taken in order to preserve
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * the intent of this method, especially when deciding whether to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * query the selection models or interact with JTable directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        private boolean moveWithinSelectedRange(JTable table, int dx, int dy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                ListSelectionModel rsm, ListSelectionModel csm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            // Note: The Actions constructor ensures that only one of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            // dx and dy is 0, and the other is either -1 or 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            // find out how many items the table is showing as selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            // and the range of items to navigate through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int totalCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int minX, maxX, minY, maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            boolean rs = table.getRowSelectionAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            boolean cs = table.getColumnSelectionAllowed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            // both column and row selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (rs && cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                totalCount = table.getSelectedRowCount() * table.getSelectedColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                minX = csm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                maxX = csm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                minY = rsm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                maxY = rsm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            // row selection only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            } else if (rs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                totalCount = table.getSelectedRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                minX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                maxX = table.getColumnCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                minY = rsm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                maxY = rsm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // column selection only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            } else if (cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                totalCount = table.getSelectedColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                minX = csm.getMinSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                maxX = csm.getMaxSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                minY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                maxY = table.getRowCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            // no selection allowed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                totalCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                // A bogus assignment to stop javac from complaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                // about unitialized values. In this case, these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                // won't even be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                minX = maxX = minY = maxY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            // For some cases, there is no point in trying to stay within the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            // selected area. Instead, move outside the selection, wrapping at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            // the table boundaries. The cases are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            boolean stayInSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // - nothing selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            if (totalCount == 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    // - one item selected, and the lead is already selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    (totalCount == 1 && table.isCellSelected(leadRow, leadColumn))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                stayInSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                maxX = table.getColumnCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                maxY = table.getRowCount() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // the mins are calculated like this in case the max is -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                minX = Math.min(0, maxX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                minY = Math.min(0, maxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                stayInSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // the algorithm below isn't prepared to deal with -1 lead/anchor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            // so massage appropriately here first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (dy == 1 && leadColumn == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                leadColumn = minX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                leadRow = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            } else if (dx == 1 && leadRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                leadRow = minY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                leadColumn = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else if (dy == -1 && leadColumn == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                leadColumn = maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                leadRow = maxY + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            } else if (dx == -1 && leadRow == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                leadRow = maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                leadColumn = maxX + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            // In cases where the lead is not within the search range,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            // we need to bring it within one cell for the the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // to work properly. Check these here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            leadRow = Math.min(Math.max(leadRow, minY - 1), maxY + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            leadColumn = Math.min(Math.max(leadColumn, minX - 1), maxX + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            // find the next position, possibly looping until it is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                calcNextPos(dx, minX, maxX, dy, minY, maxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            } while (stayInSelection && !table.isCellSelected(leadRow, leadColumn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            return stayInSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         * Find the next lead row and column based on the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * dx/dy and max/min values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        private void calcNextPos(int dx, int minX, int maxX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                 int dy, int minY, int maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (dx != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                leadColumn += dx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                if (leadColumn > maxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    leadColumn = minX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    leadRow++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (leadRow > maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        leadRow = minY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                } else if (leadColumn < minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    leadColumn = maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    leadRow--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    if (leadRow < minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                        leadRow = maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                leadRow += dy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                if (leadRow > maxY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    leadRow = minY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    leadColumn++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    if (leadColumn > maxX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                        leadColumn = minX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                } else if (leadRow < minY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    leadRow = maxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    leadColumn--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    if (leadColumn < minX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                        leadColumn = maxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            JTable table = (JTable)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            ListSelectionModel rsm = table.getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            leadRow = getAdjustedLead(table, true, rsm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            ListSelectionModel csm = table.getColumnModel().getSelectionModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            leadColumn = getAdjustedLead(table, false, csm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (key == SCROLL_LEFT_CHANGE_SELECTION ||        // Paging Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    key == SCROLL_LEFT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    key == SCROLL_RIGHT_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    key == SCROLL_RIGHT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    key == SCROLL_UP_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                    key == SCROLL_UP_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                    key == SCROLL_DOWN_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                    key == SCROLL_DOWN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    key == FIRST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                    key == FIRST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    key == FIRST_ROW ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    key == FIRST_ROW_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    key == LAST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    key == LAST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    key == LAST_ROW ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    key == LAST_ROW_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (toLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    if (vertically) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                        int rowCount = table.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                        this.dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                        this.dy = forwards ? rowCount : -rowCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        int colCount = table.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        this.dx = forwards ? colCount : -colCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                        this.dy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                else {
17897
fb367e67916d 7068740: If you wrap a JTable in a JLayer you can't use the page up and page down cmds
vkarnauk
parents: 9230
diff changeset
   422
                    if (!(SwingUtilities.getUnwrappedParent(table).getParent() instanceof
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                            JScrollPane)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    Dimension delta = table.getParent().getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                    if (vertically) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                        Rectangle r = table.getCellRect(leadRow, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                        if (forwards) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            // scroll by at least one cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                            r.y += Math.max(delta.height, r.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                            r.y -= delta.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        this.dx = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        int newRow = table.rowAtPoint(r.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                        if (newRow == -1 && forwards) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                            newRow = table.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                        this.dy = newRow - leadRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        Rectangle r = table.getCellRect(0, leadColumn, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        if (forwards) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                            // scroll by at least one cell
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                            r.x += Math.max(delta.width, r.width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                            r.x -= delta.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        int newColumn = table.columnAtPoint(r.getLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        if (newColumn == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                            boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                            newColumn = forwards ? (ltr ? table.getColumnCount() : 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                                                 : (ltr ? 0 : table.getColumnCount());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                        this.dx = newColumn - leadColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                        this.dy = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            if (key == NEXT_ROW ||  // Navigate Actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                    key == NEXT_ROW_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    key == NEXT_ROW_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    key == NEXT_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                    key == NEXT_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                    key == NEXT_COLUMN_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    key == NEXT_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    key == NEXT_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                    key == PREVIOUS_ROW ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    key == PREVIOUS_ROW_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                    key == PREVIOUS_ROW_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                    key == PREVIOUS_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                    key == PREVIOUS_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                    key == PREVIOUS_COLUMN_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                    key == PREVIOUS_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                    key == PREVIOUS_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    // Paging Actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    key == SCROLL_LEFT_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                    key == SCROLL_LEFT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                    key == SCROLL_RIGHT_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    key == SCROLL_RIGHT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    key == SCROLL_UP_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    key == SCROLL_UP_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    key == SCROLL_DOWN_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                    key == SCROLL_DOWN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                    key == FIRST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                    key == FIRST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                    key == FIRST_ROW ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    key == FIRST_ROW_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    key == LAST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    key == LAST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    key == LAST_ROW ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                    key == LAST_ROW_EXTEND_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                if (table.isEditing() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                        !table.getCellEditor().stopCellEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                // Unfortunately, this strategy introduces bugs because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // of the asynchronous nature of requestFocus() call below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // Introducing a delay with invokeLater() makes this work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                // in the typical case though race conditions then allow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                // focus to disappear altogether. The right solution appears
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                // to be to fix requestFocus() so that it queues a request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                // for the focus regardless of who owns the focus at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                // time the call to requestFocus() is made. The optimisation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                // to ignore the call to requestFocus() when the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                // already has focus may ligitimately be made as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                // request focus event is dequeued, not before.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                // boolean wasEditingWithFocus = table.isEditing() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                // table.getEditorComponent().isFocusOwner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                boolean changeLead = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (key == NEXT_ROW_CHANGE_LEAD || key == PREVIOUS_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    changeLead = (rsm.getSelectionMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                     == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                } else if (key == NEXT_COLUMN_CHANGE_LEAD || key == PREVIOUS_COLUMN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                    changeLead = (csm.getSelectionMode()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                                     == ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                if (changeLead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    moveWithinTableRange(table, dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    if (dy != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                        // casting should be safe since the action is only enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                        // for DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                        ((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(leadRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                        if (getAdjustedLead(table, false, csm) == -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                                && table.getColumnCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                            ((DefaultListSelectionModel)csm).moveLeadSelectionIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                        // casting should be safe since the action is only enabled
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                        // for DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                        ((DefaultListSelectionModel)csm).moveLeadSelectionIndex(leadColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        if (getAdjustedLead(table, true, rsm) == -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                                && table.getRowCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                            ((DefaultListSelectionModel)rsm).moveLeadSelectionIndex(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                    Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    if (cellRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                        table.scrollRectToVisible(cellRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                } else if (!inSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    moveWithinTableRange(table, dx, dy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    table.changeSelection(leadRow, leadColumn, false, extend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    if (table.getRowCount() <= 0 || table.getColumnCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                        // bail - don't try to move selection on an empty table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (moveWithinSelectedRange(table, dx, dy, rsm, csm)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                        // this is the only way we have to set both the lead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        // and the anchor without changing the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                        if (rsm.isSelectedIndex(leadRow)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                            rsm.addSelectionInterval(leadRow, leadRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                            rsm.removeSelectionInterval(leadRow, leadRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                        if (csm.isSelectedIndex(leadColumn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                            csm.addSelectionInterval(leadColumn, leadColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                            csm.removeSelectionInterval(leadColumn, leadColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        Rectangle cellRect = table.getCellRect(leadRow, leadColumn, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        if (cellRect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                            table.scrollRectToVisible(cellRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        table.changeSelection(leadRow, leadColumn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                                false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                if (wasEditingWithFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    table.editCellAt(leadRow, leadColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    final Component editorComp = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    if (editorComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                                editorComp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        });
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
            } else if (key == CANCEL_EDITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                table.removeEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            } else if (key == SELECT_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                table.selectAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            } else if (key == CLEAR_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                table.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            } else if (key == START_EDITING) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                if (!table.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                    CellEditor cellEditor = table.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                    if (cellEditor != null && !cellEditor.stopCellEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                    table.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                table.editCellAt(leadRow, leadColumn, e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                Component editorComp = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                if (editorComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                    editorComp.requestFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            } else if (key == ADD_TO_SELECTION) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (!table.isCellSelected(leadRow, leadColumn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    int oldAnchorRow = rsm.getAnchorSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                    int oldAnchorColumn = csm.getAnchorSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                    rsm.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    csm.setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    table.changeSelection(leadRow, leadColumn, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                    rsm.setAnchorSelectionIndex(oldAnchorRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    csm.setAnchorSelectionIndex(oldAnchorColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                    rsm.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                    csm.setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            } else if (key == TOGGLE_AND_ANCHOR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                table.changeSelection(leadRow, leadColumn, true, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            } else if (key == EXTEND_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                table.changeSelection(leadRow, leadColumn, false, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            } else if (key == MOVE_SELECTION_TO) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                table.changeSelection(leadRow, leadColumn, false, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            } else if (key == FOCUS_HEADER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                JTableHeader th = table.getTableHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                if (th != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    //Set the header's selected column to match the table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    int col = table.getSelectedColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    if (col >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                        TableHeaderUI thUI = th.getUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                        if (thUI instanceof BasicTableHeaderUI) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                            ((BasicTableHeaderUI)thUI).selectColumn(col);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                    //Then give the header the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                    th.requestFocusInWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        public boolean isEnabled(Object sender) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            if (sender instanceof JTable &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                Boolean.TRUE.equals(((JTable)sender).getClientProperty("Table.isFileList"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (key == NEXT_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                        key == NEXT_COLUMN_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                        key == NEXT_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                        key == NEXT_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                        key == PREVIOUS_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                        key == PREVIOUS_COLUMN_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                        key == PREVIOUS_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                        key == PREVIOUS_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                        key == SCROLL_LEFT_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                        key == SCROLL_LEFT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                        key == SCROLL_RIGHT_CHANGE_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                        key == SCROLL_RIGHT_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                        key == FIRST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                        key == FIRST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                        key == LAST_COLUMN ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                        key == LAST_COLUMN_EXTEND_SELECTION ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                        key == NEXT_ROW_CELL ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                        key == PREVIOUS_ROW_CELL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (key == CANCEL_EDITING && sender instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                return ((JTable)sender).isEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            } else if (key == NEXT_ROW_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                       key == PREVIOUS_ROW_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                // discontinuous selection actions are only enabled for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                // DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                return sender != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                       ((JTable)sender).getSelectionModel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                           instanceof DefaultListSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            } else if (key == NEXT_COLUMN_CHANGE_LEAD ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                       key == PREVIOUS_COLUMN_CHANGE_LEAD) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                // discontinuous selection actions are only enabled for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                // DefaultListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                return sender != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                       ((JTable)sender).getColumnModel().getSelectionModel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                           instanceof DefaultListSelectionModel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            } else if (key == ADD_TO_SELECTION && sender instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                // This action is typically bound to SPACE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                // If the table is already in an editing mode, SPACE should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                // simply enter a space character into the table, and not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                // select a cell. Likewise, if the lead cell is already selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                // then hitting SPACE should just enter a space character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                // into the cell and begin editing. In both of these cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                // this action will be disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                JTable table = (JTable)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                int leadRow = getAdjustedLead(table, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                int leadCol = getAdjustedLead(table, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                return !(table.isEditing() || table.isCellSelected(leadRow, leadCol));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            } else if (key == FOCUS_HEADER && sender instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                JTable table = (JTable)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                return table.getTableHeader() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
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
//  The Table's Key listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * 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
   734
     * Instantiate it only within subclasses of {@code BasicTableUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * <p>As of Java 2 platform v1.3 this class is no longer used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Instead <code>JTable</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * overrides <code>processKeyBinding</code> to dispatch the event to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * the current <code>TableCellEditor</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     public class KeyHandler implements KeyListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 17897
diff changeset
   741
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        public void keyPressed(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            getHandler().keyPressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public void keyReleased(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            getHandler().keyReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
            getHandler().keyTyped(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
//  The Table's focus listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * 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
   764
     * Instantiate it only within subclasses of {@code BasicTableUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    public class FocusHandler implements FocusListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 17897
diff changeset
   767
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            getHandler().focusGained(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            getHandler().focusLost(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
//  The Table's mouse and mouse motion listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * This class should be treated as a &quot;protected&quot; inner class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * Instantiate it only within subclasses of BasicTableUI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public class MouseInputHandler implements MouseInputListener {
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 17897
diff changeset
   789
        // NOTE: This class exists only for backward compatibility. All
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        // its functionality has been moved into Handler. If you need to add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        // new functionality add it to the Handler, but make sure this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        // class calls into the Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        public void mouseClicked(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            getHandler().mouseClicked(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            getHandler().mousePressed(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            getHandler().mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        public void mouseEntered(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            getHandler().mouseEntered(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        public void mouseExited(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            getHandler().mouseExited(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        public void mouseMoved(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
            getHandler().mouseMoved(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            getHandler().mouseDragged(e);
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
    private class Handler implements FocusListener, MouseInputListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            PropertyChangeListener, ListSelectionListener, ActionListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            BeforeDrag {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        // FocusListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        private void repaintLeadCell( ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            int lr = getAdjustedLead(table, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            int lc = getAdjustedLead(table, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (lr < 0 || lc < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            Rectangle dirtyRect = table.getCellRect(lr, lc, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            table.repaint(dirtyRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            repaintLeadCell();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            repaintLeadCell();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        // KeyListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        public void keyPressed(KeyEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        public void keyReleased(KeyEvent e) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        public void keyTyped(KeyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            KeyStroke keyStroke = KeyStroke.getKeyStroke(e.getKeyChar(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                    e.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // We register all actions using ANCESTOR_OF_FOCUSED_COMPONENT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // which means that we might perform the appropriate action
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            // in the table and then forward it to the editor if the editor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            // had focus. Make sure this doesn't happen by checking our
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            // InputMaps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            InputMap map = table.getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            if (map != null && map.get(keyStroke) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            map = table.getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                                  WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            if (map != null && map.get(keyStroke) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            keyStroke = KeyStroke.getKeyStrokeForEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
            // The AWT seems to generate an unconsumed \r event when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            // ENTER (\n) is pressed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            if (e.getKeyChar() == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            int leadRow = getAdjustedLead(table, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            int leadColumn = getAdjustedLead(table, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            if (leadRow != -1 && leadColumn != -1 && !table.isEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                if (!table.editCellAt(leadRow, leadColumn)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    return;
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
            // Forwarding events this way seems to put the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            // in a state where it believes it has focus. In reality
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            // the table retains focus - though it is difficult for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            // a user to tell, since the caret is visible and flashing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            // Calling table.requestFocus() here, to get the focus back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            // the table, seems to have no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            Component editorComp = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            if (table.isEditing() && editorComp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
                if (editorComp instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                    JComponent component = (JComponent)editorComp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                    map = component.getInputMap(JComponent.WHEN_FOCUSED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                    Object binding = (map != null) ? map.get(keyStroke) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                    if (binding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                        map = component.getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                        binding = (map != null) ? map.get(keyStroke) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    if (binding != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                        ActionMap am = component.getActionMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                        Action action = (am != null) ? am.get(binding) : null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                        if (action != null && SwingUtilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                            notifyAction(action, keyStroke, e, component,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                                         e.getModifiers())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                            e.consume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        // MouseInputListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        // Component receiving mouse events during editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        // May not be editorComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        private Component dispatchComponent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        public void mouseClicked(MouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
        private void setDispatchComponent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
            Component editorComponent = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
            Point p2 = SwingUtilities.convertPoint(table, p, editorComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            dispatchComponent =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
                    SwingUtilities.getDeepestComponentAt(editorComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
                            p2.x, p2.y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
            SwingUtilities2.setSkipClickCount(dispatchComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
                                              e.getClickCount() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        private boolean repostEvent(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
            // Check for isEditing() in case another event has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
            // caused the editor to be removed. See bug #4306499.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (dispatchComponent == null || !table.isEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            MouseEvent e2 = SwingUtilities.convertMouseEvent(table, e,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                    dispatchComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            dispatchComponent.dispatchEvent(e2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        private void setValueIsAdjusting(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            table.getSelectionModel().setValueIsAdjusting(flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
            table.getColumnModel().getSelectionModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                    setValueIsAdjusting(flag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
        // The row and column where the press occurred and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        // press event itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        private int pressedRow;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        private int pressedCol;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        private MouseEvent pressedEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        // Whether or not the mouse press (which is being considered as part
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
        // of a drag sequence) also caused the selection change to be fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        // processed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
        private boolean dragPressDidSelection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
        // Set to true when a drag gesture has been fully recognized and DnD
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        // begins. Use this to ignore further mouse events which could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
        // delivered if DnD is cancelled (via ESCAPE for example)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        private boolean dragStarted;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
        // Whether or not we should start the editing timer on release
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        private boolean shouldStartTimer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // To cache the return value of pointOutsidePrefSize since we use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        // it multiple times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        private boolean outsidePrefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        // Used to delay the start of editing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        private Timer timer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        private boolean canStartDrag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
            if (pressedRow == -1 || pressedCol == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                return !outsidePrefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
            // if this is a single selection table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            if ((table.getSelectionModel().getSelectionMode() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                     ListSelectionModel.SINGLE_SELECTION) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                (table.getColumnModel().getSelectionModel().getSelectionMode() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                     ListSelectionModel.SINGLE_SELECTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            return table.isCellSelected(pressedRow, pressedCol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        public void mousePressed(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            if (SwingUtilities2.shouldIgnore(e, table)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
            if (table.isEditing() && !table.getCellEditor().stopCellEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
                Component editorComponent = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
                if (editorComponent != null && !editorComponent.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
                    SwingUtilities2.compositeRequestFocus(editorComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            pressedRow = table.rowAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
            pressedCol = table.columnAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            outsidePrefSize = pointOutsidePrefSize(pressedRow, pressedCol, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                shouldStartTimer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                    table.isCellSelected(pressedRow, pressedCol) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                    !e.isShiftDown() &&
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1027
                    !BasicGraphicsUtils.isMenuShortcutKeyDown(e) &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                    !outsidePrefSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
            if (table.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                mousePressedDND(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                SwingUtilities2.adjustFocus(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                if (!isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                adjustSelection(e);
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
        private void mousePressedDND(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
            pressedEvent = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
            boolean grabFocus = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            dragStarted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
            if (canStartDrag() && DragRecognitionSupport.mousePressed(e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                dragPressDidSelection = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1051
                if (BasicGraphicsUtils.isMenuShortcutKeyDown(e) && isFileList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                    // do nothing for control - will be handled on release
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    // or when drag starts
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                } else if (!e.isShiftDown() && table.isCellSelected(pressedRow, pressedCol)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                    // clicking on something that's already selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                    // and need to make it the lead now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                    table.getSelectionModel().addSelectionInterval(pressedRow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                                                                   pressedRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    table.getColumnModel().getSelectionModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                        addSelectionInterval(pressedCol, pressedCol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
                dragPressDidSelection = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
                // could be a drag initiating event - don't grab focus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                grabFocus = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            } else if (!isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                // When drag can't happen, mouse drags might change the selection in the table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                // so we want the isAdjusting flag to be set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                setValueIsAdjusting(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
            if (grabFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                SwingUtilities2.adjustFocus(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
            adjustSelection(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
        private void adjustSelection(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
            // Fix for 4835633
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
            if (outsidePrefSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                // If shift is down in multi-select, we should just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                // For single select or non-shift-click, clear the selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                if (e.getID() ==  MouseEvent.MOUSE_PRESSED &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    (!e.isShiftDown() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                     table.getSelectionModel().getSelectionMode() ==
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                     ListSelectionModel.SINGLE_SELECTION)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                    table.clearSelection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                    TableCellEditor tce = table.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                    if (tce != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        tce.stopCellEditing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
            // The autoscroller can generate drag events outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            // table's range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            if ((pressedCol == -1) || (pressedRow == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
            boolean dragEnabled = table.getDragEnabled();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            if (!dragEnabled && !isFileList && table.editCellAt(pressedRow, pressedCol, e)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                setDispatchComponent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                repostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            CellEditor editor = table.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            if (dragEnabled || editor == null || editor.shouldSelectCell(e)) {
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1115
                table.changeSelection(pressedRow, pressedCol,
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1116
                        BasicGraphicsUtils.isMenuShortcutKeyDown(e),
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1117
                        e.isShiftDown());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        public void valueChanged(ListSelectionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            if (timer != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                timer.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
                timer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        public void actionPerformed(ActionEvent ae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
            table.editCellAt(pressedRow, pressedCol, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
            Component editorComponent = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            if (editorComponent != null && !editorComponent.hasFocus()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                SwingUtilities2.compositeRequestFocus(editorComponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
        private void maybeStartTimer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
            if (!shouldStartTimer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
            if (timer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                timer = new Timer(1200, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                timer.setRepeats(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
            timer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        public void mouseReleased(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
            if (SwingUtilities2.shouldIgnore(e, table)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            if (table.getDragEnabled()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                mouseReleasedDND(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
                    maybeStartTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
            pressedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            repostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            dispatchComponent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            setValueIsAdjusting(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        private void mouseReleasedDND(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            MouseEvent me = DragRecognitionSupport.mouseReleased(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            if (me != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
                SwingUtilities2.adjustFocus(table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
                if (!dragPressDidSelection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
                    adjustSelection(me);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            if (!dragStarted) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
                if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                    maybeStartTimer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
                if (pressedEvent != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                        table.rowAtPoint(p) == pressedRow &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                        table.columnAtPoint(p) == pressedCol &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
                        table.editCellAt(pressedRow, pressedCol, pressedEvent)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                    setDispatchComponent(pressedEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    repostEvent(pressedEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                    // This may appear completely odd, but must be done for backward
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    // compatibility reasons. Developers have been known to rely on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    // a call to shouldSelectCell after editing has begun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                    CellEditor ce = table.getCellEditor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    if (ce != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                        ce.shouldSelectCell(pressedEvent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        public void mouseEntered(MouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        public void mouseExited(MouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
        public void mouseMoved(MouseEvent e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        public void dragStarting(MouseEvent me) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
            dragStarted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1214
            if (BasicGraphicsUtils.isMenuShortcutKeyDown(me) && isFileList) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                table.getSelectionModel().addSelectionInterval(pressedRow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                                                               pressedRow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                table.getColumnModel().getSelectionModel().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
                    addSelectionInterval(pressedCol, pressedCol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            pressedEvent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        public void mouseDragged(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
            if (SwingUtilities2.shouldIgnore(e, table)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (table.getDragEnabled() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                    (DragRecognitionSupport.mouseDragged(e, this) || dragStarted)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            repostEvent(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
            // Check isFileList:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            // Until we support drag-selection, dragging should not change
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            // the selection (act like single-select).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
            if (isFileList || table.isEditing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            int row = table.rowAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            int column = table.columnAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            // The autoscroller can generate drag events outside the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
            // table's range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            if ((column == -1) || (row == -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1253
            table.changeSelection(row, column,
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 2658
diff changeset
  1254
                    BasicGraphicsUtils.isMenuShortcutKeyDown(e), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        // PropertyChangeListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        public void propertyChange(PropertyChangeEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            String changeName = event.getPropertyName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            if ("componentOrientation" == changeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
                InputMap inputMap = getInputMap(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
                SwingUtilities.replaceUIInputMap(table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
                    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
                    inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                JTableHeader header = table.getTableHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                if (header != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
                    header.setComponentOrientation(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                            (ComponentOrientation)event.getNewValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            } else if ("dropLocation" == changeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                JTable.DropLocation oldValue = (JTable.DropLocation)event.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
                repaintDropLocation(oldValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                repaintDropLocation(table.getDropLocation());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            } else if ("Table.isFileList" == changeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                table.revalidate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
                table.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
                    table.getSelectionModel().addListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                    table.getSelectionModel().removeListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                    timer = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
            } else if ("selectionModel" == changeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
                if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                    ListSelectionModel old = (ListSelectionModel)event.getOldValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    old.removeListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                    table.getSelectionModel().addListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        private void repaintDropLocation(JTable.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
            if (!loc.isInsertRow() && !loc.isInsertColumn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                Rectangle rect = table.getCellRect(loc.getRow(), loc.getColumn(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                    table.repaint(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            if (loc.isInsertRow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                Rectangle rect = extendRect(getHDropLineRect(loc), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                    table.repaint(rect);
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
            if (loc.isInsertColumn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                Rectangle rect = extendRect(getVDropLineRect(loc), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
                if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
                    table.repaint(rect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     * Returns true if the given point is outside the preferredSize of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     * item at the given row of the table.  (Column must be 0).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * Returns false if the "Table.isFileList" client property is not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    private boolean pointOutsidePrefSize(int row, int column, Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
        if (!isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        return SwingUtilities2.pointOutsidePrefSize(table, row, column, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
//  Factory methods for the Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    private Handler getHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        if (handler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
            handler = new Handler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        return handler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * Creates the key listener for handling keyboard navigation in the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
    protected KeyListener createKeyListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
     * Creates the focus listener for handling keyboard navigation in the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    protected FocusListener createFocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * Creates the mouse listener for the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    protected MouseInputListener createMouseInputListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        return getHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
//  The installation/uninstall procedures and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    public static ComponentUI createUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        return new BasicTableUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
//  Installation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        table = (JTable)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        rendererPane = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
        table.add(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        installDefaults2();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * Initialize JTable properties, e.g. font, foreground, and background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * The font, foreground, and background properties are only set if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * current value is either null or a UIResource, other properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * if the current value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        LookAndFeel.installColorsAndFont(table, "Table.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
                                         "Table.foreground", "Table.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
        // JTable's original row height is 16.  To correctly display the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        // contents on Linux we should have set it to 18, Windows 19 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        // Solaris 20.  As these values vary so much it's too hard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        // be backward compatable and try to update the row height, we're
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        // therefor NOT going to adjust the row height based on font.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        // developer changes the font, it's there responsability to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
        // the row height.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        LookAndFeel.installProperty(table, "opaque", Boolean.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        Color sbg = table.getSelectionBackground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if (sbg == null || sbg instanceof UIResource) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1417
            sbg = UIManager.getColor("Table.selectionBackground");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1418
            table.setSelectionBackground(sbg != null ? sbg : UIManager.getColor("textHighlight"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        Color sfg = table.getSelectionForeground();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        if (sfg == null || sfg instanceof UIResource) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1423
            sfg = UIManager.getColor("Table.selectionForeground");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1424
            table.setSelectionForeground(sfg != null ? sfg : UIManager.getColor("textHighlightText"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        Color gridColor = table.getGridColor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
        if (gridColor == null || gridColor instanceof UIResource) {
2658
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1429
            gridColor = UIManager.getColor("Table.gridColor");
43e06bc950ec 6591875: Nimbus Swing Look and Feel
peterz
parents: 2
diff changeset
  1430
            table.setGridColor(gridColor != null ? gridColor : Color.GRAY);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        // install the scrollpane border
17897
fb367e67916d 7068740: If you wrap a JTable in a JLayer you can't use the page up and page down cmds
vkarnauk
parents: 9230
diff changeset
  1434
        Container parent = SwingUtilities.getUnwrappedParent(table);  // should be viewport
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
        if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
            parent = parent.getParent();  // should be the scrollpane
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            if (parent != null && parent instanceof JScrollPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
                LookAndFeel.installBorder((JScrollPane)parent, "Table.scrollPaneBorder");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        isFileList = Boolean.TRUE.equals(table.getClientProperty("Table.isFileList"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
    private void installDefaults2() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
        TransferHandler th = table.getTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
        if (th == null || th instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            table.setTransferHandler(defaultTransferHandler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
            // default TransferHandler doesn't support drop
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
            // so we don't want drop handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
            if (table.getDropTarget() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
                table.setDropTarget(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     * Attaches listeners to the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
        focusListener = createFocusListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        keyListener = createKeyListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        mouseInputListener = createMouseInputListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
        table.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        table.addKeyListener(keyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        table.addMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
        table.addMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        table.addPropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
        if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            table.getSelectionModel().addListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * Register all keyboard actions on the JTable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
        LazyActionMap.installLazyActionMap(table, BasicTableUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                "Table.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
        InputMap inputMap = getInputMap(JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                                        WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
        SwingUtilities.replaceUIInputMap(table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                                inputMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    InputMap getInputMap(int condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            InputMap keyMap =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
                (InputMap)DefaultLookup.get(table, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                                            "Table.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            InputMap rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
            if (table.getComponentOrientation().isLeftToRight() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                ((rtlKeyMap = (InputMap)DefaultLookup.get(table, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                                            "Table.ancestorInputMap.RightToLeft")) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                return keyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                rtlKeyMap.setParent(keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
                return rtlKeyMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        // IMPORTANT: There is a very close coupling between the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
        // passed to the Actions constructor. Only certain parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        // combinations are supported. For example, the following Action would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        // not work as expected:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        //     new Actions(Actions.NEXT_ROW_CELL, 1, 4, false, true)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        // Actions which move within the selection only (having a true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        // inSelection parameter) require that one of dx or dy be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
        // zero and the other be -1 or 1. The point of this warning is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
        // that you should be very careful about making sure a particular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
        // combination of parameters is supported before changing or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
        // adding anything here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
        map.put(new Actions(Actions.NEXT_COLUMN, 1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        map.put(new Actions(Actions.NEXT_COLUMN_CHANGE_LEAD, 1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
        map.put(new Actions(Actions.PREVIOUS_COLUMN, -1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        map.put(new Actions(Actions.PREVIOUS_COLUMN_CHANGE_LEAD, -1, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        map.put(new Actions(Actions.NEXT_ROW, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
        map.put(new Actions(Actions.NEXT_ROW_CHANGE_LEAD, 0, 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        map.put(new Actions(Actions.PREVIOUS_ROW, 0, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        map.put(new Actions(Actions.PREVIOUS_ROW_CHANGE_LEAD, 0, -1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
        map.put(new Actions(Actions.NEXT_COLUMN_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                1, 0, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        map.put(new Actions(Actions.PREVIOUS_COLUMN_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                -1, 0, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
        map.put(new Actions(Actions.NEXT_ROW_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                0, 1, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
        map.put(new Actions(Actions.PREVIOUS_ROW_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                0, -1, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        map.put(new Actions(Actions.SCROLL_UP_CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                false, false, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        map.put(new Actions(Actions.SCROLL_DOWN_CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                false, true, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
        map.put(new Actions(Actions.FIRST_COLUMN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                false, false, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
        map.put(new Actions(Actions.LAST_COLUMN,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                false, true, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        map.put(new Actions(Actions.SCROLL_UP_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                true, false, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
        map.put(new Actions(Actions.SCROLL_DOWN_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                true, true, true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
        map.put(new Actions(Actions.FIRST_COLUMN_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
                true, false, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
        map.put(new Actions(Actions.LAST_COLUMN_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                true, true, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
        map.put(new Actions(Actions.FIRST_ROW, false, false, true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        map.put(new Actions(Actions.LAST_ROW, false, true, true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        map.put(new Actions(Actions.FIRST_ROW_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                true, false, true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
        map.put(new Actions(Actions.LAST_ROW_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                true, true, true, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
        map.put(new Actions(Actions.NEXT_COLUMN_CELL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                1, 0, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
        map.put(new Actions(Actions.PREVIOUS_COLUMN_CELL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
                -1, 0, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        map.put(new Actions(Actions.NEXT_ROW_CELL, 0, 1, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        map.put(new Actions(Actions.PREVIOUS_ROW_CELL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
                0, -1, false, true));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
        map.put(new Actions(Actions.SELECT_ALL));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
        map.put(new Actions(Actions.CLEAR_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
        map.put(new Actions(Actions.CANCEL_EDITING));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
        map.put(new Actions(Actions.START_EDITING));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
        map.put(TransferHandler.getCutAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
                TransferHandler.getCutAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
        map.put(TransferHandler.getCopyAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
                TransferHandler.getCopyAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        map.put(TransferHandler.getPasteAction().getValue(Action.NAME),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
                TransferHandler.getPasteAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        map.put(new Actions(Actions.SCROLL_LEFT_CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                false, false, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
        map.put(new Actions(Actions.SCROLL_RIGHT_CHANGE_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                false, true, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
        map.put(new Actions(Actions.SCROLL_LEFT_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
                true, false, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        map.put(new Actions(Actions.SCROLL_RIGHT_EXTEND_SELECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
                true, true, false, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
        map.put(new Actions(Actions.ADD_TO_SELECTION));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
        map.put(new Actions(Actions.TOGGLE_AND_ANCHOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        map.put(new Actions(Actions.EXTEND_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
        map.put(new Actions(Actions.MOVE_SELECTION_TO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
        map.put(new Actions(Actions.FOCUS_HEADER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
//  Uninstallation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        table.remove(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        rendererPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        table = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    protected void uninstallDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        if (table.getTransferHandler() instanceof UIResource) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
            table.setTransferHandler(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        table.removeFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        table.removeKeyListener(keyListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        table.removeMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        table.removeMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        table.removePropertyChangeListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        if (isFileList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            table.getSelectionModel().removeListSelectionListener(getHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        focusListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        keyListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        mouseInputListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        handler = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        SwingUtilities.replaceUIInputMap(table, JComponent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
                                   WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        SwingUtilities.replaceUIActionMap(table, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
        UIDefaults lafDefaults = UIManager.getLookAndFeelDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
        Component renderer = (Component)lafDefaults.get(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                BASELINE_COMPONENT_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
        if (renderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
            DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
            renderer = tcr.getTableCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                    table, "a", false, false, -1, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
            lafDefaults.put(BASELINE_COMPONENT_KEY, renderer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
        renderer.setFont(table.getFont());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
        int rowMargin = table.getRowMargin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        return renderer.getBaseline(Integer.MAX_VALUE, table.getRowHeight() -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
                                    rowMargin) + rowMargin / 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
     * Returns an enum indicating how the baseline of the component
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
     * changes as the size changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    public Component.BaselineResizeBehavior getBaselineResizeBehavior(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
            JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
        super.getBaselineResizeBehavior(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        return Component.BaselineResizeBehavior.CONSTANT_ASCENT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
// Size Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
    private Dimension createTableSize(long width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        int rowCount = table.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        if (rowCount > 0 && table.getColumnCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
            Rectangle r = table.getCellRect(rowCount-1, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
            height = r.y + r.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        // Width is always positive. The call to abs() is a workaround for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        // a bug in the 1.1.6 JIT on Windows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        long tmp = Math.abs(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
        if (tmp > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
            tmp = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
        return new Dimension((int)tmp, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * Return the minimum size of the table. The minimum height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * row height times the number of rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * The minimum width is the sum of the minimum widths of each column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
        long width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        Enumeration enumeration = table.getColumnModel().getColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        while (enumeration.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            TableColumn aColumn = (TableColumn)enumeration.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
            width = width + aColumn.getMinWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
        return createTableSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
     * Return the preferred size of the table. The preferred height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
     * row height times the number of rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
     * The preferred width is the sum of the preferred widths of each column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        long width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
        Enumeration enumeration = table.getColumnModel().getColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
        while (enumeration.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
            TableColumn aColumn = (TableColumn)enumeration.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            width = width + aColumn.getPreferredWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        return createTableSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     * Return the maximum size of the table. The maximum height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
     * row heighttimes the number of rows.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
     * The maximum width is the sum of the maximum widths of each column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        long width = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        Enumeration enumeration = table.getColumnModel().getColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        while (enumeration.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
            TableColumn aColumn = (TableColumn)enumeration.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
            width = width + aColumn.getMaxWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
        return createTableSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
//  Paint methods and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
    /** Paint a representation of the <code>table</code> instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * that was set in installUI().
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        Rectangle bounds = table.getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        // account for the fact that the graphics has already been translated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        // into the table's bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
        bounds.x = bounds.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
        if (table.getRowCount() <= 0 || table.getColumnCount() <= 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                // this check prevents us from painting the entire table
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
                // when the clip doesn't intersect our bounds at all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
                !bounds.intersects(clip)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
            paintDropLines(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
        Point upperLeft = clip.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        Point lowerRight = new Point(clip.x + clip.width - 1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                                     clip.y + clip.height - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        int rMin = table.rowAtPoint(upperLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
        int rMax = table.rowAtPoint(lowerRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
        // This should never happen (as long as our bounds intersect the clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
        // which is why we bail above if that is the case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        if (rMin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            rMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
        // If the table does not have enough rows to fill the view we'll get -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
        // (We could also get -1 if our bounds don't intersect the clip,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        // which is why we bail above if that is the case).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        // Replace this with the index of the last row.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        if (rMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            rMax = table.getRowCount()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        int cMin = table.columnAtPoint(ltr ? upperLeft : lowerRight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        int cMax = table.columnAtPoint(ltr ? lowerRight : upperLeft);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        if (cMin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
            cMin = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        // If the table does not have enough columns to fill the view we'll get -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
        // Replace this with the index of the last column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        if (cMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
            cMax = table.getColumnCount()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        // Paint the grid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        paintGrid(g, rMin, rMax, cMin, cMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
        // Paint the cells.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        paintCells(g, rMin, rMax, cMin, cMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        paintDropLines(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    private void paintDropLines(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
        JTable.DropLocation loc = table.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
        if (loc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        Color color = UIManager.getColor("Table.dropLineColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
        Color shortColor = UIManager.getColor("Table.dropLineShortColor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
        if (color == null && shortColor == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
        Rectangle rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
        rect = getHDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
        if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
            int x = rect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
            int w = rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
            if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                extendRect(rect, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
                g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            if (!loc.isInsertColumn() && shortColor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
                g.setColor(shortColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
                g.fillRect(x, rect.y, w, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
        rect = getVDropLineRect(loc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        if (rect != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
            int y = rect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
            int h = rect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
            if (color != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
                extendRect(rect, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
                g.setColor(color);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
                g.fillRect(rect.x, rect.y, rect.width, rect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
            if (!loc.isInsertRow() && shortColor != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
                g.setColor(shortColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
                g.fillRect(rect.x, y, rect.width, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
    private Rectangle getHDropLineRect(JTable.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        if (!loc.isInsertRow()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
        int row = loc.getRow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
        int col = loc.getColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        if (col >= table.getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
            col--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        Rectangle rect = table.getCellRect(row, col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
        if (row >= table.getRowCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
            row--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
            Rectangle prevRect = table.getCellRect(row, col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
            rect.y = prevRect.y + prevRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        if (rect.y == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
            rect.y = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
            rect.y -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
        rect.height = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    private Rectangle getVDropLineRect(JTable.DropLocation loc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        if (!loc.isInsertColumn()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
        boolean ltr = table.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        int col = loc.getColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        Rectangle rect = table.getCellRect(loc.getRow(), col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        if (col >= table.getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
            col--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
            rect = table.getCellRect(loc.getRow(), col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
            if (ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
                rect.x = rect.x + rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
        } else if (!ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
            rect.x = rect.x + rect.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        if (rect.x == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
            rect.x = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
            rect.x -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        rect.width = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    private Rectangle extendRect(Rectangle rect, boolean horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        if (rect == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
            return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        if (horizontal) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
            rect.x = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            rect.width = table.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            rect.y = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            if (table.getRowCount() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                Rectangle lastRect = table.getCellRect(table.getRowCount() - 1, 0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                rect.height = lastRect.y + lastRect.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                rect.height = table.getHeight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
        return rect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
     * Paints the grid lines within <I>aRect</I>, using the grid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
     * color set with <I>setGridColor</I>. Paints vertical lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
     * if <code>getShowVerticalLines()</code> returns true and paints
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
     * horizontal lines if <code>getShowHorizontalLines()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
     * returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    private void paintGrid(Graphics g, int rMin, int rMax, int cMin, int cMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
        Rectangle minCell = table.getCellRect(rMin, cMin, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
        Rectangle maxCell = table.getCellRect(rMax, cMax, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        Rectangle damagedArea = minCell.union( maxCell );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        if (table.getShowHorizontalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
            int tableWidth = damagedArea.x + damagedArea.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
            int y = damagedArea.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
            for (int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
                y += table.getRowHeight(row);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
                g.drawLine(damagedArea.x, y - 1, tableWidth - 1, y - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        if (table.getShowVerticalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
            TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
            int tableHeight = damagedArea.y + damagedArea.height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
            int x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
            if (table.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
                x = damagedArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
                for (int column = cMin; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
                    int w = cm.getColumn(column).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
                    x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
                    g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
                x = damagedArea.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
                for (int column = cMax; column >= cMin; column--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
                    int w = cm.getColumn(column).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
                    x += w;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
                    g.drawLine(x - 1, 0, x - 1, tableHeight - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    private int viewIndexForColumn(TableColumn aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
        TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        for (int column = 0; column < cm.getColumnCount(); column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
            if (cm.getColumn(column) == aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
                return column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
    private void paintCells(Graphics g, int rMin, int rMax, int cMin, int cMax) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        JTableHeader header = table.getTableHeader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
        TableColumn draggedColumn = (header == null) ? null : header.getDraggedColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
        TableColumnModel cm = table.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        int columnMargin = cm.getColumnMargin();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
        Rectangle cellRect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        TableColumn aColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        int columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        if (table.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
            for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                cellRect = table.getCellRect(row, cMin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                for(int column = cMin; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
                    aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
                    if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
                        paintCell(g, cellRect, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
                    cellRect.x += columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
                cellRect = table.getCellRect(row, cMin, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
                aColumn = cm.getColumn(cMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
                if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
                    paintCell(g, cellRect, row, cMin);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                for(int column = cMin+1; column <= cMax; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
                    aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
                    columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
                    cellRect.width = columnWidth - columnMargin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                    cellRect.x -= columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                    if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                        paintCell(g, cellRect, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
        // Paint the dragged column if we are dragging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
        if (draggedColumn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            paintDraggedArea(g, rMin, rMax, draggedColumn, header.getDraggedDistance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
        // Remove any renderers that may be left in the rendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
        rendererPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
    private void paintDraggedArea(Graphics g, int rMin, int rMax, TableColumn draggedColumn, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        int draggedColumnIndex = viewIndexForColumn(draggedColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        Rectangle minCell = table.getCellRect(rMin, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        Rectangle maxCell = table.getCellRect(rMax, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
        Rectangle vacatedColumnRect = minCell.union(maxCell);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
        // Paint a gray well in place of the moving column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
        g.setColor(table.getParent().getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                   vacatedColumnRect.width, vacatedColumnRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        // Move to the where the cell has been dragged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
        vacatedColumnRect.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
        // Fill the background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
        g.setColor(table.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
        g.fillRect(vacatedColumnRect.x, vacatedColumnRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                   vacatedColumnRect.width, vacatedColumnRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        // Paint the vertical grid lines if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        if (table.getShowVerticalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            int x1 = vacatedColumnRect.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            int y1 = vacatedColumnRect.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            int x2 = x1 + vacatedColumnRect.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            int y2 = y1 + vacatedColumnRect.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
            // Left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            g.drawLine(x1-1, y1, x1-1, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
            // Right
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
            g.drawLine(x2, y1, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        for(int row = rMin; row <= rMax; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
            // Render the cell value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            Rectangle r = table.getCellRect(row, draggedColumnIndex, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
            r.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
            paintCell(g, r, row, draggedColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
            // Paint the (lower) horizontal grid line if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
            if (table.getShowHorizontalLines()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
                g.setColor(table.getGridColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
                Rectangle rcr = table.getCellRect(row, draggedColumnIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
                rcr.x += distance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
                int x1 = rcr.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
                int y1 = rcr.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
                int x2 = x1 + rcr.width - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
                int y2 = y1 + rcr.height - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
                g.drawLine(x1, y2, x2, y2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
    private void paintCell(Graphics g, Rectangle cellRect, int row, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        if (table.isEditing() && table.getEditingRow()==row &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
                                 table.getEditingColumn()==column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
            Component component = table.getEditorComponent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
            component.setBounds(cellRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
            component.validate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
            TableCellRenderer renderer = table.getCellRenderer(row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
            Component component = table.prepareRenderer(renderer, row, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            rendererPane.paintComponent(g, component, table, cellRect.x, cellRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
                                        cellRect.width, cellRect.height, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    private static int getAdjustedLead(JTable table,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
                                       boolean row,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
                                       ListSelectionModel model) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        int index = model.getLeadSelectionIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
        int compare = row ? table.getRowCount() : table.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        return index < compare ? index : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
    private static int getAdjustedLead(JTable table, boolean row) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
        return row ? getAdjustedLead(table, row, table.getSelectionModel())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                   : getAdjustedLead(table, row, table.getColumnModel().getSelectionModel());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
    private static final TransferHandler defaultTransferHandler = new TableTransferHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    static class TableTransferHandler extends TransferHandler implements UIResource {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
         * Create a Transferable to use as the source for a data transfer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
         * @param c  The component holding the data to be transfered.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
         *  argument is provided to enable sharing of TransferHandlers by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
         *  multiple components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
         * @return  The representation of the data to be transfered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
        protected Transferable createTransferable(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
            if (c instanceof JTable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                JTable table = (JTable) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                int[] rows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                int[] cols;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                if (!table.getRowSelectionAllowed() && !table.getColumnSelectionAllowed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                if (!table.getRowSelectionAllowed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                    int rowCount = table.getRowCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                    rows = new int[rowCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                    for (int counter = 0; counter < rowCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                        rows[counter] = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    rows = table.getSelectedRows();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
                if (!table.getColumnSelectionAllowed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                    int colCount = table.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
                    cols = new int[colCount];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
                    for (int counter = 0; counter < colCount; counter++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
                        cols[counter] = counter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
                    cols = table.getSelectedColumns();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
                if (rows == null || cols == null || rows.length == 0 || cols.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
                StringBuffer plainBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
                StringBuffer htmlBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
                htmlBuf.append("<html>\n<body>\n<table>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
                for (int row = 0; row < rows.length; row++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
                    htmlBuf.append("<tr>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    for (int col = 0; col < cols.length; col++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                        Object obj = table.getValueAt(rows[row], cols[col]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                        String val = ((obj == null) ? "" : obj.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        plainBuf.append(val + "\t");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                        htmlBuf.append("  <td>" + val + "</td>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                    // we want a newline at the end of each line and not a tab
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                    plainBuf.deleteCharAt(plainBuf.length() - 1).append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    htmlBuf.append("</tr>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                // remove the last newline
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                plainBuf.deleteCharAt(plainBuf.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                htmlBuf.append("</table>\n</body>\n</html>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                return new BasicTransferable(plainBuf.toString(), htmlBuf.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        public int getSourceActions(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
            return COPY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
}  // End of Class BasicTableUI