4622N/A/*
4622N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
4622N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4622N/A *
4622N/A * This code is free software; you can redistribute it and/or modify it
4622N/A * under the terms of the GNU General Public License version 2 only, as
4622N/A * published by the Free Software Foundation.
4622N/A *
4622N/A * This code is distributed in the hope that it will be useful, but WITHOUT
4622N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4622N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4622N/A * version 2 for more details (a copy is included in the LICENSE file that
4622N/A * accompanied this code).
4622N/A *
4622N/A * You should have received a copy of the GNU General Public License version
4622N/A * 2 along with this work; if not, write to the Free Software Foundation,
4622N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4622N/A *
4622N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4622N/A * or visit www.oracle.com if you need additional information or have any
4622N/A * questions.
4622N/A */
4622N/A
4622N/A/*
4622N/A * @test
4622N/A * @bug 4966112
4622N/A * @summary Some Composite components does not show the Context Popup.
4622N/A * @library ../../regtesthelpers
4622N/A * @build Util
4622N/A * @author Alexander Zuev
4622N/A * @run main bug4966112
4622N/A */
4622N/Aimport javax.swing.*;
4622N/Aimport javax.swing.event.PopupMenuListener;
4622N/Aimport javax.swing.event.PopupMenuEvent;
4622N/Aimport java.awt.*;
4622N/Aimport java.awt.event.*;
4622N/Aimport sun.awt.SunToolkit;
4622N/A
4622N/Apublic class bug4966112 {
4622N/A
4622N/A private static final int NO_MOUSE_BUTTON = -1;
4622N/A private static volatile boolean shown = false;
4622N/A private static volatile int popupButton = NO_MOUSE_BUTTON;
4622N/A private static volatile JButton testButton;
4622N/A private static volatile JSplitPane jsp;
4622N/A private static volatile JSpinner spin;
4622N/A private static volatile JFileChooser filec;
4622N/A private static int buttonMask;
4622N/A private static Robot robot;
4622N/A
4622N/A public static void main(String[] args) throws Exception {
4622N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
4622N/A robot = new Robot();
4622N/A robot.setAutoDelay(100);
4622N/A
4622N/A createAndShowButton();
4622N/A toolkit.realSync();
4622N/A
4622N/A setClickPoint(testButton);
4622N/A clickMouse(InputEvent.BUTTON1_MASK);
4622N/A clickMouse(InputEvent.BUTTON2_MASK);
4622N/A clickMouse(InputEvent.BUTTON3_MASK);
4622N/A
4622N/A toolkit.realSync();
4622N/A closeFrame();
4622N/A
4622N/A if (popupButton == NO_MOUSE_BUTTON) {
4622N/A System.out.println("Test can't identify the popup trigger button. Test skipped");
4622N/A return;
4622N/A }
4622N/A
4622N/A setButtonMask();
4622N/A
4622N/A // Test Split Pane
4622N/A createAndShowSplitPane();
4622N/A toolkit.realSync();
4622N/A
4622N/A clickMouse(jsp);
4622N/A toolkit.realSync();
4622N/A closeFrame();
4622N/A
4622N/A if (!shown) {
4622N/A throw new RuntimeException("Popup was not shown on splitpane");
4622N/A }
4622N/A
4622N/A // Test Spinner
4622N/A createAndShowSpinner();
4622N/A toolkit.realSync();
4622N/A
4622N/A clickMouse(spin);
4622N/A toolkit.realSync();
4622N/A closeFrame();
4622N/A
4622N/A if (!shown) {
4622N/A throw new RuntimeException("Popup was not shown on spinner");
4622N/A }
4622N/A
4622N/A // Test File Chooser
4622N/A createAndShowFileChooser();
4622N/A toolkit.realSync();
4622N/A
4622N/A clickMouse(filec);
4622N/A toolkit.realSync();
4622N/A
4622N/A Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
4622N/A toolkit.realSync();
4622N/A
4622N/A Util.hitKeys(robot, KeyEvent.VK_ESCAPE);
4622N/A toolkit.realSync();
4622N/A closeFrame();
4622N/A
4622N/A if (!shown) {
4622N/A throw new RuntimeException("Popup was not shown on filechooser");
4622N/A }
4622N/A }
4622N/A
4622N/A private static void clickMouse(JComponent c) throws Exception {
4622N/A setClickPoint(c);
4622N/A clickMouse(buttonMask);
4622N/A }
4622N/A
4622N/A private static void clickMouse(int buttons) {
4622N/A robot.mousePress(buttons);
4622N/A robot.mouseRelease(buttons);
4622N/A }
4622N/A
4622N/A private static void setButtonMask() {
4622N/A switch (popupButton) {
4622N/A case MouseEvent.BUTTON1:
4622N/A buttonMask = InputEvent.BUTTON1_MASK;
4622N/A break;
4622N/A case MouseEvent.BUTTON2:
4622N/A buttonMask = InputEvent.BUTTON2_MASK;
4622N/A break;
4622N/A case MouseEvent.BUTTON3:
4622N/A buttonMask = InputEvent.BUTTON3_MASK;
4622N/A break;
4622N/A }
4622N/A }
4622N/A
4622N/A private static void setClickPoint(final JComponent c) throws Exception {
4622N/A final Point[] result = new Point[1];
4622N/A SwingUtilities.invokeAndWait(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A Point p = c.getLocationOnScreen();
4622N/A Dimension size = c.getSize();
4622N/A result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
4622N/A }
4622N/A });
4622N/A
4622N/A robot.mouseMove(result[0].x, result[0].y);
4622N/A }
4622N/A
4622N/A private static JPopupMenu createJPopupMenu() {
4622N/A JPopupMenu jpm = new JPopupMenu();
4622N/A jpm.add("One");
4622N/A jpm.add("Two");
4622N/A jpm.add("Three");
4622N/A jpm.addPopupMenuListener(new PopupMenuListener() {
4622N/A
4622N/A public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
4622N/A shown = true;
4622N/A }
4622N/A
4622N/A public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
4622N/A }
4622N/A
4622N/A public void popupMenuCanceled(PopupMenuEvent e) {
4622N/A }
4622N/A });
4622N/A
4622N/A AutoClosable.INSTANCE.setPopup(jpm);
4622N/A return jpm;
4622N/A }
4622N/A
4622N/A private static void createAndShowButton() throws Exception {
4622N/A SwingUtilities.invokeAndWait(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A JFrame frame = new JFrame("Button Frame");
4622N/A frame.setLayout(new BorderLayout());
4622N/A testButton = new JButton("Popup Tester");
4622N/A
4622N/A testButton.addMouseListener(new MouseAdapter() {
4622N/A
4622N/A void setPopupTrigger(MouseEvent e) {
4622N/A if (e.isPopupTrigger()) {
4622N/A popupButton = e.getButton();
4622N/A }
4622N/A }
4622N/A
4622N/A public void mouseClicked(MouseEvent e) {
4622N/A setPopupTrigger(e);
4622N/A }
4622N/A
4622N/A public void mousePressed(MouseEvent e) {
4622N/A setPopupTrigger(e);
4622N/A }
4622N/A
4622N/A public void mouseReleased(MouseEvent e) {
4622N/A setPopupTrigger(e);
4622N/A }
4622N/A });
4622N/A
4622N/A frame.add(testButton, BorderLayout.CENTER);
4622N/A frame.pack();
4622N/A frame.setVisible(true);
4622N/A AutoClosable.INSTANCE.setFrame(frame);
4622N/A }
4622N/A });
4622N/A }
4622N/A
4622N/A private static void createAndShowSplitPane() throws Exception {
4622N/A SwingUtilities.invokeAndWait(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A JFrame frame = new JFrame("Test SplitPane");
4622N/A frame.setSize(250, 200);
4622N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4622N/A frame.setLayout(new BorderLayout());
4622N/A
4622N/A shown = false;
4622N/A jsp = new JSplitPane();
4622N/A jsp.setRightComponent(new JPanel());
4622N/A jsp.setLeftComponent(new JPanel());
4622N/A jsp.setComponentPopupMenu(createJPopupMenu());
4622N/A
4622N/A frame.add(jsp, BorderLayout.CENTER);
4622N/A
4622N/A jsp.setDividerLocation(150);
4622N/A
4622N/A frame.setLocation(400, 300);
4622N/A frame.setVisible(true);
4622N/A AutoClosable.INSTANCE.setFrame(frame);
4622N/A }
4622N/A });
4622N/A }
4622N/A
4622N/A private static void createAndShowSpinner() throws Exception {
4622N/A SwingUtilities.invokeAndWait(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A JFrame frame = new JFrame("JSpinner Test");
4622N/A frame.setLayout(new BorderLayout());
4622N/A frame.setSize(200, 100);
4622N/A shown = false;
4622N/A spin = new JSpinner();
4622N/A spin.setComponentPopupMenu(createJPopupMenu());
4622N/A frame.add(spin, BorderLayout.CENTER);
4622N/A frame.setVisible(true);
4622N/A AutoClosable.INSTANCE.setFrame(frame);
4622N/A }
4622N/A });
4622N/A }
4622N/A
4622N/A private static void createAndShowFileChooser() throws Exception {
4622N/A SwingUtilities.invokeLater(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A JFrame frame = new JFrame("FileChooser test dialog");
4622N/A frame.setSize(100, 100);
4622N/A
4622N/A shown = false;
4622N/A filec = new JFileChooser();
4622N/A filec.setComponentPopupMenu(createJPopupMenu());
4622N/A filec.showOpenDialog(frame);
4622N/A
4622N/A frame.setVisible(true);
4622N/A AutoClosable.INSTANCE.setFrame(frame);
4622N/A }
4622N/A });
4622N/A }
4622N/A
4622N/A private static void closeFrame() {
4622N/A SwingUtilities.invokeLater(new Runnable() {
4622N/A
4622N/A @Override
4622N/A public void run() {
4622N/A AutoClosable.INSTANCE.close();
4622N/A }
4622N/A });
4622N/A }
4622N/A
4622N/A private static class AutoClosable {
4622N/A
4622N/A static final AutoClosable INSTANCE = new AutoClosable();
4622N/A private JFrame frame;
4622N/A private JPopupMenu popup;
4622N/A
4622N/A public void setFrame(JFrame frame) {
4622N/A this.frame = frame;
4622N/A }
4622N/A
4622N/A public void setPopup(JPopupMenu popup) {
4622N/A this.popup = popup;
4622N/A }
4622N/A
4622N/A public void close() {
4622N/A frame.dispose();
4622N/A if (popup != null) {
4622N/A popup.setVisible(false);
4622N/A }
4622N/A }
4622N/A }
4622N/A}