bug6725409.java revision 11018:66682f651425
1281SN/A/*
2281SN/A * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
3281SN/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4281SN/A *
5281SN/A * This code is free software; you can redistribute it and/or modify it
61113Sjoehw * under the terms of the GNU General Public License version 2 only, as
71113Sjoehw * published by the Free Software Foundation.
81113Sjoehw *
91113Sjoehw * This code is distributed in the hope that it will be useful, but WITHOUT
101113Sjoehw * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111113Sjoehw * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12281SN/A * version 2 for more details (a copy is included in the LICENSE file that
13281SN/A * accompanied this code).
14281SN/A *
15281SN/A * You should have received a copy of the GNU General Public License version
16281SN/A * 2 along with this work; if not, write to the Free Software Foundation,
17281SN/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18281SN/A *
19281SN/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20281SN/A * or visit www.oracle.com if you need additional information or have any
21281SN/A * questions.
22281SN/A */
23281SN/A
24281SN/A/* @test
25281SN/A * @bug 6725409
26281SN/A * @summary Checks that JInternalFrame's system menu
27281SN/A *          can be localized during run-time
28281SN/A * @author Mikhail Lapshin
29281SN/A * @library ../../../../lib/testlibrary/
30281SN/A * @build ExtendedRobot
31281SN/A * @run main bug6725409
32281SN/A */
33281SN/A
34281SN/Aimport javax.swing.*;
35281SN/Aimport java.awt.*;
36281SN/A
37281SN/Apublic class bug6725409 {
38281SN/A    private JFrame frame;
39281SN/A    private JInternalFrame iFrame;
40281SN/A    private TestTitlePane testTitlePane;
41281SN/A    private boolean passed;
42281SN/A    private static ExtendedRobot robot = createRobot();
43281SN/A
44281SN/A    public static void main(String[] args) throws Exception {
45281SN/A        try {
46281SN/A            UIManager.setLookAndFeel(
47281SN/A                    new com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel());
48281SN/A        } catch(UnsupportedLookAndFeelException e) {
49281SN/A            System.out.println("The test is for Windows LaF only");
50281SN/A            return;
51281SN/A        }
52281SN/A
53281SN/A        final bug6725409 bug6725409 = new bug6725409();
54281SN/A        try {
55281SN/A            SwingUtilities.invokeAndWait(new Runnable() {
56281SN/A                public void run() {
57281SN/A                    bug6725409.setupUIStep1();
58281SN/A                }
59281SN/A            });
60281SN/A            sync();
61281SN/A            SwingUtilities.invokeAndWait(new Runnable() {
62281SN/A                public void run() {
63281SN/A                    bug6725409.setupUIStep2();
64281SN/A                }
65281SN/A            });
66281SN/A            sync();
67281SN/A            SwingUtilities.invokeAndWait(new Runnable() {
68281SN/A                public void run() {
69281SN/A                    bug6725409.test();
70281SN/A                }
71281SN/A            });
72281SN/A            sync();
73281SN/A            bug6725409.checkResult();
74281SN/A        } finally {
75281SN/A            if (bug6725409.frame != null) {
76281SN/A                bug6725409.frame.dispose();
77281SN/A            }
78281SN/A        }
79281SN/A    }
80281SN/A
81281SN/A    private void setupUIStep1() {
82281SN/A        frame = new JFrame();
83281SN/A        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84281SN/A
85281SN/A        JDesktopPane desktop = new JDesktopPane();
86281SN/A        iFrame = new JInternalFrame("Internal Frame", true, true, true, true);
87281SN/A        iFrame.setSize(200, 100);
88281SN/A        desktop.add(iFrame);
89281SN/A        frame.add(desktop);
90281SN/A        iFrame.setVisible(true);
91281SN/A
92281SN/A        frame.setSize(500, 300);
93281SN/A        frame.setLocationRelativeTo(null);
94281SN/A        frame.setVisible(true);
95281SN/A    }
96281SN/A
97281SN/A    private void setupUIStep2() {
98281SN/A        UIManager.put("InternalFrameTitlePane.restoreButtonText",
99281SN/A                "CUSTOM.restoreButtonText");
100281SN/A        UIManager.put("InternalFrameTitlePane.moveButtonText",
101281SN/A                "CUSTOM.moveButtonText");
102281SN/A        UIManager.put("InternalFrameTitlePane.sizeButtonText",
103281SN/A                "CUSTOM.sizeButtonText");
104281SN/A        UIManager.put("InternalFrameTitlePane.minimizeButtonText",
105281SN/A                "CUSTOM.minimizeButtonText");
106281SN/A        UIManager.put("InternalFrameTitlePane.maximizeButtonText",
107281SN/A                "CUSTOM.maximizeButtonText");
108281SN/A        UIManager.put("InternalFrameTitlePane.closeButtonText",
109281SN/A                "CUSTOM.closeButtonText");
110281SN/A        SwingUtilities.updateComponentTreeUI(frame);
111281SN/A    }
112281SN/A
113281SN/A    // The test depends on the order of the menu items in
114281SN/A    // WindowsInternalFrameTitlePane.systemPopupMenu
115281SN/A    private void test() {
116281SN/A        testTitlePane = new TestTitlePane(iFrame);
117281SN/A        passed = true;
118281SN/A        checkMenuItemText(0, "CUSTOM.restoreButtonText");
119281SN/A        checkMenuItemText(1, "CUSTOM.moveButtonText");
120281SN/A        checkMenuItemText(2, "CUSTOM.sizeButtonText");
121281SN/A        checkMenuItemText(3, "CUSTOM.minimizeButtonText");
122281SN/A        checkMenuItemText(4, "CUSTOM.maximizeButtonText");
123281SN/A        // Skip separator
124281SN/A        checkMenuItemText(6, "CUSTOM.closeButtonText");
125281SN/A    }
126281SN/A
127281SN/A    private void checkMenuItemText(int index, String text) {
128281SN/A        JMenuItem menuItem = (JMenuItem)
129281SN/A                testTitlePane.getSystemPopupMenu().getComponent(index);
130281SN/A        if (!text.equals(menuItem.getText())) {
131281SN/A            passed = false;
132281SN/A        }
133281SN/A    }
134281SN/A
135281SN/A    private void checkResult() {
136281SN/A        if (passed) {
137281SN/A            System.out.println("Test passed");
138281SN/A        } else {
139281SN/A            throw new RuntimeException("Unable to localize " +
140281SN/A                    "JInternalFrame's system menu during run-time");
141281SN/A        }
142281SN/A    }
143281SN/A
144281SN/A    private static void sync() {
145281SN/A        robot.waitForIdle();
146281SN/A    }
147281SN/A    private static ExtendedRobot createRobot() {
148281SN/A        try {
149281SN/A             ExtendedRobot robot = new ExtendedRobot();
150281SN/A             return robot;
151281SN/A         }catch(Exception ex) {
152281SN/A             ex.printStackTrace();
153281SN/A             throw new Error("Unexpected Failure");
154281SN/A         }
155281SN/A    }
156281SN/A
157281SN/A    // Extend WindowsInternalFrameTitlePane to get access to systemPopupMenu
158281SN/A    private class TestTitlePane extends
159281SN/A            com.sun.java.swing.plaf.windows.WindowsInternalFrameTitlePane {
160281SN/A        private JPopupMenu systemPopupMenu;
161281SN/A
162281SN/A        public TestTitlePane(JInternalFrame f) {
163281SN/A            super(f);
164281SN/A        }
165281SN/A
166281SN/A        public JPopupMenu getSystemPopupMenu() {
167281SN/A            return systemPopupMenu;
168281SN/A        }
169281SN/A
170281SN/A        protected void addSystemMenuItems(JPopupMenu menu) {
171281SN/A            super.addSystemMenuItems(menu);
172281SN/A            systemPopupMenu = menu;
173281SN/A        }
174281SN/A    }
175281SN/A}
176281SN/A