608N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
608N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
608N/A *
608N/A * This code is free software; you can redistribute it and/or modify it
608N/A * under the terms of the GNU General Public License version 2 only, as
608N/A * published by the Free Software Foundation.
608N/A *
608N/A * This code is distributed in the hope that it will be useful, but WITHOUT
608N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
608N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
608N/A * version 2 for more details (a copy is included in the LICENSE file that
608N/A * accompanied this code).
608N/A *
608N/A * You should have received a copy of the GNU General Public License version
608N/A * 2 along with this work; if not, write to the Free Software Foundation,
608N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
608N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
608N/A */
608N/A
608N/A/* @test
608N/A * @bug 6607130
608N/A * @summary Checks that JComboBox cell editor is hidden if the same
608N/A * item is selected with keyboard.
608N/A * Also checks that JComboBox cell editor is hidden if F2 and then
608N/A * ENTER were pressed.
608N/A * @author Mikhail Lapshin
608N/A */
608N/A
608N/Aimport sun.awt.SunToolkit;
608N/A
608N/Aimport javax.swing.*;
608N/Aimport javax.swing.table.DefaultTableModel;
608N/Aimport java.awt.*;
608N/Aimport java.awt.event.KeyEvent;
608N/A
608N/Apublic class bug6607130 {
608N/A private JFrame frame;
608N/A private JComboBox cb;
608N/A private Robot robot;
608N/A
608N/A public static void main(String[] args) throws Exception {
608N/A final bug6607130 test = new bug6607130();
608N/A try {
608N/A SwingUtilities.invokeAndWait(new Runnable() {
608N/A public void run() {
608N/A test.setupUI();
608N/A }
608N/A });
608N/A test.test();
608N/A } finally {
608N/A if (test.frame != null) {
608N/A test.frame.dispose();
608N/A }
608N/A }
608N/A }
608N/A
608N/A public bug6607130() throws AWTException {
608N/A robot = new Robot();
608N/A }
608N/A
608N/A private void setupUI() {
608N/A frame = new JFrame();
608N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
608N/A
608N/A DefaultTableModel model = new DefaultTableModel(1, 1);
608N/A JTable table = new JTable(model);
608N/A
608N/A cb = new JComboBox(new String[]{"one", "two", "three"});
608N/A table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));
608N/A frame.add(table);
608N/A
608N/A frame.pack();
608N/A frame.setLocationRelativeTo(null);
608N/A frame.setVisible(true);
608N/A }
608N/A
608N/A private void test() throws Exception {
608N/A realSync();
608N/A test1();
608N/A realSync();
608N/A checkResult("First test");
608N/A test2();
608N/A realSync();
608N/A checkResult("Second test");
608N/A }
608N/A
608N/A private void test1() throws Exception {
608N/A // Select 'one'
608N/A hitKey(KeyEvent.VK_TAB);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_F2);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_DOWN);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_DOWN);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_ENTER);
608N/A realSync();
608N/A
608N/A // Select 'one' again
608N/A hitKey(KeyEvent.VK_F2);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_DOWN);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_ENTER);
608N/A realSync();
608N/A }
608N/A
608N/A private void test2() throws Exception {
608N/A // Press F2 and then press ENTER
608N/A // Editor should be shown and then closed
608N/A hitKey(KeyEvent.VK_F2);
608N/A realSync();
608N/A hitKey(KeyEvent.VK_ENTER);
608N/A realSync();
608N/A }
608N/A
608N/A private void checkResult(String testName) {
608N/A if (!cb.isShowing()) {
608N/A System.out.println(testName + " passed");
608N/A } else {
608N/A System.out.println(testName + " failed");
608N/A throw new RuntimeException("JComboBox is showing " +
608N/A "after item selection.");
608N/A }
608N/A }
608N/A
608N/A private static void realSync() {
608N/A ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
608N/A }
608N/A
608N/A public void hitKey(int keycode) {
608N/A robot.keyPress(keycode);
608N/A robot.keyRelease(keycode);
608N/A delay();
608N/A }
608N/A
608N/A private void delay() {
608N/A try {
608N/A Thread.sleep(1000);
608N/A } catch(InterruptedException ie) {
608N/A ie.printStackTrace();
608N/A }
608N/A }
608N/A}