1 /*
   2  * Copyright (c) 2008, 2015, 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 /* @test
  25  * @bug 6725409
  26  * @summary Checks that JInternalFrame's system menu
  27  *          can be localized during run-time
  28  * @author Mikhail Lapshin
  29  * @library ../../../../lib/testlibrary/
  30  * @modules java.desktop/com.sun.java.swing.plaf.windows
  31  * @build ExtendedRobot
  32  * @run main bug6725409
  33  */
  34 
  35 import javax.swing.*;
  36 import java.awt.*;
  37 
  38 public class bug6725409 {
  39     private JFrame frame;
  40     private JInternalFrame iFrame;
  41     private TestTitlePane testTitlePane;
  42     private boolean passed;
  43     private static ExtendedRobot robot = createRobot();
  44 
  45     public static void main(String[] args) throws Exception {
  46         try {
  47             UIManager.setLookAndFeel(
  48                     new com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel());
  49         } catch(UnsupportedLookAndFeelException e) {
  50             System.out.println("The test is for Windows LaF only");
  51             return;
  52         }
  53 
  54         final bug6725409 bug6725409 = new bug6725409();
  55         try {
  56             SwingUtilities.invokeAndWait(new Runnable() {
  57                 public void run() {
  58                     bug6725409.setupUIStep1();
  59                 }
  60             });
  61             sync();
  62             SwingUtilities.invokeAndWait(new Runnable() {
  63                 public void run() {
  64                     bug6725409.setupUIStep2();
  65                 }
  66             });
  67             sync();
  68             SwingUtilities.invokeAndWait(new Runnable() {
  69                 public void run() {
  70                     bug6725409.test();
  71                 }
  72             });
  73             sync();
  74             bug6725409.checkResult();
  75         } finally {
  76             if (bug6725409.frame != null) {
  77                 bug6725409.frame.dispose();
  78             }
  79         }
  80     }
  81 
  82     private void setupUIStep1() {
  83         frame = new JFrame();
  84         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85 
  86         JDesktopPane desktop = new JDesktopPane();
  87         iFrame = new JInternalFrame("Internal Frame", true, true, true, true);
  88         iFrame.setSize(200, 100);
  89         desktop.add(iFrame);
  90         frame.add(desktop);
  91         iFrame.setVisible(true);
  92 
  93         frame.setSize(500, 300);
  94         frame.setLocationRelativeTo(null);
  95         frame.setVisible(true);
  96     }
  97 
  98     private void setupUIStep2() {
  99         UIManager.put("InternalFrameTitlePane.restoreButtonText",
 100                 "CUSTOM.restoreButtonText");
 101         UIManager.put("InternalFrameTitlePane.moveButtonText",
 102                 "CUSTOM.moveButtonText");
 103         UIManager.put("InternalFrameTitlePane.sizeButtonText",
 104                 "CUSTOM.sizeButtonText");
 105         UIManager.put("InternalFrameTitlePane.minimizeButtonText",
 106                 "CUSTOM.minimizeButtonText");
 107         UIManager.put("InternalFrameTitlePane.maximizeButtonText",
 108                 "CUSTOM.maximizeButtonText");
 109         UIManager.put("InternalFrameTitlePane.closeButtonText",
 110                 "CUSTOM.closeButtonText");
 111         SwingUtilities.updateComponentTreeUI(frame);
 112     }
 113 
 114     // The test depends on the order of the menu items in
 115     // WindowsInternalFrameTitlePane.systemPopupMenu
 116     private void test() {
 117         testTitlePane = new TestTitlePane(iFrame);
 118         passed = true;
 119         checkMenuItemText(0, "CUSTOM.restoreButtonText");
 120         checkMenuItemText(1, "CUSTOM.moveButtonText");
 121         checkMenuItemText(2, "CUSTOM.sizeButtonText");
 122         checkMenuItemText(3, "CUSTOM.minimizeButtonText");
 123         checkMenuItemText(4, "CUSTOM.maximizeButtonText");
 124         // Skip separator
 125         checkMenuItemText(6, "CUSTOM.closeButtonText");
 126     }
 127 
 128     private void checkMenuItemText(int index, String text) {
 129         JMenuItem menuItem = (JMenuItem)
 130                 testTitlePane.getSystemPopupMenu().getComponent(index);
 131         if (!text.equals(menuItem.getText())) {
 132             passed = false;
 133         }
 134     }
 135 
 136     private void checkResult() {
 137         if (passed) {
 138             System.out.println("Test passed");
 139         } else {
 140             throw new RuntimeException("Unable to localize " +
 141                     "JInternalFrame's system menu during run-time");
 142         }
 143     }
 144 
 145     private static void sync() {
 146         robot.waitForIdle();
 147     }
 148     private static ExtendedRobot createRobot() {
 149         try {
 150              ExtendedRobot robot = new ExtendedRobot();
 151              return robot;
 152          }catch(Exception ex) {
 153              ex.printStackTrace();
 154              throw new Error("Unexpected Failure");
 155          }
 156     }
 157 
 158     // Extend WindowsInternalFrameTitlePane to get access to systemPopupMenu
 159     private class TestTitlePane extends
 160             com.sun.java.swing.plaf.windows.WindowsInternalFrameTitlePane {
 161         private JPopupMenu systemPopupMenu;
 162 
 163         public TestTitlePane(JInternalFrame f) {
 164             super(f);
 165         }
 166 
 167         public JPopupMenu getSystemPopupMenu() {
 168             return systemPopupMenu;
 169         }
 170 
 171         protected void addSystemMenuItems(JPopupMenu menu) {
 172             super.addSystemMenuItems(menu);
 173             systemPopupMenu = menu;
 174         }
 175     }
 176 }