1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /* @test
  25    @bug 6884066
  26    @summary JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
  27    @author Alexander Potochkin
  28    @run main bug6884066
  29 */
  30 
  31 import sun.awt.SunToolkit;
  32 
  33 import javax.swing.*;
  34 import javax.swing.table.JTableHeader;
  35 import javax.swing.table.TableColumnModel;
  36 import javax.swing.table.TableColumn;
  37 import java.awt.*;
  38 import java.awt.event.InputEvent;
  39 
  40 import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
  41 
  42 public class bug6884066 {
  43     private static JTableHeader header;
  44 
  45     public static void main(String[] args) throws Exception {
  46 
  47         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  48 
  49         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  50         Robot robot = new Robot();
  51         robot.setAutoDelay(20);
  52         SwingUtilities.invokeAndWait(new Runnable() {
  53             public void run() {
  54                 // just to quickly grab a column model
  55                 JTable table = new JTable(10, 5);
  56                 header = new JTableHeader(table.getColumnModel());
  57                 checkColumn(0, "A");
  58                 JFrame frame = new JFrame("standalone header");
  59                 frame.add(header);
  60                 frame.pack();
  61                 frame.setVisible(true);
  62                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  63             }
  64         });
  65         toolkit.realSync();
  66         Point point = header.getLocationOnScreen();
  67         robot.mouseMove(point.x + 3, point.y + 3);
  68         robot.mousePress(InputEvent.BUTTON1_MASK);
  69         for (int i = 0; i < header.getWidth() - 3; i++) {
  70             robot.mouseMove(point.x + i, point.y + 3);
  71         }
  72         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  73         SwingUtilities.invokeAndWait(new Runnable() {
  74             public void run() {
  75                 TableColumnModel model = header.getColumnModel();
  76                 checkColumn(model.getColumnCount() - 1, "A");
  77             }
  78         });
  79     }
  80 
  81     private static void checkColumn(int index, String str) {
  82         TableColumnModel model = header.getColumnModel();
  83         Object value = model.getColumn(index).getHeaderValue();
  84         if (!str.equals(value)) {
  85             throw new RuntimeException("Unexpected header's value; " +
  86                     "index = " + index + " value = " + value);
  87         }
  88     }
  89 }