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