diff --git a/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java b/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java index 6a48eb423a3..9900c9b7e0e 100644 --- a/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java +++ b/jdk/src/java.desktop/share/classes/javax/swing/SwingUtilities.java @@ -845,6 +845,38 @@ public class SwingUtilities implements SwingConstants return result; } + /** + * Check whether MouseEvent contains speficied mouse button or + * mouse button down mask based on MouseEvent ID. + * + * @param anEvent a MouseEvent object + * @param mouseButton mouse button type + * @param mouseButtonDownMask mouse button down mask event modifier + * + * @return true if the anEvent contains speficied mouseButton or + * mouseButtonDownMask based on MouseEvent ID. + */ + private static boolean checkMouseButton(MouseEvent anEvent, + int mouseButton, + int mouseButtonDownMask) + { + switch (anEvent.getID()) { + case MouseEvent.MOUSE_PRESSED: + case MouseEvent.MOUSE_RELEASED: + case MouseEvent.MOUSE_CLICKED: + return (anEvent.getButton() == mouseButton); + + case MouseEvent.MOUSE_ENTERED: + case MouseEvent.MOUSE_EXITED: + case MouseEvent.MOUSE_DRAGGED: + return ((anEvent.getModifiersEx() & mouseButtonDownMask) != 0); + + default: + return ((anEvent.getModifiersEx() & mouseButtonDownMask) != 0 || + anEvent.getButton() == mouseButton); + } + } + /** * Returns true if the mouse event specifies the left mouse button. * @@ -852,8 +884,8 @@ public class SwingUtilities implements SwingConstants * @return true if the left mouse button was active */ public static boolean isLeftMouseButton(MouseEvent anEvent) { - return ((anEvent.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK) != 0 || - anEvent.getButton() == MouseEvent.BUTTON1); + return checkMouseButton(anEvent, MouseEvent.BUTTON1, + InputEvent.BUTTON1_DOWN_MASK); } /** @@ -863,8 +895,8 @@ public class SwingUtilities implements SwingConstants * @return true if the middle mouse button was active */ public static boolean isMiddleMouseButton(MouseEvent anEvent) { - return ((anEvent.getModifiersEx() & InputEvent.BUTTON2_DOWN_MASK) != 0 || - anEvent.getButton() == MouseEvent.BUTTON2); + return checkMouseButton(anEvent, MouseEvent.BUTTON2, + InputEvent.BUTTON2_DOWN_MASK); } /** @@ -874,8 +906,8 @@ public class SwingUtilities implements SwingConstants * @return true if the right mouse button was active */ public static boolean isRightMouseButton(MouseEvent anEvent) { - return ((anEvent.getModifiersEx() & InputEvent.BUTTON3_DOWN_MASK) != 0 || - anEvent.getButton() == MouseEvent.BUTTON3); + return checkMouseButton(anEvent, MouseEvent.BUTTON3, + InputEvent.BUTTON3_DOWN_MASK); } /** diff --git a/jdk/test/javax/swing/JButton/PressedButtonRightClickTest.java b/jdk/test/javax/swing/JButton/PressedButtonRightClickTest.java new file mode 100644 index 00000000000..c4cddc71f18 --- /dev/null +++ b/jdk/test/javax/swing/JButton/PressedButtonRightClickTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + + +import java.awt.AWTException; +import java.awt.BorderLayout; +import java.awt.Point; +import java.awt.Robot; +import java.awt.event.InputEvent; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.SwingUtilities; + +/* + * @test + * @bug 8049069 + * @summary Tests whether right mouse click releases a pressed JButton + */ + +public class PressedButtonRightClickTest { + + private static Robot testRobot; + private static JFrame myFrame; + private static JButton myButton; + + public static void main(String[] args) throws Throwable { + + SwingUtilities.invokeAndWait(new Runnable() { + + @Override + public void run() { + constructTestUI(); + } + }); + + try { + testRobot = new Robot(); + } catch (AWTException ex) { + throw new RuntimeException("Exception in Robot creation"); + } + + testRobot.waitForIdle(); + + // Method performing auto test operation + test(); + + disposeTestUI(); + } + + private static void test() { + Point loc = myFrame.getLocationOnScreen(); + + testRobot.mouseMove((loc.x + 100), (loc.y + 100)); + + // Press the left mouse button + testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + myButton.setText("Left button pressed"); + testRobot.delay(1000); + + // Press the right mouse button + testRobot.mousePress(InputEvent.BUTTON3_DOWN_MASK); + myButton.setText("Left button pressed + Right button pressed"); + testRobot.delay(1000); + + // Release the right mouse button + testRobot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK); + myButton.setText("Right button released"); + testRobot.delay(1000); + + // Test whether the button is still pressed + if (myButton.getModel().isPressed() == false) { + disposeTestUI(); + throw new RuntimeException("Test Failed!"); + } + } + + private static void disposeTestUI() { + myFrame.setVisible(false); + myFrame.dispose(); + } + + public static void constructTestUI() { + myFrame = new JFrame(); + myFrame.setLayout(new BorderLayout()); + myButton = new JButton("Whatever"); + myFrame.add(myButton, BorderLayout.CENTER); + myFrame.setSize(400, 300); + myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + myFrame.setVisible(true); + } +} +