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