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