src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 35650 jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java@15f1f0ff7790
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32669
8ff453aa551b 8134947: [macosx] Various memory leaks in Aqua look and feel
serb
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4394
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.swing.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.plaf.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.swing.table.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * BasicTableHeaderUI implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Alan Chung
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author Philip Milne
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public class BasicTableHeaderUI extends TableHeaderUI {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static Cursor resizeCursor = Cursor.getPredefinedCursor(Cursor.E_RESIZE_CURSOR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// Instance Variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    52
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    53
     *  The {@code JTableHeader} that is delegating the painting to this UI.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    54
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected JTableHeader header;
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    56
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    57
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    58
     * The instance of {@code CellRendererPane}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    59
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected CellRendererPane rendererPane;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    62
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    63
     * Listeners that are attached to the {@code JTable}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
    64
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected MouseInputListener mouseInputListener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // The column header over which the mouse currently is.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int rolloverColumn = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // The column that should be highlighted when the table header has the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private int selectedColumnIndex = 0; // Read ONLY via getSelectedColumnIndex!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static FocusListener focusListener = new FocusListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public void focusGained(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            repaintHeader(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public void focusLost(FocusEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            repaintHeader(e.getSource());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        private void repaintHeader(Object source) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (source instanceof JTableHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                JTableHeader th = (JTableHeader)source;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                BasicTableHeaderUI ui =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                   (BasicTableHeaderUI)BasicLookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                        getUIOfType(th.getUI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                            BasicTableHeaderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                th.repaint(th.getHeaderRect(ui.getSelectedColumnIndex()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * This class should be treated as a "protected" inner class.
9230
c5674561cb9e 6985329: 9 classes in swing.plaf contains words inappropriate for public spec - about some compiler bug
alexp
parents: 7668
diff changeset
   100
     * Instantiate it only within subclasses of {@code BasicTableHeaderUI}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public class MouseInputHandler implements MouseInputListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        private int mouseXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        private Cursor otherCursor = resizeCursor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public void mouseClicked(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   108
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   109
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   110
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (e.getClickCount() % 2 == 1 &&
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   112
                    SwingUtilities.isLeftMouseButton(e)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                JTable table = header.getTable();
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   114
                RowSorter<?> sorter;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                if (table != null && (sorter = table.getRowSorter()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                    int columnIndex = header.columnAtPoint(e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                    if (columnIndex != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                        columnIndex = table.convertColumnIndexToModel(
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   119
                                columnIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                        sorter.toggleSortOrder(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        private TableColumn getResizingColumn(Point p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return getResizingColumn(p, header.columnAtPoint(p));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        private TableColumn getResizingColumn(Point p, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (column == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                 return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            Rectangle r = header.getHeaderRect(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            r.grow(-3, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (r.contains(p)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            int midPoint = r.x + r.width/2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            int columnIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if( header.getComponentOrientation().isLeftToRight() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                columnIndex = (p.x < midPoint) ? column - 1 : column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                columnIndex = (p.x < midPoint) ? column : column - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            if (columnIndex == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return header.getColumnModel().getColumn(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public void mousePressed(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   153
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   154
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   155
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            header.setDraggedColumn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            header.setResizingColumn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            header.setDraggedDistance(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            Point p = e.getPoint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            // First find which header cell was hit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            TableColumnModel columnModel = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int index = header.columnAtPoint(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (index != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // The last 3 pixels + 3 pixels of next column are for resizing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                TableColumn resizingColumn = getResizingColumn(p, index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                if (canResize(resizingColumn, header)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    header.setResizingColumn(resizingColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    if( header.getComponentOrientation().isLeftToRight() ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        mouseXOffset = p.x - resizingColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        mouseXOffset = p.x + resizingColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                else if (header.getReorderingAllowed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    TableColumn hitColumn = columnModel.getColumn(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    header.setDraggedColumn(hitColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    mouseXOffset = p.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (header.getReorderingAllowed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                int oldRolloverColumn = rolloverColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                rolloverColumn = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                rolloverColumnUpdated(oldRolloverColumn, rolloverColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        private void swapCursor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            Cursor tmp = header.getCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            header.setCursor(otherCursor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            otherCursor = tmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        public void mouseMoved(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   198
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   199
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   200
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            if (canResize(getResizingColumn(e.getPoint()), header) !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                (header.getCursor() == resizeCursor)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                swapCursor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            updateRolloverColumn(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        public void mouseDragged(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   209
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   210
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   211
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int mouseX = e.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            TableColumn resizingColumn  = header.getResizingColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            TableColumn draggedColumn  = header.getDraggedColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            boolean headerLeftToRight = header.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (resizingColumn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                int oldWidth = resizingColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                int newWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (headerLeftToRight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    newWidth = mouseX - mouseXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                } else  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    newWidth = mouseXOffset - mouseX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                mouseXOffset += changeColumnWidth(resizingColumn, header,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                                                  oldWidth, newWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            else if (draggedColumn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                TableColumnModel cm = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                int draggedDistance = mouseX - mouseXOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                int direction = (draggedDistance < 0) ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                int columnIndex = viewIndexForColumn(draggedColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                int newColumnIndex = columnIndex + (headerLeftToRight ? direction : -direction);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                if (0 <= newColumnIndex && newColumnIndex < cm.getColumnCount()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                    int width = cm.getColumn(newColumnIndex).getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                    if (Math.abs(draggedDistance) > (width / 2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                        mouseXOffset = mouseXOffset + direction * width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                        header.setDraggedDistance(draggedDistance - direction * width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                        //Cache the selected column.
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   244
                        int selectedIndex =
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   245
                                SwingUtilities2.convertColumnIndexToModel(
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   246
                                        header.getColumnModel(),
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   247
                                        getSelectedColumnIndex());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        //Now do the move.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                        cm.moveColumn(columnIndex, newColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        //Update the selected index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                        selectColumn(
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   254
                            SwingUtilities2.convertColumnIndexToView(
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   255
                                    header.getColumnModel(), selectedIndex),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                            false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                setDraggedDistance(draggedDistance, columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            updateRolloverColumn(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        public void mouseReleased(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   268
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   269
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   270
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            setDraggedDistance(0, viewIndexForColumn(header.getDraggedColumn()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            header.setResizingColumn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            header.setDraggedColumn(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            updateRolloverColumn(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        public void mouseEntered(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   280
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   281
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   282
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            updateRolloverColumn(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public void mouseExited(MouseEvent e) {
5595
27cbc0c6ba34 6889007: No resize cursor during hovering mouse over JTable
alexp
parents: 5590
diff changeset
   287
            if (!header.isEnabled()) {
5590
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   288
                return;
3ec077d7e893 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
alexp
parents: 4394
diff changeset
   289
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            int oldRolloverColumn = rolloverColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            rolloverColumn = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            rolloverColumnUpdated(oldRolloverColumn, rolloverColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
// Protected & Private Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        private void setDraggedDistance(int draggedDistance, int column) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            header.setDraggedDistance(draggedDistance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            if (column != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                header.getColumnModel().moveColumn(column, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
//  Factory methods for the Listeners
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   311
     * Creates the mouse listener for the {@code JTableHeader}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   312
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   313
     * @return the mouse listener for the {@code JTableHeader}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    protected MouseInputListener createMouseInputListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return new MouseInputHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
//  The installation/uninstall procedures and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   323
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   324
     * Returns a new instance of {@code BasicTableHeaderUI}.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   325
     *
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   326
     * @param h a component.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   327
     * @return a new instance of {@code BasicTableHeaderUI}
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   328
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    public static ComponentUI createUI(JComponent h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        return new BasicTableHeaderUI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
//  Installation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    public void installUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        header = (JTableHeader)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        rendererPane = new CellRendererPane();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        header.add(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        installDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        installListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        installKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   347
     * Initializes JTableHeader properties such as font, foreground, and background.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * The font, foreground, and background properties are only set if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * current value is either null or a UIResource, other properties are set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * if the current value is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * @see #installUI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    protected void installDefaults() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        LookAndFeel.installColorsAndFont(header, "TableHeader.background",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                                         "TableHeader.foreground", "TableHeader.font");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        LookAndFeel.installProperty(header, "opaque", Boolean.TRUE);
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
     * Attaches listeners to the JTableHeader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    protected void installListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        mouseInputListener = createMouseInputListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        header.addMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        header.addMouseMotionListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        header.addFocusListener(focusListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Register all keyboard actions on the JTableHeader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    protected void installKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        InputMap keyMap = (InputMap)DefaultLookup.get(header, this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                "TableHeader.ancestorInputMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        SwingUtilities.replaceUIInputMap(header,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, keyMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        LazyActionMap.installLazyActionMap(header, BasicTableHeaderUI.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                "TableHeader.actionMap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
// Uninstall methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    public void uninstallUI(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        uninstallDefaults();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        uninstallListeners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        uninstallKeyboardActions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        header.remove(rendererPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        rendererPane = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        header = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   395
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   396
     * Uninstalls default properties
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   397
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    protected void uninstallDefaults() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
25761
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   400
    /**
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   401
     * Unregisters listeners.
c408b10ef757 8043968: Fix doclint warnings from javax.swing.plaf.basic package, 1 of 7
aeremeev
parents: 25565
diff changeset
   402
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    protected void uninstallListeners() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        header.removeMouseListener(mouseInputListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        header.removeMouseMotionListener(mouseInputListener);
32669
8ff453aa551b 8134947: [macosx] Various memory leaks in Aqua look and feel
serb
parents: 25859
diff changeset
   406
        header.removeFocusListener(focusListener);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        mouseInputListener = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * Unregisters default key actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected void uninstallKeyboardActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        SwingUtilities.replaceUIInputMap(header, JComponent.WHEN_FOCUSED, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        SwingUtilities.replaceUIActionMap(header, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * Populates TableHeader's actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    static void loadActionMap(LazyActionMap map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        map.put(new Actions(Actions.TOGGLE_SORT_ORDER));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        map.put(new Actions(Actions.SELECT_COLUMN_TO_LEFT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        map.put(new Actions(Actions.SELECT_COLUMN_TO_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        map.put(new Actions(Actions.MOVE_COLUMN_LEFT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        map.put(new Actions(Actions.MOVE_COLUMN_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        map.put(new Actions(Actions.RESIZE_LEFT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        map.put(new Actions(Actions.RESIZE_RIGHT));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        map.put(new Actions(Actions.FOCUS_TABLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
// Support for mouse rollover
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Returns the index of the column header over which the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * currently is. When the mouse is not over the table header,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * -1 is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @see #rolloverColumnUpdated(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return the index of the current rollover column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    protected int getRolloverColumn() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return rolloverColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
4394
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   451
     * This method gets called every time when a rollover column in the table
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   452
     * header is updated. Every look and feel that supports a rollover effect
92a8ec883f5d 6827653: Make Synth UI classes public
peterz
parents: 2
diff changeset
   453
     * in a table header should override this method and repaint the header.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param oldColumn the index of the previous rollover column or -1 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * mouse was not over a column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @param newColumn the index of the new rollover column or -1 if the mouse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * is not over a column
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * @see #getRolloverColumn()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @see JTableHeader#getHeaderRect(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    protected void rolloverColumnUpdated(int oldColumn, int newColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void updateRolloverColumn(MouseEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        if (header.getDraggedColumn() == null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            header.contains(e.getPoint())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            int col = header.columnAtPoint(e.getPoint());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            if (col != rolloverColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                int oldRolloverColumn = rolloverColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                rolloverColumn = col;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                rolloverColumnUpdated(oldRolloverColumn, rolloverColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
// Support for keyboard and mouse access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private int selectNextColumn(boolean doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        int newIndex = getSelectedColumnIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (newIndex < header.getColumnModel().getColumnCount() - 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            newIndex++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if (doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                selectColumn(newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        return newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private int selectPreviousColumn(boolean doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        int newIndex = getSelectedColumnIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (newIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            newIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            if (doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                selectColumn(newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Selects the specified column in the table header. Repaints the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * affected header cells and makes sure the newly selected one is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    void selectColumn(int newColIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        selectColumn(newColIndex, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    void selectColumn(int newColIndex, boolean doScroll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        Rectangle repaintRect = header.getHeaderRect(selectedColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        header.repaint(repaintRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        selectedColumnIndex = newColIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        repaintRect = header.getHeaderRect(newColIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        header.repaint(repaintRect);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
        if (doScroll) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            scrollToColumn(newColIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
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
     * Used by selectColumn to scroll horizontally, if necessary,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * to ensure that the newly selected column is visible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    private void scrollToColumn(int col) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        Container container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        JTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        //Test whether the header is in a scroll pane and has a table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        if ((header.getParent() == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            ((container = header.getParent().getParent()) == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            !(container instanceof JScrollPane) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            ((table = header.getTable()) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        //Now scroll, if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        Rectangle vis = table.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        Rectangle cellBounds = table.getCellRect(0, col, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        vis.x = cellBounds.x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        vis.width = cellBounds.width;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        table.scrollRectToVisible(vis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    private int getSelectedColumnIndex() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        int numCols = header.getColumnModel().getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        if (selectedColumnIndex >= numCols && numCols > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            selectedColumnIndex = numCols - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        return selectedColumnIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    private static boolean canResize(TableColumn column,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                                     JTableHeader header) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        return (column != null) && header.getResizingAllowed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                                && column.getResizable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    private int changeColumnWidth(TableColumn resizingColumn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                  JTableHeader th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                  int oldWidth, int newWidth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        resizingColumn.setWidth(newWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        Container container;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        JTable table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if ((th.getParent() == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            ((container = th.getParent().getParent()) == null) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            !(container instanceof JScrollPane) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            ((table = th.getTable()) == null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (!container.getComponentOrientation().isLeftToRight() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                !th.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                JViewport viewport = ((JScrollPane)container).getViewport();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                int viewportWidth = viewport.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                int diff = newWidth - oldWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                int newHeaderWidth = table.getWidth() + diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                /* Resize a table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                Dimension tableSize = table.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                tableSize.width += diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                table.setSize(tableSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                /* If this table is in AUTO_RESIZE_OFF mode and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                 * has a horizontal scrollbar, we need to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                 * a view's position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                if ((newHeaderWidth >= viewportWidth) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                    (table.getAutoResizeMode() == JTable.AUTO_RESIZE_OFF)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    Point p = viewport.getViewPosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    p.x = Math.max(0, Math.min(newHeaderWidth - viewportWidth,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                                               p.x + diff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    viewport.setViewPosition(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    return diff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
// Baseline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Returns the baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @throws NullPointerException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @throws IllegalArgumentException {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @see javax.swing.JComponent#getBaseline(int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public int getBaseline(JComponent c, int width, int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        super.getBaseline(c, width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        int baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        TableColumnModel columnModel = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        for(int column = 0; column < columnModel.getColumnCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            TableColumn aColumn = columnModel.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            Component comp = getHeaderRenderer(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            Dimension pref = comp.getPreferredSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            int columnBaseline = comp.getBaseline(pref.width, height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            if (columnBaseline >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                if (baseline == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                    baseline = columnBaseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                else if (baseline != columnBaseline) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    baseline = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return baseline;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
// Paint Methods and support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public void paint(Graphics g, JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        if (header.getColumnModel().getColumnCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        boolean ltr = header.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        Rectangle clip = g.getClipBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        Point left = clip.getLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        Point right = new Point( clip.x + clip.width - 1, clip.y );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        TableColumnModel cm = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        int cMin = header.columnAtPoint( ltr ? left : right );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        int cMax = header.columnAtPoint( ltr ? right : left );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        // This should never happen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        if (cMin == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            cMin =  0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        // If the table does not have enough columns to fill the view we'll get -1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        // Replace this with the index of the last column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        if (cMax == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            cMax = cm.getColumnCount()-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        TableColumn draggedColumn = header.getDraggedColumn();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        int columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        Rectangle cellRect = header.getHeaderRect(ltr ? cMin : cMax);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        TableColumn aColumn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        if (ltr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            for(int column = cMin; column <= cMax ; column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                cellRect.width = columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                    paintCell(g, cellRect, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                cellRect.x += columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            for(int column = cMax; column >= cMin; column--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                aColumn = cm.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                columnWidth = aColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                cellRect.width = columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                if (aColumn != draggedColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    paintCell(g, cellRect, column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                cellRect.x += columnWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // Paint the dragged column if we are dragging.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        if (draggedColumn != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            int draggedColumnIndex = viewIndexForColumn(draggedColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            Rectangle draggedCellRect = header.getHeaderRect(draggedColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            // Draw a gray well in place of the moving column.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            g.setColor(header.getParent().getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            g.fillRect(draggedCellRect.x, draggedCellRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                               draggedCellRect.width, draggedCellRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            draggedCellRect.x += header.getDraggedDistance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            // Fill the background.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            g.setColor(header.getBackground());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
            g.fillRect(draggedCellRect.x, draggedCellRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                       draggedCellRect.width, draggedCellRect.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            paintCell(g, draggedCellRect, draggedColumnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        // Remove all components in the rendererPane.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        rendererPane.removeAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    private Component getHeaderRenderer(int columnIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        TableColumn aColumn = header.getColumnModel().getColumn(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        TableCellRenderer renderer = aColumn.getHeaderRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (renderer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            renderer = header.getDefaultRenderer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        boolean hasFocus = !header.isPaintingForPrint()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                           && (columnIndex == getSelectedColumnIndex())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                           && header.hasFocus();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        return renderer.getTableCellRendererComponent(header.getTable(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                                                aColumn.getHeaderValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                                                false, hasFocus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                                                -1, columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    private void paintCell(Graphics g, Rectangle cellRect, int columnIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        Component component = getHeaderRenderer(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        rendererPane.paintComponent(g, component, header, cellRect.x, cellRect.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                            cellRect.width, cellRect.height, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    private int viewIndexForColumn(TableColumn aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        TableColumnModel cm = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        for (int column = 0; column < cm.getColumnCount(); column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
            if (cm.getColumn(column) == aColumn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                return column;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
// Size Methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
//
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    private int getHeaderHeight() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        int height = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        boolean accomodatedDefault = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        TableColumnModel columnModel = header.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        for(int column = 0; column < columnModel.getColumnCount(); column++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            TableColumn aColumn = columnModel.getColumn(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            boolean isDefault = (aColumn.getHeaderRenderer() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            if (!isDefault || !accomodatedDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                Component comp = getHeaderRenderer(column);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                int rendererHeight = comp.getPreferredSize().height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                height = Math.max(height, rendererHeight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                // Configuring the header renderer to calculate its preferred size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                // is expensive. Optimise this by assuming the default renderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                // always has the same height as the first non-zero height that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                // it returns for a non-null/non-empty value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                if (isDefault && rendererHeight > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    Object headerValue = aColumn.getHeaderValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    if (headerValue != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                        headerValue = headerValue.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        if (headerValue != null && !headerValue.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                            accomodatedDefault = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return height;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
    private Dimension createHeaderSize(long width) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        // None of the callers include the intercell spacing, do it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        if (width > Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            width = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        return new Dimension((int)width, getHeaderHeight());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * Return the minimum size of the header. The minimum width is the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * of the minimum widths of each column (plus inter-cell spacing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    public Dimension getMinimumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        long width = 0;
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   798
        Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        while (enumeration.hasMoreElements()) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   800
            TableColumn aColumn = enumeration.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            width = width + aColumn.getMinWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return createHeaderSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * Return the preferred size of the header. The preferred height is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * maximum of the preferred heights of all of the components provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * by the header renderers. The preferred width is the sum of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * preferred widths of each column (plus inter-cell spacing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
    public Dimension getPreferredSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        long width = 0;
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   814
        Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        while (enumeration.hasMoreElements()) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   816
            TableColumn aColumn = enumeration.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            width = width + aColumn.getPreferredWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return createHeaderSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * Return the maximum size of the header. The maximum width is the sum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * of the maximum widths of each column (plus inter-cell spacing).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    public Dimension getMaximumSize(JComponent c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        long width = 0;
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   828
        Enumeration<TableColumn> enumeration = header.getColumnModel().getColumns();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        while (enumeration.hasMoreElements()) {
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   830
            TableColumn aColumn = enumeration.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            width = width + aColumn.getMaxWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        return createHeaderSize(width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    private static class Actions extends UIAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        public static final String TOGGLE_SORT_ORDER =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            "toggleSortOrder";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        public static final String SELECT_COLUMN_TO_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            "selectColumnToLeft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        public static final String SELECT_COLUMN_TO_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            "selectColumnToRight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        public static final String MOVE_COLUMN_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            "moveColumnLeft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        public static final String MOVE_COLUMN_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            "moveColumnRight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        public static final String RESIZE_LEFT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
            "resizeLeft";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        public static final String RESIZE_RIGHT =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
            "resizeRight";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        public static final String FOCUS_TABLE =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            "focusTable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        public Actions(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
35650
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 32669
diff changeset
   858
        @Override
15f1f0ff7790 8133039: Provide public API to sun.swing.UIAction#isEnabled(Object)
alexsch
parents: 32669
diff changeset
   859
        public boolean accept(Object sender) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            if (sender instanceof JTableHeader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                JTableHeader th = (JTableHeader)sender;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                TableColumnModel cm = th.getColumnModel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                if (cm.getColumnCount() <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                String key = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                BasicTableHeaderUI ui =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    (BasicTableHeaderUI)BasicLookAndFeel.getUIOfType(th.getUI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                                                      BasicTableHeaderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                if (ui != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                    if (key == MOVE_COLUMN_LEFT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                        return th.getReorderingAllowed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                            && maybeMoveColumn(true, th, ui, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    } else if (key == MOVE_COLUMN_RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        return th.getReorderingAllowed()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                            && maybeMoveColumn(false, th, ui, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                    } else if (key == RESIZE_LEFT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                               key == RESIZE_RIGHT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                        return canResize(cm.getColumn(ui.getSelectedColumnIndex()), th);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    } else if (key == FOCUS_TABLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                        return (th.getTable() != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        public void actionPerformed(ActionEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            JTableHeader th = (JTableHeader)e.getSource();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            BasicTableHeaderUI ui =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                (BasicTableHeaderUI)BasicLookAndFeel.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                                        getUIOfType(th.getUI(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                                            BasicTableHeaderUI.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            if (ui == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            String name = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            if (TOGGLE_SORT_ORDER == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
                JTable table = th.getTable();
25565
ce603b34c98d 8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.*
darcy
parents: 23010
diff changeset
   902
                RowSorter<?> sorter = table == null ? null : table.getRowSorter();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                if (sorter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    int columnIndex = ui.getSelectedColumnIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                    columnIndex = table.convertColumnIndexToModel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                                                      columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                    sorter.toggleSortOrder(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            } else if (SELECT_COLUMN_TO_LEFT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                if (th.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                    ui.selectPreviousColumn(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
                    ui.selectNextColumn(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            } else if (SELECT_COLUMN_TO_RIGHT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
                if (th.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
                    ui.selectNextColumn(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
                    ui.selectPreviousColumn(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            } else if (MOVE_COLUMN_LEFT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
                moveColumn(true, th, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
            } else if (MOVE_COLUMN_RIGHT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
                moveColumn(false, th, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            } else if (RESIZE_LEFT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                resize(true, th, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            } else if (RESIZE_RIGHT == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                resize(false, th, ui);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            } else if (FOCUS_TABLE == name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                JTable table = th.getTable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                if (table != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                    table.requestFocusInWindow();
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
        private void moveColumn(boolean leftArrow, JTableHeader th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                                BasicTableHeaderUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            maybeMoveColumn(leftArrow, th, ui, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        private boolean maybeMoveColumn(boolean leftArrow, JTableHeader th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                                        BasicTableHeaderUI ui, boolean doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            int oldIndex = ui.getSelectedColumnIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            int newIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (th.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                newIndex = leftArrow ? ui.selectPreviousColumn(doIt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                                     : ui.selectNextColumn(doIt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                newIndex = leftArrow ? ui.selectNextColumn(doIt)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                                     : ui.selectPreviousColumn(doIt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            if (newIndex != oldIndex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                if (doIt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                    th.getColumnModel().moveColumn(oldIndex, newIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                    return true; // we'd do the move if asked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
        private void resize(boolean leftArrow, JTableHeader th,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                            BasicTableHeaderUI ui) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            int columnIndex = ui.getSelectedColumnIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
            TableColumn resizingColumn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
                th.getColumnModel().getColumn(columnIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            th.setResizingColumn(resizingColumn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            int oldWidth = resizingColumn.getWidth();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            int newWidth = oldWidth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            if (th.getComponentOrientation().isLeftToRight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                newWidth = newWidth + (leftArrow ? -1 : 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                newWidth = newWidth + (leftArrow ? 1 : -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
            ui.changeColumnWidth(resizingColumn, th, oldWidth, newWidth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
}  // End of Class BasicTableHeaderUI