8067346: Swing submenu has a changed starting offset
authoralexsch
Fri, 02 Oct 2015 17:12:47 +0400
changeset 33247 f02018c0b3a4
parent 33246 d54eb15e19eb
child 33248 76e17c81ec4c
8067346: Swing submenu has a changed starting offset Reviewed-by: serb, alexsch Contributed-by: Rajeev Chamyal <rajeev.chamyal@oracle.com>
jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java
jdk/src/java.desktop/share/classes/sun/awt/OSInfo.java
jdk/test/javax/swing/JMenu/8067346/bug8067346.java
--- a/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java	Fri Oct 02 10:29:36 2015 +0400
+++ b/jdk/src/java.desktop/share/classes/com/sun/java/swing/plaf/windows/WindowsLookAndFeel.java	Fri Oct 02 17:12:47 2015 +0400
@@ -1569,6 +1569,11 @@
                 && OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_VISTA) >= 0;
     }
 
+    static boolean isOnWindows7() {
+        return OSInfo.getOSType() == OSInfo.OSType.WINDOWS
+                && OSInfo.getWindowsVersion().compareTo(OSInfo.WINDOWS_7) >= 0;
+    }
+
     private void initVistaComponentDefaults(UIDefaults table) {
         if (! isOnVista()) {
             return;
@@ -1638,28 +1643,30 @@
         }
         table.putDefaults(menuDefaults);
 
-        /* no margins */
-        InsetsUIResource insets = new InsetsUIResource(0, 0, 0, 0);
-        for (int i = 0, j = 0; i < menuClasses.length; i++) {
-            String key = menuClasses[i] + ".margin";
-            Object oldValue = table.get(key);
-            menuDefaults[j++] = key;
-            menuDefaults[j++] = new XPValue(insets, oldValue);
-        }
-        table.putDefaults(menuDefaults);
+        /*For Windows7 margin and checkIconOffset should be greater than 0 */
+        if (!isOnWindows7()) {
+            /* no margins */
+            InsetsUIResource insets = new InsetsUIResource(0, 0, 0, 0);
+            for (int i = 0, j = 0; i < menuClasses.length; i++) {
+                String key = menuClasses[i] + ".margin";
+                Object oldValue = table.get(key);
+                menuDefaults[j++] = key;
+                menuDefaults[j++] = new XPValue(insets, oldValue);
+            }
+            table.putDefaults(menuDefaults);
 
-        /* set checkIcon offset */
-        Integer checkIconOffsetInteger =
-            Integer.valueOf(0);
-        for (int i = 0, j = 0; i < menuClasses.length; i++) {
-            String key = menuClasses[i] + ".checkIconOffset";
-            Object oldValue = table.get(key);
-            menuDefaults[j++] = key;
-            menuDefaults[j++] =
-                new XPValue(checkIconOffsetInteger, oldValue);
+            /* set checkIcon offset */
+            Integer checkIconOffsetInteger =
+                Integer.valueOf(0);
+            for (int i = 0, j = 0; i < menuClasses.length; i++) {
+                String key = menuClasses[i] + ".checkIconOffset";
+                Object oldValue = table.get(key);
+                menuDefaults[j++] = key;
+                menuDefaults[j++] =
+                    new XPValue(checkIconOffsetInteger, oldValue);
+            }
+            table.putDefaults(menuDefaults);
         }
-        table.putDefaults(menuDefaults);
-
         /* set width of the gap after check icon */
         Integer afterCheckIconGap = WindowsPopupMenuUI.getSpanBeforeGutter()
                 + WindowsPopupMenuUI.getGutterWidth()
--- a/jdk/src/java.desktop/share/classes/sun/awt/OSInfo.java	Fri Oct 02 10:29:36 2015 +0400
+++ b/jdk/src/java.desktop/share/classes/sun/awt/OSInfo.java	Fri Oct 02 17:12:47 2015 +0400
@@ -56,6 +56,7 @@
     public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1);
     public static final WindowsVersion WINDOWS_2003 = new WindowsVersion(5, 2);
     public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0);
