6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
Reviewed-by: rupashka
This commit is contained in:
parent
f6adc01088
commit
dd916173db
@ -35,6 +35,7 @@ import javax.swing.table.*;
|
|||||||
import static com.sun.java.swing.plaf.windows.TMSchema.*;
|
import static com.sun.java.swing.plaf.windows.TMSchema.*;
|
||||||
import static com.sun.java.swing.plaf.windows.XPStyle.*;
|
import static com.sun.java.swing.plaf.windows.XPStyle.*;
|
||||||
import sun.swing.table.*;
|
import sun.swing.table.*;
|
||||||
|
import sun.swing.SwingUtilities2;
|
||||||
|
|
||||||
|
|
||||||
public class WindowsTableHeaderUI extends BasicTableHeaderUI {
|
public class WindowsTableHeaderUI extends BasicTableHeaderUI {
|
||||||
@ -163,18 +164,13 @@ public class WindowsTableHeaderUI extends BasicTableHeaderUI {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int viewIndexForColumn(TableColumn aColumn) {
|
|
||||||
if (aColumn != null) {
|
|
||||||
return header.getTable().convertColumnIndexToView(
|
|
||||||
aColumn.getModelIndex());
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void paint(Graphics g) {
|
public void paint(Graphics g) {
|
||||||
Dimension size = getSize();
|
Dimension size = getSize();
|
||||||
State state = State.NORMAL;
|
State state = State.NORMAL;
|
||||||
if (column == viewIndexForColumn(header.getDraggedColumn())) {
|
TableColumn draggedColumn = header.getDraggedColumn();
|
||||||
|
if (draggedColumn != null &&
|
||||||
|
column == SwingUtilities2.convertColumnIndexToView(
|
||||||
|
header.getColumnModel(), draggedColumn.getModelIndex())) {
|
||||||
state = State.PRESSED;
|
state = State.PRESSED;
|
||||||
} else if (isSelected || hasFocus || hasRollover) {
|
} else if (isSelected || hasFocus || hasRollover) {
|
||||||
state = State.HOT;
|
state = State.HOT;
|
||||||
|
@ -2583,10 +2583,8 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
|
|||||||
* @see #convertColumnIndexToView
|
* @see #convertColumnIndexToView
|
||||||
*/
|
*/
|
||||||
public int convertColumnIndexToModel(int viewColumnIndex) {
|
public int convertColumnIndexToModel(int viewColumnIndex) {
|
||||||
if (viewColumnIndex < 0) {
|
return SwingUtilities2.convertColumnIndexToModel(
|
||||||
return viewColumnIndex;
|
getColumnModel(), viewColumnIndex);
|
||||||
}
|
|
||||||
return getColumnModel().getColumn(viewColumnIndex).getModelIndex();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2603,16 +2601,8 @@ public class JTable extends JComponent implements TableModelListener, Scrollable
|
|||||||
* @see #convertColumnIndexToModel
|
* @see #convertColumnIndexToModel
|
||||||
*/
|
*/
|
||||||
public int convertColumnIndexToView(int modelColumnIndex) {
|
public int convertColumnIndexToView(int modelColumnIndex) {
|
||||||
if (modelColumnIndex < 0) {
|
return SwingUtilities2.convertColumnIndexToView(
|
||||||
return modelColumnIndex;
|
getColumnModel(), modelColumnIndex);
|
||||||
}
|
|
||||||
TableColumnModel cm = getColumnModel();
|
|
||||||
for (int column = 0; column < getColumnCount(); column++) {
|
|
||||||
if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
|
|
||||||
return column;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -98,15 +98,18 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
private Cursor otherCursor = resizeCursor;
|
private Cursor otherCursor = resizeCursor;
|
||||||
|
|
||||||
public void mouseClicked(MouseEvent e) {
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (e.getClickCount() % 2 == 1 &&
|
if (e.getClickCount() % 2 == 1 &&
|
||||||
SwingUtilities.isLeftMouseButton(e)){
|
SwingUtilities.isLeftMouseButton(e)) {
|
||||||
JTable table = header.getTable();
|
JTable table = header.getTable();
|
||||||
RowSorter sorter;
|
RowSorter sorter;
|
||||||
if (table != null && (sorter = table.getRowSorter()) != null) {
|
if (table != null && (sorter = table.getRowSorter()) != null) {
|
||||||
int columnIndex = header.columnAtPoint(e.getPoint());
|
int columnIndex = header.columnAtPoint(e.getPoint());
|
||||||
if (columnIndex != -1) {
|
if (columnIndex != -1) {
|
||||||
columnIndex = table.convertColumnIndexToModel(
|
columnIndex = table.convertColumnIndexToModel(
|
||||||
columnIndex);
|
columnIndex);
|
||||||
sorter.toggleSortOrder(columnIndex);
|
sorter.toggleSortOrder(columnIndex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -140,6 +143,9 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void mousePressed(MouseEvent e) {
|
public void mousePressed(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
header.setDraggedColumn(null);
|
header.setDraggedColumn(null);
|
||||||
header.setResizingColumn(null);
|
header.setResizingColumn(null);
|
||||||
header.setDraggedDistance(0);
|
header.setDraggedDistance(0);
|
||||||
@ -182,6 +188,9 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void mouseMoved(MouseEvent e) {
|
public void mouseMoved(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (canResize(getResizingColumn(e.getPoint()), header) !=
|
if (canResize(getResizingColumn(e.getPoint()), header) !=
|
||||||
(header.getCursor() == resizeCursor)) {
|
(header.getCursor() == resizeCursor)) {
|
||||||
swapCursor();
|
swapCursor();
|
||||||
@ -190,6 +199,9 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void mouseDragged(MouseEvent e) {
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int mouseX = e.getX();
|
int mouseX = e.getX();
|
||||||
|
|
||||||
TableColumn resizingColumn = header.getResizingColumn();
|
TableColumn resizingColumn = header.getResizingColumn();
|
||||||
@ -217,21 +229,23 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
if (0 <= newColumnIndex && newColumnIndex < cm.getColumnCount()) {
|
if (0 <= newColumnIndex && newColumnIndex < cm.getColumnCount()) {
|
||||||
int width = cm.getColumn(newColumnIndex).getWidth();
|
int width = cm.getColumn(newColumnIndex).getWidth();
|
||||||
if (Math.abs(draggedDistance) > (width / 2)) {
|
if (Math.abs(draggedDistance) > (width / 2)) {
|
||||||
JTable table = header.getTable();
|
|
||||||
|
|
||||||
mouseXOffset = mouseXOffset + direction * width;
|
mouseXOffset = mouseXOffset + direction * width;
|
||||||
header.setDraggedDistance(draggedDistance - direction * width);
|
header.setDraggedDistance(draggedDistance - direction * width);
|
||||||
|
|
||||||
//Cache the selected column.
|
//Cache the selected column.
|
||||||
int selectedIndex = table.convertColumnIndexToModel(
|
int selectedIndex =
|
||||||
getSelectedColumnIndex());
|
SwingUtilities2.convertColumnIndexToModel(
|
||||||
|
header.getColumnModel(),
|
||||||
|
getSelectedColumnIndex());
|
||||||
|
|
||||||
//Now do the move.
|
//Now do the move.
|
||||||
cm.moveColumn(columnIndex, newColumnIndex);
|
cm.moveColumn(columnIndex, newColumnIndex);
|
||||||
|
|
||||||
//Update the selected index.
|
//Update the selected index.
|
||||||
selectColumn(
|
selectColumn(
|
||||||
table.convertColumnIndexToView(selectedIndex),
|
SwingUtilities2.convertColumnIndexToView(
|
||||||
|
header.getColumnModel(), selectedIndex),
|
||||||
false);
|
false);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -244,6 +258,9 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void mouseReleased(MouseEvent e) {
|
public void mouseReleased(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
setDraggedDistance(0, viewIndexForColumn(header.getDraggedColumn()));
|
setDraggedDistance(0, viewIndexForColumn(header.getDraggedColumn()));
|
||||||
|
|
||||||
header.setResizingColumn(null);
|
header.setResizingColumn(null);
|
||||||
@ -253,10 +270,16 @@ public class BasicTableHeaderUI extends TableHeaderUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void mouseEntered(MouseEvent e) {
|
public void mouseEntered(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
updateRolloverColumn(e);
|
updateRolloverColumn(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void mouseExited(MouseEvent e) {
|
public void mouseExited(MouseEvent e) {
|
||||||
|
if (SwingUtilities2.shouldIgnore(e, header)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
int oldRolloverColumn = rolloverColumn;
|
int oldRolloverColumn = rolloverColumn;
|
||||||
rolloverColumn = -1;
|
rolloverColumn = -1;
|
||||||
rolloverColumnUpdated(oldRolloverColumn, rolloverColumn);
|
rolloverColumnUpdated(oldRolloverColumn, rolloverColumn);
|
||||||
|
@ -44,6 +44,8 @@ import javax.swing.text.JTextComponent;
|
|||||||
import javax.swing.text.DefaultHighlighter;
|
import javax.swing.text.DefaultHighlighter;
|
||||||
import javax.swing.text.DefaultCaret;
|
import javax.swing.text.DefaultCaret;
|
||||||
import javax.swing.table.TableCellRenderer;
|
import javax.swing.table.TableCellRenderer;
|
||||||
|
import javax.swing.table.TableColumnModel;
|
||||||
|
|
||||||
import sun.swing.PrintColorUIResource;
|
import sun.swing.PrintColorUIResource;
|
||||||
import sun.swing.ImageIconUIResource;
|
import sun.swing.ImageIconUIResource;
|
||||||
import sun.print.ProxyPrintGraphics;
|
import sun.print.ProxyPrintGraphics;
|
||||||
@ -1807,4 +1809,54 @@ public class SwingUtilities2 {
|
|||||||
boolean three) {
|
boolean three) {
|
||||||
return liesIn(rect, p, false, false, three);
|
return liesIn(rect, p, false, false, three);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the index of the column in the view at
|
||||||
|
* {@code viewColumnIndex} to the index of the column
|
||||||
|
* in the table model. Returns the index of the corresponding
|
||||||
|
* column in the model. If {@code viewColumnIndex}
|
||||||
|
* is less than zero, returns {@code viewColumnIndex}.
|
||||||
|
*
|
||||||
|
* @param cm the table model
|
||||||
|
* @param viewColumnIndex the index of the column in the view
|
||||||
|
* @return the index of the corresponding column in the model
|
||||||
|
*
|
||||||
|
* @see JTable#convertColumnIndexToModel(int)
|
||||||
|
* @see javax.swing.plaf.basic.BasicTableHeaderUI
|
||||||
|
*/
|
||||||
|
public static int convertColumnIndexToModel(TableColumnModel cm,
|
||||||
|
int viewColumnIndex) {
|
||||||
|
if (viewColumnIndex < 0) {
|
||||||
|
return viewColumnIndex;
|
||||||
|
}
|
||||||
|
return cm.getColumn(viewColumnIndex).getModelIndex();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maps the index of the column in the {@code cm} at
|
||||||
|
* {@code modelColumnIndex} to the index of the column
|
||||||
|
* in the view. Returns the index of the
|
||||||
|
* corresponding column in the view; returns {@code -1} if this column
|
||||||
|
* is not being displayed. If {@code modelColumnIndex} is less than zero,
|
||||||
|
* returns {@code modelColumnIndex}.
|
||||||
|
*
|
||||||
|
* @param cm the table model
|
||||||
|
* @param modelColumnIndex the index of the column in the model
|
||||||
|
* @return the index of the corresponding column in the view
|
||||||
|
*
|
||||||
|
* @see JTable#convertColumnIndexToView(int)
|
||||||
|
* @see javax.swing.plaf.basic.BasicTableHeaderUI
|
||||||
|
*/
|
||||||
|
public static int convertColumnIndexToView(TableColumnModel cm,
|
||||||
|
int modelColumnIndex) {
|
||||||
|
if (modelColumnIndex < 0) {
|
||||||
|
return modelColumnIndex;
|
||||||
|
}
|
||||||
|
for (int column = 0; column < cm.getColumnCount(); column++) {
|
||||||
|
if (cm.getColumn(column).getModelIndex() == modelColumnIndex) {
|
||||||
|
return column;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
89
jdk/test/javax/swing/JTableHeader/6884066/bug6884066.java
Normal file
89
jdk/test/javax/swing/JTableHeader/6884066/bug6884066.java
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010 Sun Microsystems, Inc. All Rights Reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
||||||
|
* CA 95054 USA or visit www.sun.com if you need additional information or
|
||||||
|
* have any questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* @test
|
||||||
|
@bug 6884066
|
||||||
|
@summary JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
|
||||||
|
@author Alexander Potochkin
|
||||||
|
@run main bug6884066
|
||||||
|
*/
|
||||||
|
|
||||||
|
import sun.awt.SunToolkit;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.table.JTableHeader;
|
||||||
|
import javax.swing.table.TableColumnModel;
|
||||||
|
import javax.swing.table.TableColumn;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.InputEvent;
|
||||||
|
|
||||||
|
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
|
||||||
|
|
||||||
|
public class bug6884066 {
|
||||||
|
private static JTableHeader header;
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
|
||||||
|
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
|
||||||
|
Robot robot = new Robot();
|
||||||
|
robot.setAutoDelay(20);
|
||||||
|
SwingUtilities.invokeAndWait(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
// just to quickly grab a column model
|
||||||
|
JTable table = new JTable(10, 5);
|
||||||
|
header = new JTableHeader(table.getColumnModel());
|
||||||
|
checkColumn(0, "A");
|
||||||
|
JFrame frame = new JFrame("standalone header");
|
||||||
|
frame.add(header);
|
||||||
|
frame.pack();
|
||||||
|
frame.setVisible(true);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
toolkit.realSync();
|
||||||
|
Point point = header.getLocationOnScreen();
|
||||||
|
robot.mouseMove(point.x + 3, point.y + 3);
|
||||||
|
robot.mousePress(InputEvent.BUTTON1_MASK);
|
||||||
|
for (int i = 0; i < header.getWidth() - 3; i++) {
|
||||||
|
robot.mouseMove(point.x + i, point.y + 3);
|
||||||
|
}
|
||||||
|
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
||||||
|
SwingUtilities.invokeAndWait(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
TableColumnModel model = header.getColumnModel();
|
||||||
|
checkColumn(model.getColumnCount() - 1, "A");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void checkColumn(int index, String str) {
|
||||||
|
TableColumnModel model = header.getColumnModel();
|
||||||
|
Object value = model.getColumn(index).getHeaderValue();
|
||||||
|
if (!str.equals(value)) {
|
||||||
|
throw new RuntimeException("Unexpected header's value; " +
|
||||||
|
"index = " + index + " value = " + value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user