6607130: REGRESSION: JComboBox cell editor isn't hidden if the same value is selected with keyboard

JComboBox cell editor now hides if the same value is selected with keyboard

Reviewed-by: peterz, alexp
diff --git a/jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java b/jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
index f073eed..c49387f 100644
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicComboBoxUI.java
@@ -1509,15 +1509,21 @@
                             || ui.isTableCellEditor) {
                         Object listItem = ui.popup.getList().getSelectedValue();
                         if (listItem != null) {
-                            comboBox.getModel().setSelectedItem(listItem);
-                            // Ensure that JComboBox.actionPerformed()
-                            // doesn't set editor value as selected item
+                            // Use the selected value from popup
+                            // to set the selected item in combo box,
+                            // but ensure before that JComboBox.actionPerformed()
+                            // won't use editor's value to set the selected item
                             comboBox.getEditor().setItem(listItem);
+                            comboBox.setSelectedItem(listItem);
                         }
                     }
                     comboBox.setPopupVisible(false);
                 }
                 else {
+                    // Hide combo box if it is a table cell editor
+                    if (ui.isTableCellEditor && !comboBox.isEditable()) {
+                        comboBox.setSelectedItem(comboBox.getSelectedItem());
+                    }
                     // Call the default button binding.
                     // This is a pretty messy way of passing an event through
                     // to the root pane.
diff --git a/jdk/test/javax/swing/JComboBox/6607130/bug6607130.java b/jdk/test/javax/swing/JComboBox/6607130/bug6607130.java
new file mode 100644
index 0000000..5d465c9
--- /dev/null
+++ b/jdk/test/javax/swing/JComboBox/6607130/bug6607130.java
@@ -0,0 +1,149 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/* @test
+ * @bug 6607130
+ * @summary Checks that JComboBox cell editor is hidden if the same
+ *          item is selected with keyboard.
+ *          Also checks that JComboBox cell editor is hidden if F2 and then
+ *          ENTER were pressed.
+ * @author Mikhail Lapshin
+ */
+
+import sun.awt.SunToolkit;
+
+import javax.swing.*;
+import javax.swing.table.DefaultTableModel;
+import java.awt.*;
+import java.awt.event.KeyEvent;
+
+public class bug6607130 {
+    private JFrame frame;
+    private JComboBox cb;
+    private Robot robot;
+
+    public static void main(String[] args) throws Exception {
+        final bug6607130 test = new bug6607130();
+        try {
+            SwingUtilities.invokeAndWait(new Runnable() {
+                public void run() {
+                    test.setupUI();
+                }
+            });
+            test.test();
+        } finally {
+            if (test.frame != null) {
+                test.frame.dispose();
+            }
+        }
+    }
+
+    public bug6607130() throws AWTException {
+        robot = new Robot();
+    }
+
+    private void setupUI() {
+        frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+        DefaultTableModel model = new DefaultTableModel(1, 1);
+        JTable table = new JTable(model);
+
+        cb = new JComboBox(new String[]{"one", "two", "three"});
+        table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(cb));
+        frame.add(table);
+
+        frame.pack();
+        frame.setLocationRelativeTo(null);
+        frame.setVisible(true);
+    }
+
+    private void test() throws Exception {
+        realSync();
+        test1();
+        realSync();
+        checkResult("First test");
+        test2();
+        realSync();
+        checkResult("Second test");
+    }
+
+    private void test1() throws Exception {
+        // Select 'one'
+        hitKey(KeyEvent.VK_TAB);
+        realSync();
+        hitKey(KeyEvent.VK_F2);
+        realSync();
+        hitKey(KeyEvent.VK_DOWN);
+        realSync();
+        hitKey(KeyEvent.VK_DOWN);
+        realSync();
+        hitKey(KeyEvent.VK_ENTER);
+        realSync();
+
+        // Select 'one' again
+        hitKey(KeyEvent.VK_F2);
+        realSync();
+        hitKey(KeyEvent.VK_DOWN);
+        realSync();
+        hitKey(KeyEvent.VK_ENTER);
+        realSync();
+    }
+
+    private void test2() throws Exception {
+        // Press F2 and then press ENTER
+        // Editor should be shown and then closed
+        hitKey(KeyEvent.VK_F2);
+        realSync();
+        hitKey(KeyEvent.VK_ENTER);
+        realSync();
+    }
+
+    private void checkResult(String testName) {
+        if (!cb.isShowing()) {
+            System.out.println(testName + " passed");
+        } else {
+            System.out.println(testName + " failed");
+            throw new RuntimeException("JComboBox is showing " +
+                    "after item selection.");
+        }
+    }
+
+    private static void realSync() {
+        ((SunToolkit) (Toolkit.getDefaultToolkit())).realSync();
+    }
+
+    public void hitKey(int keycode) {
+        robot.keyPress(keycode);
+        robot.keyRelease(keycode);
+        delay();
+    }
+
+    private void delay() {
+        try {
+            Thread.sleep(1000);
+        } catch(InterruptedException ie) {
+            ie.printStackTrace();
+        }
+    }
+}