1 
   2 /*
   3  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * @test
  27  * @key headful
  28  * @bug 4917669
  29  * @summary 1.4 REGRESSION: MenuItem accelerator doesn't work if parent menu is in JDialog
  30  * @author Alexander Zuev
  31  * @library ../../regtesthelpers
  32  * @run main bug4917669
  33  */
  34 
  35 import javax.swing.*;
  36 import java.awt.event.*;
  37 import java.awt.*;
  38 import sun.awt.SunToolkit;
  39 
  40 public class bug4917669 {
  41 
  42     private static volatile boolean passed = false;
  43     private static JFrame mainFrame;
  44 
  45     public static void main(String[] args) throws Exception {
  46         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  47         Robot robot = new Robot();
  48         robot.setAutoDelay(500);
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57 
  58         toolkit.realSync();
  59 
  60         SwingUtilities.invokeAndWait(new Runnable() {
  61 
  62             @Override
  63             public void run() {
  64                 createAndShowDialog();
  65             }
  66         });
  67 
  68         toolkit.realSync();
  69 
  70         Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_O);
  71         toolkit.realSync();
  72 
  73         if (!passed) {
  74             throw new RuntimeException("Action did not received by menu item.");
  75         }
  76     }
  77 
  78     private static void createAndShowDialog() {
  79         JDialog dialog = new JDialog(mainFrame, "Test Dialog", false);
  80         JMenuBar mb = new JMenuBar();
  81         JMenu file = new JMenu("File");
  82         JMenuItem menuItem = new JMenuItem("Open");
  83         menuItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
  84         menuItem.addActionListener(new ActionListener() {
  85 
  86             public void actionPerformed(ActionEvent e) {
  87                 passed = true;
  88             }
  89         });
  90         file.add(menuItem);
  91         mb.add(file);
  92         dialog.setJMenuBar(mb);
  93 
  94         dialog.setSize(100, 100);
  95         dialog.setLocation(200, 200);
  96         dialog.setVisible(true);
  97     }
  98 
  99     private static void createAndShowGUI() {
 100         mainFrame = new JFrame("Bug4917669");
 101         mainFrame.setLayout(new BorderLayout());
 102         mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 103         mainFrame.setSize(50, 50);
 104         mainFrame.setVisible(true);
 105     }
 106 
 107 }