1/*
2 * Copyright (c) 2012, 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 4220171
28 * @author Konstantin Eremin
29 * @summary Tests
30 * @library ../../regtesthelpers
31 * @build Util
32 * @run main bug4220171
33 */
34import java.awt.Color;
35import java.awt.Point;
36import java.awt.Rectangle;
37import java.awt.Robot;
38import java.awt.event.InputEvent;
39import java.awt.event.KeyEvent;
40import javax.swing.*;
41import javax.swing.border.LineBorder;
42
43public class bug4220171 {
44
45    private static JTable table;
46
47    public static void main(String args[]) throws Exception {
48
49        Robot robot = new Robot();
50        robot.setAutoDelay(50);
51
52        javax.swing.SwingUtilities.invokeAndWait(new Runnable() {
53
54            public void run() {
55                createAndShowGUI();
56            }
57        });
58
59        robot.waitForIdle();
60
61        clickMouse(robot, 0, 0);
62        Util.hitKeys(robot, KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_ENTER);
63        robot.waitForIdle();
64        checkCell(0, 0);
65
66        clickMouse(robot, 0, 1);
67        Util.hitKeys(robot, KeyEvent.VK_D, KeyEvent.VK_E, KeyEvent.VK_ENTER);
68        robot.waitForIdle();
69        checkCell(0, 1);
70
71        clickMouse(robot, 1, 0);
72        Util.hitKeys(robot, KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_ENTER);
73        robot.waitForIdle();
74        checkCell(1, 0);
75
76        clickMouse(robot, 1, 1);
77        Util.hitKeys(robot, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_ENTER);
78        robot.waitForIdle();
79        checkCell(1, 1);
80    }
81
82    static void checkCell(final int row, final int column) throws Exception {
83        SwingUtilities.invokeAndWait(new Runnable() {
84
85            public void run() {
86                if (table.getValueAt(row, column) != null) {
87                    throw new RuntimeException(
88                            String.format("Cell (%d, %d) is editable", row, column));
89                }
90            }
91        });
92    }
93
94    static void clickMouse(Robot robot, int row, int column) throws Exception {
95        Point point = getCellClickPoint(row, column);
96        robot.mouseMove(point.x, point.y);
97        robot.mousePress(InputEvent.BUTTON1_MASK);
98        robot.mouseRelease(InputEvent.BUTTON1_MASK);
99    }
100
101    private static Point getCellClickPoint(final int row, final int column) throws Exception {
102        final Point[] result = new Point[1];
103        SwingUtilities.invokeAndWait(new Runnable() {
104
105            @Override
106            public void run() {
107                Rectangle rect = table.getCellRect(row, column, false);
108                Point point = new Point(rect.x + rect.width / 2,
109                        rect.y + rect.height / 2);
110                SwingUtilities.convertPointToScreen(point, table);
111                result[0] = point;
112            }
113        });
114
115        return result[0];
116    }
117
118    private static void createAndShowGUI() {
119        JFrame frame = new JFrame("Test");
120        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121        frame.setSize(200, 200);
122
123        table = new JTable(2, 2);
124        table.setEnabled(false);
125
126        frame.getContentPane().add(table);
127        frame.setVisible(true);
128    }
129}
130