1 /*
   2  * To change this license header, choose License Headers in Project Properties.
   3  * To change this template file, choose Tools | Templates
   4  /*
   5  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  23  * or visit www.oracle.com if you need additional information or have any
  24  * questions.
  25  */
  26 
  27 import java.awt.Point;
  28 import java.awt.Rectangle;
  29 import java.awt.Robot;
  30 import java.awt.event.InputEvent;
  31 import java.awt.event.KeyEvent;
  32 import javax.swing.DefaultCellEditor;
  33 import javax.swing.JComboBox;
  34 import javax.swing.JFrame;
  35 import javax.swing.JTable;
  36 import javax.swing.SwingUtilities;
  37 
  38 /**
  39  * @test
  40  * @key headful
  41  * @bug 8072767
  42  * @author Alexander Scherbatiy
  43  * @summary DefaultCellEditor for comboBox creates ActionEvent with wrong source
  44  *          object
  45  * @run main bug8072767
  46  */
  47 
  48 public class bug8072767 {
  49 
  50     private static final String TEST1 = "Test";
  51     private static final String TEST2 = TEST1 + 1;
  52 
  53     private static JFrame frame;
  54     private static JTable table;
  55     private static Point point;
  56     private static boolean testPass;
  57 
  58     public static void main(String[] args) throws Exception {
  59         Robot robot = new Robot();
  60         robot.setAutoDelay(50);
  61         SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);
  62         robot.waitForIdle();
  63         SwingUtilities.invokeAndWait(() -> {
  64             point = table.getLocationOnScreen();
  65             Rectangle rect = table.getCellRect(0, 0, true);
  66             point.translate(rect.width / 2, rect.height / 2);
  67         });
  68         robot.mouseMove(point.x, point.y);
  69         robot.mousePress(InputEvent.BUTTON1_MASK);
  70         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  71         robot.waitForIdle();
  72 
  73         robot.keyPress(KeyEvent.VK_1);
  74         robot.keyRelease(KeyEvent.VK_1);
  75         robot.waitForIdle();
  76 
  77         SwingUtilities.invokeAndWait(() -> {
  78             point = frame.getLocationOnScreen();
  79             point.translate(frame.getWidth() / 2, frame.getHeight() / 2);
  80         });
  81 
  82         robot.mouseMove(point.x, point.y);
  83         robot.mousePress(InputEvent.BUTTON1_MASK);
  84         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  85         robot.waitForIdle();
  86 
  87         SwingUtilities.invokeAndWait(() -> {
  88             testPass = TEST2.equals(table.getValueAt(0, 0));
  89             frame.dispose();
  90         });
  91 
  92         if (!testPass) {
  93             throw new RuntimeException("Table cell is not edited!");
  94         }
  95     }
  96 
  97     private static void createAndShowGUI() {
  98         frame = new JFrame();
  99         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 100         frame.setSize(200, 200);
 101         frame.setLocation(100, 100);
 102 
 103         table = new JTable(
 104                 new String[][]{{TEST1}}, new String[]{"Header"});
 105         JComboBox<String> comboBox = new JComboBox<>();
 106         comboBox.setEditable(true);
 107         table.getColumnModel().getColumn(0).setCellEditor(
 108                 new DefaultCellEditor(comboBox));
 109         frame.getContentPane().add(table);
 110         frame.setVisible(true);
 111     }
 112 }