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