1 /*
   2  * Copyright (c) 2003, 2016, 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  * @key headful
  27  * @bug 4634626
  28  * @summary Implement context popup menus for components
  29  * @author Alexander Zuev
  30  * @library ../../../../lib/testlibrary
  31  * @build ExtendedRobot
  32  * @run applet bug4634626.html
  33  */
  34 
  35 import javax.swing.*;
  36 import java.awt.event.*;
  37 import java.awt.*;
  38 
  39 public class bug4634626 extends JApplet {
  40 
  41     public boolean passed = true;
  42     public boolean done = false;
  43 
  44     public JFrame mainFrame = new JFrame("Bug4634626");
  45     public JRootPane rootPane = mainFrame.getRootPane();
  46     public JPanel contentPane = new JPanel();
  47     public JButton nopButton = new JButton("No popup button");
  48     public JTextArea someText = new JTextArea("Some text here", 20, 10);
  49     public JButton popButton = new JButton("Button with the popup");
  50 
  51     public JPopupMenu btnPopup = new JPopupMenu();
  52     public JPopupMenu commonPopup = new JPopupMenu();
  53     static public Error toBeThrown = null;
  54     static int popTrig = MouseEvent.BUTTON3_MASK;
  55     static boolean popt = false;
  56 
  57     public static class MouseWatcher extends MouseAdapter {
  58         public void mousePressed(MouseEvent e) {
  59             if(e.isPopupTrigger()) popt = true;
  60             if(e.getComponent() != null &&
  61                e.getComponent() instanceof JComponent &&
  62                e.isPopupTrigger() &&
  63                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  64                 toBeThrown =
  65                   new Error("The event got thru the component with popup: "
  66                   + e);
  67             }
  68         }
  69         public void mouseReleased(MouseEvent e) {
  70             if(e.isPopupTrigger()) popt = true;
  71             if(e.getComponent() != null &&
  72                e.getComponent() instanceof JComponent &&
  73                e.isPopupTrigger() &&
  74                ((JComponent)e.getComponent()).getComponentPopupMenu() != null) {
  75                 toBeThrown =
  76                   new Error("The event got thru the component with popup: "
  77                   + e);
  78             }
  79             if(toBeThrown != null) {
  80                 throw(toBeThrown);
  81             }
  82         }
  83     }
  84 
  85     public static MouseWatcher mouser = new MouseWatcher();
  86 
  87     public void init() {
  88 
  89         try {
  90             popButton.setComponentPopupMenu(null);
  91             popButton.setComponentPopupMenu(null);
  92             popButton.setComponentPopupMenu(btnPopup);
  93             popButton.setComponentPopupMenu(null);
  94         } catch(Exception ex) {
  95             System.err.println("Unexpected exception was thrown by " +
  96                                "setComponentPopupMenu() method: " + ex);
  97         }
  98         btnPopup.add("Button 1");
  99         btnPopup.add("Button 2");
 100         btnPopup.add("Button 3");
 101         popButton.setComponentPopupMenu(btnPopup);
 102         popButton.addMouseListener(mouser);
 103         commonPopup.add("One");
 104         commonPopup.add("Two");
 105         commonPopup.add("Three");
 106 
 107         contentPane.setLayout(new BorderLayout());
 108         contentPane.setComponentPopupMenu(commonPopup);
 109         contentPane.addMouseListener(mouser);
 110         contentPane.add(nopButton, BorderLayout.NORTH);
 111         nopButton.addMouseListener(mouser);
 112         contentPane.add(popButton, BorderLayout.SOUTH);
 113         someText.addMouseListener(mouser);
 114         contentPane.add(someText, BorderLayout.CENTER);
 115         mainFrame.setContentPane(contentPane);
 116 
 117         mainFrame.pack();
 118         mainFrame.setLocation(50, 50);
 119 
 120         mainFrame.addWindowListener(new TestStateListener());
 121         mainFrame.setVisible(true);
 122 
 123         while(!done) Thread.yield();
 124 
 125         if(!passed) {
 126             throw new RuntimeException("Test failed");
 127         }
 128 
 129     }
 130 
 131     public class TestStateListener extends WindowAdapter {
 132         public void windowOpened(WindowEvent ev) {
 133             try {
 134                 ev.getWindow().toFront();
 135                 ev.getWindow().requestFocus();
 136                 new Thread(new RobotThread()).start();
 137             } catch (Exception ex) {
 138                 throw new RuntimeException("Thread Exception");
 139             }
 140         }
 141     }
 142 
 143     class RobotThread implements Runnable {
 144         public void run() {
 145             ExtendedRobot robo;
 146             try {
 147                 robo = new ExtendedRobot();
 148             }catch(Exception ex) {
 149                 ex.printStackTrace();
 150                 throw new RuntimeException("Cannot create Robot");
 151             }
 152             robo.setAutoDelay(100);
 153             robo.waitForIdle();
 154 
 155             // Determine working popup trigger event
 156             clickMouseOn(robo, nopButton, popTrig);
 157             robo.waitForIdle();
 158             robo.delay(500);
 159             if(!popt) popTrig = MouseEvent.BUTTON2_MASK;
 160 
 161             // Inheritance is OFF by default. Popup should not appear.
 162             clickMouseOn(robo, someText, popTrig);
 163 
 164             // Set inheritance ON watch for popup.
 165             someText.setInheritsPopupMenu(true);
 166             clickMouseOn(robo, someText, popTrig);
 167             robo.waitForIdle();
 168             robo.delay(500);
 169             if(!commonPopup.isVisible()) {
 170                 toBeThrown = new Error("Popup should be visible");
 171                 passed = false;
 172             }
 173             // Dispose popup.
 174             robo.type(KeyEvent.VK_ESCAPE);
 175             robo.waitForIdle();
 176             someText.setInheritsPopupMenu(false);
 177 
 178             // Button with popup assigned. Wathch for popup.
 179             clickMouseOn(robo, popButton, popTrig);
 180             robo.waitForIdle();
 181             robo.delay(500);
 182             if(!btnPopup.isVisible()) {
 183                 toBeThrown = new Error("Popup should be visible");
 184                 passed = false;
 185             }
 186             // Dispose popup.
 187             robo.type(KeyEvent.VK_ESCAPE);
 188             // Test finished.
 189             done = true;
 190         }
 191     }
 192 
 193 
 194 
 195     public void destroy() {
 196         if(!passed) {
 197             throw(toBeThrown);
 198         }
 199     }
 200     private void clickMouseOn(ExtendedRobot robot, Component c, int button) {
 201         java.awt.Point p = c.getLocationOnScreen();
 202         java.awt.Dimension size = c.getSize();
 203         p.x += size.width / 2;
 204         p.y += size.height / 2;
 205         robot.mouseMove(p.x, p.y);
 206         robot.delay(100);
 207         robot.click(button);
 208     }
 209 }