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