1 /* 2 * Copyright (c) 2015, 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.Robot; 25 import java.awt.event.ActionEvent; 26 import java.awt.event.KeyEvent; 27 import javax.swing.AbstractAction; 28 import javax.swing.Action; 29 import javax.swing.JComboBox; 30 import javax.swing.JFrame; 31 import javax.swing.JLabel; 32 import javax.swing.KeyStroke; 33 import javax.swing.SwingUtilities; 34 import sun.swing.UIAction; 35 36 /** 37 * @test 38 * @bug 8133039 39 * @summary Provide public API to sun.swing.UIAction#isEnabled(Object) 40 * @author Alexander Scherbatiy 41 */ 42 public class bug8133039 { 43 44 private static volatile int ACTION_PERFORMED_CALLS = 0; 45 private static volatile int ACTION_ACCEPTED_CALLS = 0; 46 47 public static void main(String[] args) throws Exception { 48 testActionNotification(); 49 testPopupAction(); 50 } 51 52 private static void testActionNotification() { 53 54 KeyEvent keyEvent = new KeyEvent(new JLabel("Test"), 0, 0, 0, 0, 'A'); 55 SenderObject rejectedSenderObject = new SenderObject(); 56 SwingUtilities.notifyAction(new TestAction(false), null, keyEvent, 57 rejectedSenderObject, 0); 58 59 if (rejectedSenderObject.accepted) { 60 throw new RuntimeException("The sender is incorrectly accepted!"); 61 } 62 63 if (rejectedSenderObject.performed) { 64 throw new RuntimeException("The action is incorrectly performed!"); 65 } 66 67 SenderObject acceptedSenderObject = new SenderObject(); 68 SwingUtilities.notifyAction(new TestAction(true), null, keyEvent, 69 acceptedSenderObject, 0); 70 71 if (!acceptedSenderObject.accepted) { 72 throw new RuntimeException("The sender is not accepted!"); 73 } 74 75 if (!acceptedSenderObject.performed) { 76 throw new RuntimeException("The action is not performed!"); 77 } 78 } 79 80 private static void testPopupAction() throws Exception { 81 82 SwingUtilities.invokeAndWait(bug8133039::createAndShowGUI); 83 84 Robot robot = new Robot(); 85 robot.setAutoDelay(50); 86 robot.waitForIdle(); 87 88 robot.keyPress(KeyEvent.VK_A); 89 robot.keyRelease(KeyEvent.VK_A); 90 robot.waitForIdle(); 91 92 if (ACTION_ACCEPTED_CALLS != 1) { 93 throw new RuntimeException("Method accept is not invoked!"); 94 } 95 96 if (ACTION_PERFORMED_CALLS != 1) { 97 throw new RuntimeException("Method actionPerformed is not invoked!"); 98 } 99 100 robot.keyPress(KeyEvent.VK_A); 101 robot.keyRelease(KeyEvent.VK_A); 102 robot.waitForIdle(); 103 104 if (ACTION_ACCEPTED_CALLS != 2) { 105 throw new RuntimeException("Method accept is not invoked!"); 106 } 107 108 if (ACTION_PERFORMED_CALLS != 1) { 109 throw new RuntimeException("Method actionPerformed is invoked twice!"); 110 } 111 } 112 113 private static void createAndShowGUI() { 114 115 JFrame frame = new JFrame(); 116 frame.setSize(300, 300); 117 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 118 119 JComboBox<String> comboBox = new JComboBox<>(new String[]{"1", "2", "3"}); 120 121 Action showPopupAction = new ShowPopupAction(); 122 comboBox.getInputMap().put(KeyStroke.getKeyStroke("A"), "showPopup"); 123 comboBox.getActionMap().put("showPopup", showPopupAction); 124 125 frame.getContentPane().add(comboBox); 126 frame.setVisible(true); 127 } 128 129 private static class ShowPopupAction extends UIAction { 130 131 public ShowPopupAction() { 132 super("showPopup"); 133 } 134 135 @Override 136 public void actionPerformed(ActionEvent e) { 137 ACTION_PERFORMED_CALLS++; 138 Object src = e.getSource(); 139 if (src instanceof JComboBox) { 140 ((JComboBox) src).showPopup(); 141 } 142 } 143 144 @Override 145 public boolean accept(Object sender) { 146 ACTION_ACCEPTED_CALLS++; 147 if (sender instanceof JComboBox) { 148 JComboBox c = (JComboBox) sender; 149 return !c.isPopupVisible(); 150 } 151 return false; 152 } 153 } 154 155 private static class SenderObject { 156 157 private boolean accepted; 158 private boolean performed; 159 } 160 161 private static class TestAction extends AbstractAction { 162 163 private final boolean acceptSender; 164 165 public TestAction(boolean acceptSender) { 166 this.acceptSender = acceptSender; 167 } 168 169 @Override 170 public boolean accept(Object sender) { 171 ((SenderObject) sender).accepted = acceptSender; 172 return acceptSender; 173 } 174 175 @Override 176 public void actionPerformed(ActionEvent e) { 177 ((SenderObject) e.getSource()).performed = true; 178 } 179 } 180 }