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/*
25   @test
26  @key headful
27   @bug 6884066
28   @summary JTableHeader listens mouse in disabled state and doesn't work when not attached to a table
29   @author Alexander Potochkin
30   @run main bug6884066
31*/
32
33import javax.swing.*;
34import javax.swing.table.JTableHeader;
35import javax.swing.table.TableColumnModel;
36import javax.swing.table.TableColumn;
37import java.awt.*;
38import java.awt.event.InputEvent;
39
40public class bug6884066 {
41    private static JTableHeader header;
42
43    public static void main(String[] args) throws Exception {
44
45        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
46
47        Robot robot = new Robot();
48        robot.setAutoDelay(20);
49        SwingUtilities.invokeAndWait(new Runnable() {
50            public void run() {
51                // just to quickly grab a column model
52                JTable table = new JTable(10, 5);
53                header = new JTableHeader(table.getColumnModel());
54                checkColumn(0, "A");
55                JFrame frame = new JFrame("standalone header");
56                frame.add(header);
57                frame.pack();
58                frame.setVisible(true);
59                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
60            }
61        });
62        robot.waitForIdle();
63        Point point = header.getLocationOnScreen();
64        robot.mouseMove(point.x + 3, point.y + 3);
65        robot.mousePress(InputEvent.BUTTON1_MASK);
66        for (int i = 0; i < header.getWidth() - 3; i++) {
67            robot.mouseMove(point.x + i, point.y + 3);
68        }
69        robot.mouseRelease(InputEvent.BUTTON1_MASK);
70        SwingUtilities.invokeAndWait(new Runnable() {
71            public void run() {
72                TableColumnModel model = header.getColumnModel();
73                checkColumn(model.getColumnCount() - 1, "A");
74            }
75        });
76    }
77
78    private static void checkColumn(int index, String str) {
79        TableColumnModel model = header.getColumnModel();
80        Object value = model.getColumn(index).getHeaderValue();
81        if (!str.equals(value)) {
82            throw new RuntimeException("Unexpected header's value; " +
83                    "index = " + index + " value = " + value);
84        }
85    }
86}
87