+    public static final WindowsVersion WINDOWS_7 = new WindowsVersion(6, 1);
 
     private static final String OS_NAME = "os.name";
     private static final String OS_VERSION = "os.version";
@@ -70,6 +71,7 @@
         windowsVersionMap.put(WINDOWS_XP.toString(), WINDOWS_XP);
         windowsVersionMap.put(WINDOWS_2003.toString(), WINDOWS_2003);
         windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_VISTA);
+        windowsVersionMap.put(WINDOWS_VISTA.toString(), WINDOWS_7);
     }
 
     private static final PrivilegedAction<OSType> osTypeAction = new PrivilegedAction<OSType>() {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JMenu/8067346/bug8067346.java	Fri Oct 02 17:12:47 2015 +0400
@@ -0,0 +1,142 @@
+/*
+ * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/* @test
+ @bug 8067346
+ @summary Submenu has a changed offset on Windows7 with Windows look and feel
+ @requires (os.family == "windows")
+ @run main bug8067346
+ */
+import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
+import java.awt.Insets;
+import javax.swing.JFrame;
+import javax.swing.JMenu;
+import javax.swing.JMenuBar;
+import javax.swing.JMenuItem;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+
+
+public class bug8067346 {
+
+    private JMenuBar menuBar;
+    private JFrame frame;
+    private String[] menuClasses = {"MenuItem", "Menu",
+        "CheckBoxMenuItem", "RadioButtonMenuItem"};
+    private String MARGIN = ".margin";
+    private String CHECKICONOFFSET = ".checkIconOffset";
+    private static boolean runTest = true;
+
+    public static void main(String[] args) throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                bug8067346 test = new bug8067346();
+                try {
+                    // set windows look and feel
+                    UIManager.setLookAndFeel(new WindowsLookAndFeel());
+                } catch (UnsupportedLookAndFeelException e) {
+                    runTest = false;
+                }
+                if(runTest) {
+                    test.createUI();
+                    test.performTest();
+                    test.dispose();
+                }
+            }
+        });
+    }
+
+    public void createUI() {
+
+        frame = new JFrame();
+        menuBar = new JMenuBar();
+        frame.setJMenuBar(menuBar);
+        JMenu menu, submenu;
+        JMenuItem menuItem;
+
+        menu = new JMenu("A Menu");
+        menuBar.add(menu);
+        menu.addSeparator();
+
+        submenu = new JMenu("A submenu");
+
+        menuItem = new JMenuItem("An item in the submenu");
+        submenu.add(menuItem);
+        menu.add(submenu);
+    }
+
+    public void performTest() {
+        try {
+            String errorMessage = "Incorrect value for ";
+            StringBuilder errorMessageBuilder = new StringBuilder(errorMessage);
+            boolean error = false;
+            int retVal = testMargin();
+            if (retVal != 0) {
+                errorMessageBuilder.append(menuClasses[retVal])
+                        .append(MARGIN).append("\n");
+                error = true;
+            }
+            retVal = testCheckIconOffset();
+            if (retVal != 0) {
+                errorMessageBuilder.append(errorMessage)
+                        .append(menuClasses[retVal]).append(CHECKICONOFFSET);
+            }
+            if (error || retVal != 0) {
+                throw new RuntimeException(errorMessageBuilder.toString());
+            }
+        } finally {
+            dispose();
+        }
+    }
+
+    private int testMargin() {
+
+        for (int inx = 0; inx < menuClasses.length; inx++) {
+            Insets margin = (Insets) UIManager.get(menuClasses[inx] + MARGIN);
+            if (margin != null && margin.bottom == 0 && margin.left == 0
+                    && margin.right == 0 && margin.top == 0) {
+                return inx + 1;
+            }
+        }
+        return 0;
+    }
+
+    private int testCheckIconOffset() {
+
+        for (int inx = 0; inx < menuClasses.length; inx++) {
+            Object checkIconOffset = UIManager.get(menuClasses[inx]
+                    + CHECKICONOFFSET);
+            if (checkIconOffset != null && ((Integer) checkIconOffset) == 0) {
+                return inx + 1;
+            }
+        }
+        return 0;
+    }
+
+    public void dispose() {
+        frame.dispose();
+    }
+}