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
35import javax.swing.*;
36import java.awt.event.*;
37import java.awt.*;
38
39public class bug4917669 {
40
41    private static volatile boolean passed = false;
42    private static JFrame mainFrame;
43
44    public static void main(String[] args) throws Exception {
45        Robot robot = new Robot();
46        robot.setAutoDelay(500);
47
48        SwingUtilities.invokeAndWait(new Runnable() {
49
50            @Override
51            public void run() {
52                createAndShowGUI();
53            }
54        });
55
56        robot.waitForIdle();
57
58        SwingUtilities.invokeAndWait(new Runnable() {
59
60            @Override
61            public void run() {
62                createAndShowDialog();
63            }
64        });
65
66        robot.waitForIdle();
67
68        Util.hitKeys(robot, KeyEvent.VK_CONTROL, KeyEvent.VK_O);
69        robot.waitForIdle();
70
71        if (!passed) {
72            throw new RuntimeException("Action did not received by menu item.");
73        }
74    }
75
76    private static void createAndShowDialog() {
77        JDialog dialog = new JDialog(mainFrame, "Test Dialog", false);
78        JMenuBar mb = new JMenuBar();
79        JMenu file = new JMenu("File");
80        JMenuItem menuItem = new JMenuItem("Open");
81        menuItem.setAccelerator(KeyStroke.getKeyStroke("control O"));
82        menuItem.addActionListener(new ActionListener() {
83
84            public void actionPerformed(ActionEvent e) {
85                passed = true;
86            }
87        });
88        file.add(menuItem);
89        mb.add(file);
90        dialog.setJMenuBar(mb);
91
92        dialog.setSize(100, 100);
93        dialog.setLocation(200, 200);
94        dialog.setVisible(true);
95    }
96
97    private static void createAndShowGUI() {
98        mainFrame = new JFrame("Bug4917669");
99        mainFrame.setLayout(new BorderLayout());
100        mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
101        mainFrame.setSize(50, 50);
102        mainFrame.setVisible(true);
103    }
104
105}
106