|
| 1 | +/* |
| 2 | + * Copyright (c) 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 | +import java.awt.BorderLayout; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.event.InputEvent; |
| 27 | +import java.awt.event.KeyEvent; |
| 28 | + |
| 29 | +import javax.swing.Box; |
| 30 | +import javax.swing.JFrame; |
| 31 | +import javax.swing.JLabel; |
| 32 | +import javax.swing.JMenu; |
| 33 | +import javax.swing.JMenuBar; |
| 34 | +import javax.swing.JMenuItem; |
| 35 | +import javax.swing.JPanel; |
| 36 | +import javax.swing.KeyStroke; |
| 37 | +import javax.swing.UIManager; |
| 38 | + |
| 39 | +import static javax.swing.BorderFactory.createEmptyBorder; |
| 40 | + |
| 41 | +/* |
| 42 | + * @test id=windows |
| 43 | + * @bug 8348760 8365375 8365389 8365625 |
| 44 | + * @requires (os.family == "windows") |
| 45 | + * @summary Verify that Windows Look & Feel allows changing |
| 46 | + * accelerator colors |
| 47 | + * @library /java/awt/regtesthelpers |
| 48 | + * @build PassFailJFrame |
| 49 | + * @run main/manual MenuItemAcceleratorColor |
| 50 | + */ |
| 51 | + |
| 52 | +/* |
| 53 | + * @test id=classic |
| 54 | + * @bug 8348760 8365375 8365389 8365625 |
| 55 | + * @requires (os.family == "windows") |
| 56 | + * @summary Verify that Windows Classic Look & Feel allows changing |
| 57 | + * accelerator colors |
| 58 | + * @library /java/awt/regtesthelpers |
| 59 | + * @build PassFailJFrame |
| 60 | + * @run main/manual MenuItemAcceleratorColor classic |
| 61 | + */ |
| 62 | +public final class MenuItemAcceleratorColor { |
| 63 | + private static final String INSTRUCTIONS = |
| 64 | + "Click the Menu to open it.\n" + |
| 65 | + "\n" + |
| 66 | + "Verify that the first and the last menu items render " + |
| 67 | + "their accelerators using the default colors, the color " + |
| 68 | + "should match that of the menu item itself in regular and " + |
| 69 | + "selected states.\n" + |
| 70 | + "\n" + |
| 71 | + "Verify that the second menu item renders its accelerator " + |
| 72 | + "with green and that the color changes to red when selected.\n" + |
| 73 | + "\n" + |
| 74 | + "Verify that the third menu item renders its accelerator " + |
| 75 | + "with magenta and yellow correspondingly.\n" + |
| 76 | + "\n" + |
| 77 | + "Verify that only the fifth menu item renders its accelerator " + |
| 78 | + "with blue; both the fourth and sixth should render their " + |
| 79 | + "accelerator with a shade of gray.\n" + |
| 80 | + "\n" + |
| 81 | + "If the above conditions are satisfied, press the Pass button; " + |
| 82 | + "otherwise, press the Fail button."; |
| 83 | + |
| 84 | + public static void main(String[] args) throws Exception { |
| 85 | + UIManager.setLookAndFeel((args.length > 0 && "classic".equals(args[0])) |
| 86 | + ? "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel" |
| 87 | + : "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); |
| 88 | + |
| 89 | + PassFailJFrame.builder() |
| 90 | + .instructions(INSTRUCTIONS) |
| 91 | + .rows(20) |
| 92 | + .columns(60) |
| 93 | + .testUI(MenuItemAcceleratorColor::createUI) |
| 94 | + .build() |
| 95 | + .awaitAndCheck(); |
| 96 | + } |
| 97 | + |
| 98 | + private static Box createInfoPanel() { |
| 99 | + Box box = Box.createVerticalBox(); |
| 100 | + box.add(new JLabel("Look and Feel: " |
| 101 | + + UIManager.getLookAndFeel() |
| 102 | + .getName())); |
| 103 | + box.add(new JLabel("Java version: " |
| 104 | + + System.getProperty("java.runtime.version"))); |
| 105 | + return box; |
| 106 | + } |
| 107 | + |
| 108 | + private static JFrame createUI() { |
| 109 | + JPanel content = new JPanel(new BorderLayout()); |
| 110 | + content.setBorder(createEmptyBorder(8, 8, 8, 8)); |
| 111 | + content.add(createInfoPanel(), |
| 112 | + BorderLayout.SOUTH); |
| 113 | + |
| 114 | + JFrame frame = new JFrame("Accelerator colors in Windows L&F"); |
| 115 | + frame.setJMenuBar(createMenuBar()); |
| 116 | + frame.add(content, BorderLayout.CENTER); |
| 117 | + frame.setSize(350, 370); |
| 118 | + return frame; |
| 119 | + } |
| 120 | + |
| 121 | + private static JMenuBar createMenuBar() { |
| 122 | + JMenuItem first = new JMenuItem("First menu item"); |
| 123 | + first.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, |
| 124 | + InputEvent.CTRL_DOWN_MASK)); |
| 125 | + |
| 126 | + // Modify colors for accelerator rendering |
| 127 | + Color acceleratorForeground = UIManager.getColor("MenuItem.acceleratorForeground"); |
| 128 | + Color acceleratorSelectionForeground = UIManager.getColor("MenuItem.acceleratorSelectionForeground"); |
| 129 | + UIManager.put("MenuItem.acceleratorForeground", Color.GREEN); |
| 130 | + UIManager.put("MenuItem.acceleratorSelectionForeground", Color.RED); |
| 131 | + |
| 132 | + JMenuItem second = new JMenuItem("Second menu item"); |
| 133 | + second.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, |
| 134 | + InputEvent.SHIFT_DOWN_MASK |
| 135 | + | InputEvent.CTRL_DOWN_MASK)); |
| 136 | + |
| 137 | + UIManager.put("MenuItem.acceleratorForeground", Color.MAGENTA); |
| 138 | + UIManager.put("MenuItem.acceleratorSelectionForeground", Color.YELLOW); |
| 139 | + JMenuItem third = new JMenuItem("Third menu item"); |
| 140 | + third.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, |
| 141 | + InputEvent.ALT_DOWN_MASK)); |
| 142 | + |
| 143 | + // Restore colors |
| 144 | + UIManager.put("MenuItem.acceleratorForeground", acceleratorForeground); |
| 145 | + UIManager.put("MenuItem.acceleratorSelectionForeground", acceleratorSelectionForeground); |
| 146 | + |
| 147 | + |
| 148 | + // Disabled foreground |
| 149 | + JMenuItem fourth = new JMenuItem("Fourth menu item"); |
| 150 | + fourth.setEnabled(false); |
| 151 | + fourth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, |
| 152 | + InputEvent.CTRL_DOWN_MASK)); |
| 153 | + |
| 154 | + Color disabledForeground = UIManager.getColor("MenuItem.disabledForeground"); |
| 155 | + UIManager.put("MenuItem.disabledForeground", Color.BLUE); |
| 156 | + |
| 157 | + JMenuItem fifth = new JMenuItem("Fifth menu item"); |
| 158 | + fifth.setEnabled(false); |
| 159 | + fifth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, |
| 160 | + InputEvent.CTRL_DOWN_MASK |
| 161 | + | InputEvent.SHIFT_DOWN_MASK)); |
| 162 | + |
| 163 | + // Restore disabled foreground |
| 164 | + UIManager.put("MenuItem.disabledForeground", disabledForeground); |
| 165 | + |
| 166 | + JMenuItem sixth = new JMenuItem("Sixth menu item"); |
| 167 | + sixth.setEnabled(false); |
| 168 | + sixth.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, |
| 169 | + InputEvent.CTRL_DOWN_MASK |
| 170 | + | InputEvent.ALT_DOWN_MASK)); |
| 171 | + |
| 172 | + |
| 173 | + JMenuItem quit = new JMenuItem("Quit"); |
| 174 | + quit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, |
| 175 | + InputEvent.CTRL_DOWN_MASK)); |
| 176 | + |
| 177 | + JMenu menu = new JMenu("Menu"); |
| 178 | + menu.add(first); |
| 179 | + menu.add(second); |
| 180 | + menu.add(third); |
| 181 | + menu.addSeparator(); |
| 182 | + menu.add(fourth); |
| 183 | + menu.add(fifth); |
| 184 | + menu.add(sixth); |
| 185 | + menu.addSeparator(); |
| 186 | + menu.add(quit); |
| 187 | + |
| 188 | + JMenuBar menuBar = new JMenuBar(); |
| 189 | + menuBar.add(menu); |
| 190 | + |
| 191 | + return menuBar; |
| 192 | + } |
| 193 | +} |
0 commit comments