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