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