|
| 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6726690 |
| 27 | + * @summary Verifies SwingUtilities.replaceUI*Map() methods remove |
| 28 | + * previously installed maps |
| 29 | + * @run main UIMapTest |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.event.ActionEvent; |
| 33 | +import java.awt.event.KeyEvent; |
| 34 | +import javax.swing.AbstractAction; |
| 35 | +import javax.swing.Action; |
| 36 | +import javax.swing.ActionMap; |
| 37 | +import javax.swing.ComponentInputMap; |
| 38 | +import javax.swing.InputMap; |
| 39 | +import javax.swing.JButton; |
| 40 | +import javax.swing.JComponent; |
| 41 | +import javax.swing.JFrame; |
| 42 | +import javax.swing.KeyStroke; |
| 43 | +import javax.swing.SwingUtilities; |
| 44 | + |
| 45 | +public class UIMapTest { |
| 46 | + |
| 47 | + public static void main(String[] args) { |
| 48 | + |
| 49 | + StringBuilder str = new StringBuilder(); |
| 50 | + |
| 51 | + // Create the test button |
| 52 | + JButton button = new JButton("Test"); |
| 53 | + |
| 54 | + // Create an input map that maps ENTER to the button |
| 55 | + ComponentInputMap map = new ComponentInputMap(button); |
| 56 | + map.put(KeyStroke.getKeyStroke("pressed ENTER"), "pressed"); |
| 57 | + map.put(KeyStroke.getKeyStroke("released ENTER"), "released"); |
| 58 | + |
| 59 | + // Add the map |
| 60 | + SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, map); |
| 61 | + |
| 62 | + // Attempt to remove the map |
| 63 | + SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, null); |
| 64 | + |
| 65 | + if (button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW). |
| 66 | + get(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) != null) { |
| 67 | + str.append("\nSwingUtilities.replaceUIInputMap " + |
| 68 | + "didn't remove previously installed input map"); |
| 69 | + } |
| 70 | + |
| 71 | + // Get the InputMap for the button when it has focus |
| 72 | + InputMap inputMap = button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); |
| 73 | + |
| 74 | + // Map the VK_ENTER key stroke to a specific action name |
| 75 | + inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "doEnterAction"); |
| 76 | + Action enterAction = new AbstractAction() { |
| 77 | + @Override |
| 78 | + public void actionPerformed(ActionEvent e) { } |
| 79 | + }; |
| 80 | + button.getActionMap().put("doEnterAction", enterAction); |
| 81 | + SwingUtilities.replaceUIActionMap(button, null); |
| 82 | + if (button.getActionMap().size() != 0) { |
| 83 | + str.append("\nSwingUtilities.replaceUIActionMap " + |
| 84 | + "didn't remove previously installed action map"); |
| 85 | + } |
| 86 | + if (str.length() != 0) { |
| 87 | + throw new RuntimeException(str.toString()); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments