1 /*
   2  * Copyright (c) 2009, 2013, 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 6505027
  28  * @summary Tests focus problem inside internal frame
  29  * @author Sergey Malenkov
  30  * @library ..
  31  */
  32 
  33 import java.awt.AWTException;
  34 import java.awt.BorderLayout;
  35 import java.awt.Component;
  36 import java.awt.Container;
  37 import java.awt.KeyboardFocusManager;
  38 import java.awt.Point;
  39 import java.awt.Robot;
  40 import java.awt.event.InputEvent;
  41 import javax.swing.DefaultCellEditor;
  42 import javax.swing.JComboBox;
  43 import javax.swing.JDesktopPane;
  44 import javax.swing.JFrame;
  45 import javax.swing.JInternalFrame;
  46 import javax.swing.JScrollPane;
  47 import javax.swing.JTable;
  48 import javax.swing.JTextField;
  49 import javax.swing.SwingUtilities;
  50 import javax.swing.table.DefaultTableModel;
  51 import javax.swing.table.TableColumn;
  52 
  53 public class Test6505027 {
  54 
  55     private static final boolean INTERNAL = true;
  56     private static final boolean TERMINATE = true;
  57 
  58     private static final int WIDTH = 450;
  59     private static final int HEIGHT = 200;
  60     private static final int OFFSET = 10;
  61 
  62     private static final String[] COLUMNS = { "Size", "Shape" }; // NON-NLS: column names
  63     private static final String[] ITEMS = { "a", "b", "c", "d" }; // NON-NLS: combobox content
  64     private static final String KEY = "terminateEditOnFocusLost"; // NON-NLS: property name
  65 
  66     public static void main(String[] args) throws Throwable {
  67         SwingTest.start(Test6505027.class);
  68     }
  69 
  70     private final JTable table = new JTable(new DefaultTableModel(COLUMNS, 2));
  71 
  72     public Test6505027(JFrame main) {
  73         Container container = main;
  74         if (INTERNAL) {
  75             JInternalFrame frame = new JInternalFrame();
  76             frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT);
  77             frame.setVisible(true);
  78 
  79             JDesktopPane desktop = new JDesktopPane();
  80             desktop.add(frame, new Integer(1));
  81 
  82             container.add(desktop);
  83             container = frame;
  84         }
  85         if (TERMINATE) {
  86             this.table.putClientProperty(KEY, Boolean.TRUE);
  87         }
  88         TableColumn column = this.table.getColumn(COLUMNS[1]);
  89         column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS)));
  90 
  91         container.add(BorderLayout.NORTH, new JTextField());
  92         container.add(BorderLayout.CENTER, new JScrollPane(this.table));
  93     }
  94 
  95     public void press() throws AWTException {
  96         Point point = this.table.getCellRect(1, 1, false).getLocation();
  97         SwingUtilities.convertPointToScreen(point, this.table);
  98 
  99         Robot robot = new Robot();
 100         robot.setAutoDelay(50);
 101         robot.mouseMove(point.x + 1, point.y + 1);
 102         robot.mousePress(InputEvent.BUTTON1_MASK);
 103         robot.mouseRelease(InputEvent.BUTTON1_MASK);
 104     }
 105 
 106     public static void validate() {
 107         Component component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
 108         if (!component.getClass().equals(JComboBox.class)) {
 109             throw new Error("unexpected focus owner: " + component);
 110         }
 111     }
 112 }