|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, 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 | +/* @test |
| 25 | + * @key headful |
| 26 | + * @bug 6817009 |
| 27 | + * @summary Verifies that Action.SELECTED_KEY is not toggled when key binding is used |
| 28 | + @run main TestSelectedKey |
| 29 | + */ |
| 30 | + |
| 31 | +import java.awt.BorderLayout; |
| 32 | +import java.awt.Dimension; |
| 33 | +import java.awt.EventQueue; |
| 34 | +import java.awt.event.ActionEvent; |
| 35 | +import java.awt.event.KeyEvent; |
| 36 | +import java.beans.PropertyChangeEvent; |
| 37 | +import java.beans.PropertyChangeListener; |
| 38 | +import java.awt.Robot; |
| 39 | + |
| 40 | +import javax.swing.SwingUtilities; |
| 41 | +import javax.swing.JLabel; |
| 42 | +import javax.swing.AbstractAction; |
| 43 | +import javax.swing.Action; |
| 44 | +import javax.swing.Box; |
| 45 | +import javax.swing.BoxLayout; |
| 46 | +import javax.swing.KeyStroke; |
| 47 | +import javax.swing.JToolBar; |
| 48 | +import javax.swing.JToggleButton; |
| 49 | +import javax.swing.JPopupMenu; |
| 50 | +import javax.swing.JCheckBoxMenuItem; |
| 51 | +import javax.swing.JFrame; |
| 52 | +import javax.swing.InputMap; |
| 53 | +import javax.swing.JComponent; |
| 54 | +import javax.swing.ActionMap; |
| 55 | + |
| 56 | +public class TestSelectedKey { |
| 57 | + private static Robot robot; |
| 58 | + private static JFrame frame; |
| 59 | + private static boolean toggled = false; |
| 60 | + |
| 61 | + public static void main(String[] args) throws Exception { |
| 62 | + robot = new Robot(); |
| 63 | + try { |
| 64 | + SwingUtilities.invokeAndWait(() -> createGUI()); |
| 65 | + robot.waitForIdle(); |
| 66 | + robot.delay(1000); |
| 67 | + robot.keyPress(KeyEvent.VK_CONTROL); |
| 68 | + robot.keyPress(KeyEvent.VK_Z); |
| 69 | + robot.keyRelease(KeyEvent.VK_Z); |
| 70 | + robot.keyRelease(KeyEvent.VK_CONTROL); |
| 71 | + robot.waitForIdle(); |
| 72 | + robot.delay(1000); |
| 73 | + if (!toggled) { |
| 74 | + throw new RuntimeException("JToggleButton not toggled via accelerator key"); |
| 75 | + } |
| 76 | + } finally { |
| 77 | + SwingUtilities.invokeAndWait(() -> { |
| 78 | + if (frame != null) { |
| 79 | + frame.dispose(); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private static void createGUI() { |
| 86 | + final JLabel label = new JLabel("Toggle me actions 0"); |
| 87 | + final JLabel selected = new JLabel("Toggle me selected: false"); |
| 88 | + AbstractAction action = new AbstractAction("Toggle me") { |
| 89 | + private int count = 0; |
| 90 | + @Override |
| 91 | + public void actionPerformed(ActionEvent e) { |
| 92 | + label.setText("Toggle me actions " + (++count)); |
| 93 | + } |
| 94 | + }; |
| 95 | + action.addPropertyChangeListener(new PropertyChangeListener() { |
| 96 | + public void propertyChange(PropertyChangeEvent evt) { |
| 97 | + if (Action.SELECTED_KEY.equals(evt.getPropertyName())) { |
| 98 | + selected.setText("Toggle me selected: " + evt.getNewValue()); |
| 99 | + toggled = (boolean)evt.getNewValue(); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + action.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK)); |
| 104 | + // need to set manually or won't be updated by Swing |
| 105 | + action.putValue(Action.SELECTED_KEY, Boolean.FALSE); |
| 106 | + |
| 107 | + Box labels = new Box(BoxLayout.PAGE_AXIS); |
| 108 | + labels.add(label); |
| 109 | + labels.add(selected); |
| 110 | + |
| 111 | + JToolBar toolBar = new JToolBar(); |
| 112 | + toolBar.add(new JToggleButton(action)); |
| 113 | + |
| 114 | + JPopupMenu popupMenu = new JPopupMenu(); |
| 115 | + popupMenu.add(new JCheckBoxMenuItem(action)); |
| 116 | + labels.setComponentPopupMenu(popupMenu); |
| 117 | + |
| 118 | + frame = new JFrame("Test Action.SELECTED_KEY"); |
| 119 | + frame.getContentPane().add(toolBar, BorderLayout.PAGE_START); |
| 120 | + frame.getContentPane().add(labels, BorderLayout.CENTER); |
| 121 | + |
| 122 | + InputMap inputMap = frame.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); |
| 123 | + ActionMap actionMap = frame.getRootPane().getActionMap(); |
| 124 | + inputMap.put((KeyStroke) action.getValue(Action.ACCELERATOR_KEY), "toggleAction"); |
| 125 | + actionMap.put("toggleAction", action); |
| 126 | + |
| 127 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 128 | + frame.setPreferredSize(new Dimension(400, 200)); |
| 129 | + frame.pack(); |
| 130 | + frame.setLocationRelativeTo(null); |
| 131 | + frame.setVisible(true); |
| 132 | + } |
| 133 | +} |
0 commit comments