# HG changeset patch # User alexp # Date 1274806499 -14400 # Node ID 3ec077d7e89363d8a9e90d6f1248c9e844ae8c32 # Parent 5e4f59f34eea02392f4bc83f030bdc08ad588312 6884066: JTableHeader listens mouse in disabled state and doesn't work when not attached to a table Reviewed-by: rupashka diff -r 5e4f59f34eea -r 3ec077d7e893 jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsTableHeaderUI.java --- a/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsTableHeaderUI.java Tue May 25 20:39:52 2010 +0400 +++ b/jdk/src/share/classes/com/sun/java/swing/plaf/windows/WindowsTableHeaderUI.java Tue May 25 20:54:59 2010 +0400 @@ -35,6 +35,7 @@ import static com.sun.java.swing.plaf.windows.TMSchema.*; import static com.sun.java.swing.plaf.windows.XPStyle.*; import sun.swing.table.*; +import sun.swing.SwingUtilities2; public class WindowsTableHeaderUI extends BasicTableHeaderUI { @@ -163,18 +164,13 @@ return this; } - private int viewIndexForColumn(TableColumn aColumn) { - if (aColumn != null) { - return header.getTable().convertColumnIndexToView( - aColumn.getModelIndex()); - } - return -1; - } - public void paint(Graphics g) { Dimension size = getSize(); 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; } else if (isSelected || hasFocus || hasRollover) { state = State.HOT; diff -r 5e4f59f34eea -r 3ec077d7e893 jdk/src/share/classes/javax/swing/JTable.java --- a/jdk/src/share/classes/javax/swing/JTable.java Tue May 25 20:39:52 2010 +0400 +++ b/jdk/src/share/classes/javax/swing/JTable.java Tue May 25 20:54:59 2010 +0400 @@ -2583,10 +2583,8 @@ * @see #convertColumnIndexToView */ public int convertColumnIndexToModel(int viewColumnIndex) { - if (viewColumnIndex < 0) { - return viewColumnIndex; - } - return getColumnModel().getColumn(viewColumnIndex).getModelIndex(); + return SwingUtilities2.convertColumnIndexToModel( + getColumnModel(), viewColumnIndex); } /** @@ -2603,16 +2601,8 @@ * @see #convertColumnIndexToModel */ public int convertColumnIndexToView(int modelColumnIndex) { - if (modelColumnIndex < 0) { - return modelColumnIndex; - } - TableColumnModel cm = getColumnModel(); - for (int column = 0; column < getColumnCount(); column++) { - if (cm.getColumn(column).getModelIndex() == modelColumnIndex) { - return column; - } - } - return -1; + return SwingUtilities2.convertColumnIndexToView( + getColumnModel(), modelColumnIndex); } /** diff -r 5e4f59f34eea -r 3ec077d7e893 jdk/src/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java --- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java Tue May 25 20:39:52 2010 +0400 +++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicTableHeaderUI.java Tue May 25 20:54:59 2010 +0400 @@ -98,15 +98,18 @@ private Cursor otherCursor = resizeCursor; public void mouseClicked(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } if (e.getClickCount() % 2 == 1 && - SwingUtilities.isLeftMouseButton(e)){ + SwingUtilities.isLeftMouseButton(e)) { JTable table = header.getTable(); RowSorter sorter; if (table != null && (sorter = table.getRowSorter()) != null) { int columnIndex = header.columnAtPoint(e.getPoint()); if (columnIndex != -1) { columnIndex = table.convertColumnIndexToModel( - columnIndex); + columnIndex); sorter.toggleSortOrder(columnIndex); } } @@ -140,6 +143,9 @@ } public void mousePressed(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } header.setDraggedColumn(null); header.setResizingColumn(null); header.setDraggedDistance(0); @@ -182,6 +188,9 @@ } public void mouseMoved(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } if (canResize(getResizingColumn(e.getPoint()), header) != (header.getCursor() == resizeCursor)) { swapCursor(); @@ -190,6 +199,9 @@ } public void mouseDragged(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } int mouseX = e.getX(); TableColumn resizingColumn = header.getResizingColumn(); @@ -217,21 +229,23 @@ if (0 <= newColumnIndex && newColumnIndex < cm.getColumnCount()) { int width = cm.getColumn(newColumnIndex).getWidth(); if (Math.abs(draggedDistance) > (width / 2)) { - JTable table = header.getTable(); mouseXOffset = mouseXOffset + direction * width; header.setDraggedDistance(draggedDistance - direction * width); //Cache the selected column. - int selectedIndex = table.convertColumnIndexToModel( - getSelectedColumnIndex()); + int selectedIndex = + SwingUtilities2.convertColumnIndexToModel( + header.getColumnModel(), + getSelectedColumnIndex()); //Now do the move. cm.moveColumn(columnIndex, newColumnIndex); //Update the selected index. selectColumn( - table.convertColumnIndexToView(selectedIndex), + SwingUtilities2.convertColumnIndexToView( + header.getColumnModel(), selectedIndex), false); return; @@ -244,6 +258,9 @@ } public void mouseReleased(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } setDraggedDistance(0, viewIndexForColumn(header.getDraggedColumn())); header.setResizingColumn(null); @@ -253,10 +270,16 @@ } public void mouseEntered(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } updateRolloverColumn(e); } public void mouseExited(MouseEvent e) { + if (SwingUtilities2.shouldIgnore(e, header)) { + return; + } int oldRolloverColumn = rolloverColumn; rolloverColumn = -1; rolloverColumnUpdated(oldRolloverColumn, rolloverColumn); diff -r 5e4f59f34eea -r 3ec077d7e893 jdk/src/share/classes/sun/swing/SwingUtilities2.java --- a/jdk/src/share/classes/sun/swing/SwingUtilities2.java Tue May 25 20:39:52 2010 +0400 +++ b/jdk/src/share/classes/sun/swing/SwingUtilities2.java Tue May 25 20:54:59 2010 +0400 @@ -44,6 +44,8 @@ import javax.swing.text.DefaultHighlighter; import javax.swing.text.DefaultCaret; import javax.swing.table.TableCellRenderer; +import javax.swing.table.TableColumnModel; + import sun.swing.PrintColorUIResource; import sun.swing.ImageIconUIResource; import sun.print.ProxyPrintGraphics; @@ -1807,4 +1809,54 @@ boolean 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; + } } diff -r 5e4f59f34eea -r 3ec077d7e893 jdk/test/javax/swing/JTableHeader/6884066/bug6884066.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/javax/swing/JTableHeader/6884066/bug6884066.java Tue May 25 20:54:59 2010 +0400 @@ -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); + } + } +}