From c42ef70a453690ecdccc570161fdfda58318ecc6 Mon Sep 17 00:00:00 2001 From: Prasanta Sadhukhan Date: Wed, 28 Sep 2022 08:50:58 +0000 Subject: [PATCH] 7148092: [macosx] When Alt+down arrow key is pressed, the combobox popup does not appear. Reviewed-by: prr, serb --- .../classes/com/apple/laf/AquaComboBoxUI.java | 23 ++++ .../com/apple/laf/AquaKeyBindings.java | 4 + .../plaf/aqua/TestAltUpDownComboBox.java | 101 ++++++++++++++++++ 3 files changed, 128 insertions(+) create mode 100644 test/jdk/javax/swing/plaf/aqua/TestAltUpDownComboBox.java diff --git a/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxUI.java b/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxUI.java index 3090c4716a8..dcef8606abb 100644 --- a/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxUI.java +++ b/src/java.desktop/macosx/classes/com/apple/laf/AquaComboBoxUI.java @@ -323,6 +323,8 @@ public class AquaComboBoxUI extends BasicComboBoxUI implements Sizeable { actionMap.put("aquaSelectPageDown", highlightPageDownAction); actionMap.put("aquaHidePopup", hideAction); + actionMap.put("aquaOpenPopupOrhighlightLast", openPopupOrHighlightLast); + actionMap.put("aquaOpenPopupOrhighlightFirst", openPopupOrHighlightFirst); SwingUtilities.replaceUIActionMap(comboBox, actionMap); } @@ -604,6 +606,27 @@ public class AquaComboBoxUI extends BasicComboBoxUI implements Sizeable { } }; + @SuppressWarnings("serial") // anonymous class + private final Action openPopupOrHighlightLast = new ComboBoxAction() { + @Override + void performComboBoxAction(final AquaComboBoxUI ui) { + final int size = listBox.getModel().getSize(); + listBox.setSelectedIndex(size - 1); + listBox.ensureIndexIsVisible(size - 1); + comboBox.setSelectedIndex(ui.getPopup().getList().getSelectedIndex()); + } + }; + + @SuppressWarnings("serial") // anonymous class + private final Action openPopupOrHighlightFirst = new ComboBoxAction() { + @Override + void performComboBoxAction(final AquaComboBoxUI ui) { + listBox.setSelectedIndex(0); + listBox.ensureIndexIsVisible(0); + comboBox.setSelectedIndex(ui.getPopup().getList().getSelectedIndex()); + } + }; + public void applySizeFor(final JComponent c, final Size size) { if (arrowButton == null) return; final Border border = arrowButton.getBorder(); diff --git a/src/java.desktop/macosx/classes/com/apple/laf/AquaKeyBindings.java b/src/java.desktop/macosx/classes/com/apple/laf/AquaKeyBindings.java index 5b9320c2a35..52572643511 100644 --- a/src/java.desktop/macosx/classes/com/apple/laf/AquaKeyBindings.java +++ b/src/java.desktop/macosx/classes/com/apple/laf/AquaKeyBindings.java @@ -222,6 +222,10 @@ public class AquaKeyBindings { "KP_UP", "aquaSelectPrevious", "DOWN", "aquaSelectNext", "KP_DOWN", "aquaSelectNext", + "alt DOWN", "aquaOpenPopupOrhighlightLast", + "alt KP_DOWN", "aquaOpenPopupOrhighlightLast", + "alt UP", "aquaOpenPopupOrhighlightFirst", + "alt KP_UP", "aquaOpenPopupOrhighlightFirst", "SPACE", "aquaSpacePressed" // "spacePopup" })); } diff --git a/test/jdk/javax/swing/plaf/aqua/TestAltUpDownComboBox.java b/test/jdk/javax/swing/plaf/aqua/TestAltUpDownComboBox.java new file mode 100644 index 00000000000..9093757db27 --- /dev/null +++ b/test/jdk/javax/swing/plaf/aqua/TestAltUpDownComboBox.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/* @test + * @bug 7148092 + * @requires (os.family == "mac") + * @summary Tests that alt+down arrow pulls down JComboBox popup + * @key headful + * @run main TestAltUpDownComboBox +*/ + +import java.awt.Container; +import java.awt.Robot; +import java.awt.event.KeyEvent; +import javax.swing.BoxLayout; +import javax.swing.JFrame; +import javax.swing.JComboBox; +import javax.swing.SwingUtilities; + +public class TestAltUpDownComboBox { + + private static JFrame frame; + private static JComboBox combo; + + public static void main(String[] argv) throws Exception { + Robot robot = new Robot(); + robot.setAutoDelay(100); + try { + SwingUtilities.invokeAndWait(() -> { + frame = new JFrame(""); + Object[] fruits = {"Banana", "Pear", "Apple"}; + combo = new JComboBox(fruits); + Container pane = frame.getContentPane(); + pane.setLayout(new BoxLayout(pane, BoxLayout.X_AXIS)); + pane.add(combo); + + frame.pack(); + frame.setLocationRelativeTo(null); + frame.setVisible(true); + }); + robot.waitForIdle(); + robot.delay(1000); + robot.keyPress(KeyEvent.VK_ALT); + robot.keyPress(KeyEvent.VK_DOWN); + robot.keyRelease(KeyEvent.VK_DOWN); + robot.keyRelease(KeyEvent.VK_ALT); + robot.delay(1000); + + if (!combo.isPopupVisible()) { + throw new RuntimeException("comboBox is not visible"); + } + + robot.keyPress(KeyEvent.VK_ALT); + robot.keyPress(KeyEvent.VK_DOWN); + robot.keyRelease(KeyEvent.VK_DOWN); + robot.keyRelease(KeyEvent.VK_ALT); + robot.delay(1000); + + if (combo.getSelectedIndex() != combo.getItemCount() - 1) { + System.out.println(combo.getSelectedIndex()); + throw new RuntimeException("Alt+Down did not select last entry"); + } + + robot.keyPress(KeyEvent.VK_ALT); + robot.keyPress(KeyEvent.VK_UP); + robot.keyRelease(KeyEvent.VK_UP); + robot.keyRelease(KeyEvent.VK_ALT); + robot.delay(1000); + + if (combo.getSelectedIndex() != 0) { + System.out.println(combo.getSelectedIndex()); + throw new RuntimeException("Alt+Up did not select first entry"); + } + } finally { + SwingUtilities.invokeAndWait(() -> { + if (frame != null) { + frame.dispose(); + } + }); + } + } +}