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