|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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 javax.swing.JButton; |
| 25 | +import javax.swing.JFrame; |
| 26 | +import javax.swing.JMenuItem; |
| 27 | +import javax.swing.JPopupMenu; |
| 28 | +import javax.swing.SwingUtilities; |
| 29 | +import javax.swing.UIManager; |
| 30 | +import javax.swing.UnsupportedLookAndFeelException; |
| 31 | +import java.awt.Container; |
| 32 | +import java.awt.FlowLayout; |
| 33 | +import java.awt.Point; |
| 34 | +import java.awt.Robot; |
| 35 | +import java.awt.event.ActionEvent; |
| 36 | +import java.awt.event.ActionListener; |
| 37 | +import java.awt.event.InputEvent; |
| 38 | +import java.awt.event.MouseEvent; |
| 39 | +import java.io.StringWriter; |
| 40 | +import java.io.PrintWriter; |
| 41 | + |
| 42 | +/** |
| 43 | + * @test |
| 44 | + * @bug 8048109 |
| 45 | + * @summary JToggleButton does not fire actionPerformed under certain |
| 46 | + * conditions |
| 47 | + * @key headful |
| 48 | + * @run main SetInvokerJPopupMenuTest |
| 49 | + */ |
| 50 | + |
| 51 | +public class SetInvokerJPopupMenuTest { |
| 52 | + |
| 53 | + private static MyPopupMenu popup; |
| 54 | + private static MyButton jtb ; |
| 55 | + private static Robot robot; |
| 56 | + private static JFrame f; |
| 57 | + private static Point p; |
| 58 | + private static boolean isPopupVisible; |
| 59 | + |
| 60 | + public static void main(String[] args) throws Exception { |
| 61 | + UIManager.LookAndFeelInfo[] installedLookAndFeels; |
| 62 | + installedLookAndFeels = UIManager.getInstalledLookAndFeels(); |
| 63 | + |
| 64 | + for(UIManager.LookAndFeelInfo LF : installedLookAndFeels) { |
| 65 | + try { |
| 66 | + robot = new Robot(); |
| 67 | + UIManager.setLookAndFeel(LF.getClassName()); |
| 68 | + SwingUtilities.invokeAndWait(() -> { |
| 69 | + jtb = new MyButton("Press Me"); |
| 70 | + jtb.addActionListener(new ActionListener( ) { |
| 71 | + @Override |
| 72 | + public void actionPerformed(ActionEvent ev) { |
| 73 | + if (!popup.isVisible()) { |
| 74 | + postUp(); |
| 75 | + } |
| 76 | + else { |
| 77 | + postDown(); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + f = new JFrame( ); |
| 83 | + f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 84 | + f.setLocationRelativeTo(null); |
| 85 | + f.setSize(300, 400); |
| 86 | + Container c = f.getContentPane( ); |
| 87 | + c.setLayout(new FlowLayout( )); |
| 88 | + c.add(jtb); |
| 89 | + f.setVisible(true); |
| 90 | + popup = new MyPopupMenu(); |
| 91 | + popup.add(new JMenuItem("A")); |
| 92 | + popup.add(new JMenuItem("B")); |
| 93 | + popup.add(new JMenuItem("C")); |
| 94 | + popup.setVisible(false); |
| 95 | + p = jtb.getLocationOnScreen(); |
| 96 | + }); |
| 97 | + |
| 98 | + robot.waitForIdle(); |
| 99 | + robot.setAutoDelay(50); |
| 100 | + robot.mouseMove(p.x + 15, p.y + 15); |
| 101 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 102 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 103 | + |
| 104 | + robot.waitForIdle(); |
| 105 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 106 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 107 | + |
| 108 | + } catch (UnsupportedLookAndFeelException e) { |
| 109 | + System.out.println("Note: LookAndFeel " + LF.getClassName() |
| 110 | + + " is not supported on this configuration"); |
| 111 | + } finally { |
| 112 | + if (f != null) { |
| 113 | + SwingUtilities.invokeAndWait(() -> f.dispose()); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + SwingUtilities.invokeAndWait(() -> { |
| 118 | + isPopupVisible = popup.isVisible(); |
| 119 | + }); |
| 120 | + |
| 121 | + if (isPopupVisible) { |
| 122 | + throw new RuntimeException("PopupMenu is not taken down after"+ |
| 123 | + " single button click"); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + public static void postUp() { |
| 129 | + popup.setInvoker(jtb); |
| 130 | + popup.setVisible(true); |
| 131 | + } |
| 132 | + |
| 133 | + public static void postDown() { |
| 134 | + popup.setVisible(false); |
| 135 | + } |
| 136 | + |
| 137 | + private static class MyButton extends JButton { |
| 138 | + public MyButton(String string) { |
| 139 | + super (string); |
| 140 | + } |
| 141 | + @Override |
| 142 | + protected void processMouseEvent(MouseEvent e) { |
| 143 | + super.processMouseEvent(e); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private static class MyPopupMenu extends JPopupMenu { |
| 148 | + @Override |
| 149 | + public void setVisible( boolean state ) { |
| 150 | + if( !state ) { |
| 151 | + Exception ex = new Exception(); |
| 152 | + StringWriter stringWriter = new StringWriter(); |
| 153 | + PrintWriter printWriter = new PrintWriter( stringWriter ); |
| 154 | + ex.printStackTrace( printWriter ); |
| 155 | + String traceString = stringWriter.getBuffer().toString(); |
| 156 | + if( traceString.lastIndexOf( "windowDeactivated" ) > 0 |
| 157 | + || traceString.lastIndexOf( "menuSelectionChanged" ) |
| 158 | + > 0 ) { |
| 159 | + return; |
| 160 | + } |
| 161 | + } |
| 162 | + super.setVisible(state); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments