|
| 1 | +/* |
| 2 | + * Copyright (c) 2011, 2025, 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 | +* @bug 6513492 |
| 27 | +* @summary Escape key needs to be pressed twice to remove focus from an empty/diabled Menu. |
| 28 | +* @library /java/awt/regtesthelpers |
| 29 | +* @build PassFailJFrame |
| 30 | +* @run main/manual bug6513492 |
| 31 | +*/ |
| 32 | + |
| 33 | +import java.awt.event.ActionEvent; |
| 34 | +import java.awt.event.ActionListener; |
| 35 | + |
| 36 | +import javax.swing.JFrame; |
| 37 | +import javax.swing.JMenu; |
| 38 | +import javax.swing.JMenuBar; |
| 39 | +import javax.swing.JMenuItem; |
| 40 | +import javax.swing.JTextArea; |
| 41 | +import javax.swing.SwingUtilities; |
| 42 | +import javax.swing.UIManager; |
| 43 | + |
| 44 | +public class bug6513492 { |
| 45 | + |
| 46 | + private static final String INSTRUCTIONS = """ |
| 47 | + Test Menu for different LaF: |
| 48 | +
|
| 49 | + * For Windows Laf: |
| 50 | + Click the editor |
| 51 | + Click EmpyMenu, press Escape -> focus must go to the editor |
| 52 | + Click EmpyMenu, press right arrow button, press Escape -> focus must go to the editor |
| 53 | + Click SubMenuTest, highlight the first disabled submenu, press Escape |
| 54 | + -> focus must stay at the topLevelMenu |
| 55 | +
|
| 56 | + * For Metal, Nimbus and Aqua Laf |
| 57 | + Click the editor |
| 58 | + Click SubMenuTest, highlight the EmptySubmenu, press Escape -> focus must go to the editor |
| 59 | + Click SubMenuTest, highlight the EnabledItem, press Escape -> focus must go to the editor |
| 60 | +
|
| 61 | + * For GTK and Motif |
| 62 | + Click the editor |
| 63 | + Open any menu or submenu, press Escape -> focus must go to the editor."""; |
| 64 | + |
| 65 | + public static void main(String[] args) throws Exception { |
| 66 | + PassFailJFrame.builder() |
| 67 | + .title("bug6513492 Instructions") |
| 68 | + .instructions(INSTRUCTIONS) |
| 69 | + .columns(35) |
| 70 | + .testUI(bug6513492::createTestUI) |
| 71 | + .logArea() |
| 72 | + .build() |
| 73 | + .awaitAndCheck(); |
| 74 | + } |
| 75 | + |
| 76 | + private static JFrame createTestUI() { |
| 77 | + PassFailJFrame.log("Menu.cancelMode = " + |
| 78 | + UIManager.getString("Menu.cancelMode")); |
| 79 | + PassFailJFrame.log("Menu.preserveTopLevelSelection = " + |
| 80 | + UIManager.getBoolean("Menu.preserveTopLevelSelection")); |
| 81 | + PassFailJFrame.log(""); |
| 82 | + |
| 83 | + JFrame frame = new JFrame("bug6513492"); |
| 84 | + JMenuBar bar = new JMenuBar(); |
| 85 | + bar.add(new JMenu("EmptyMenu")); |
| 86 | + |
| 87 | + JMenu disabledMenu = new JMenu("NotEmpyButDisabled"); |
| 88 | + disabledMenu.add(new JMenuItem("item")); |
| 89 | + disabledMenu.setEnabled(false); |
| 90 | + bar.add(disabledMenu); |
| 91 | + |
| 92 | + JMenu menu = new JMenu("SubMenuTest"); |
| 93 | + JMenu disabledSubmenu = new JMenu("Submenu"); |
| 94 | + disabledSubmenu.add(new JMenuItem("item")); |
| 95 | + disabledSubmenu.setEnabled(false); |
| 96 | + menu.add(disabledSubmenu); |
| 97 | + |
| 98 | + JMenu enabledSubmenu = new JMenu("Submenu"); |
| 99 | + enabledSubmenu.add(new JMenuItem("item")); |
| 100 | + menu.add(enabledSubmenu); |
| 101 | + |
| 102 | + JMenu emptySubmenu = new JMenu("EmptySubmenu"); |
| 103 | + menu.add(emptySubmenu); |
| 104 | + |
| 105 | + menu.add(new JMenuItem("EnabledItem")); |
| 106 | + JMenuItem item = new JMenuItem("DisabledItem"); |
| 107 | + item.setEnabled(false); |
| 108 | + menu.add(item); |
| 109 | + bar.add(menu); |
| 110 | + |
| 111 | + JMenu lafMenu = new JMenu("Change LaF"); |
| 112 | + |
| 113 | + UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels(); |
| 114 | + for (final UIManager.LookAndFeelInfo lafInfo : lafs) { |
| 115 | + JMenuItem lafItem = new JMenuItem(lafInfo.getName()); |
| 116 | + lafItem.addActionListener(new ActionListener() { |
| 117 | + public void actionPerformed(ActionEvent e) { |
| 118 | + setLaf(frame, lafInfo.getClassName()); |
| 119 | + } |
| 120 | + }); |
| 121 | + lafMenu.add(lafItem); |
| 122 | + } |
| 123 | + |
| 124 | + frame.setJMenuBar(bar); |
| 125 | + bar.add(menu); |
| 126 | + bar.add(lafMenu); |
| 127 | + |
| 128 | + JTextArea field = new JTextArea("The editor"); |
| 129 | + frame.add(field); |
| 130 | + field.requestFocusInWindow(); |
| 131 | + frame.pack(); |
| 132 | + return frame; |
| 133 | + } |
| 134 | + |
| 135 | + private static void setLaf(JFrame frame, String laf) { |
| 136 | + try { |
| 137 | + UIManager.setLookAndFeel(laf); |
| 138 | + SwingUtilities.updateComponentTreeUI(frame); |
| 139 | + PassFailJFrame.log("Menu.cancelMode = " + |
| 140 | + UIManager.getString("Menu.cancelMode")); |
| 141 | + PassFailJFrame.log("Menu.preserveTopLevelSelection = " + |
| 142 | + UIManager.getBoolean("Menu.preserveTopLevelSelection")); |
| 143 | + PassFailJFrame.log(""); |
| 144 | + } catch (Exception e) { |
| 145 | + throw new RuntimeException(e); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments