4769772: JInternalFrame.setIcon(true) before JDesktopPane.add(JIF) causes wrong state
authorrchamyal
Wed, 13 Jan 2016 11:56:46 +0530
changeset 35670 320e424b168c
parent 35669 9ff296647fde
child 35671 20db05a43cb7
4769772: JInternalFrame.setIcon(true) before JDesktopPane.add(JIF) causes wrong state Reviewed-by: alexsch, aivanov
jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaInternalFrameManager.java
jdk/src/java.desktop/share/classes/javax/swing/JDesktopPane.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java
jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthDesktopPaneUI.java
jdk/test/javax/swing/JInternalFrame/4769772/TestJInternalFrameIconify.java
--- a/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaInternalFrameManager.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/jdk/src/java.desktop/macosx/classes/com/apple/laf/AquaInternalFrameManager.java	Wed Jan 13 11:56:46 2016 +0530
@@ -105,7 +105,9 @@
         // Position depends on *current* position of frame, unlike super which reuses the first position
         final Rectangle r = getBoundsForIconOf(f);
         desktopIcon.setBounds(r.x, r.y, r.width, r.height);
-
+        if (!wasIcon(f)) {
+            setWasIcon(f, Boolean.TRUE);
+        }
         c = f.getParent();
         if (c == null) return;
 
--- a/jdk/src/java.desktop/share/classes/javax/swing/JDesktopPane.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/JDesktopPane.java	Wed Jan 13 11:56:46 2016 +0530
@@ -480,6 +480,7 @@
      * @since 1.6
      */
     protected void addImpl(Component comp, Object constraints, int index) {
+        checkComponentAttributes(comp);
         super.addImpl(comp, constraints, index);
         if (componentOrderCheckingEnabled) {
             if (comp instanceof JInternalFrame ||
@@ -489,6 +490,12 @@
         }
     }
 
+    private void checkComponentAttributes(Component comp) {
+        if (comp instanceof JInternalFrame && ((JInternalFrame) comp).isIcon()) {
+            ((JInternalFrame) comp).putClientProperty("wasIconOnce", Boolean.FALSE);
+        }
+    }
+
     /**
      * {@inheritDoc}
      * @since 1.6
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java	Wed Jan 13 11:56:46 2016 +0530
@@ -1710,6 +1710,12 @@
                 } else {
                     parentBounds = null;
                 }
+                if ((frame.getParent() != null) && frame.isIcon()) {
+                    Boolean value = (Boolean) frame.getClientProperty("wasIconOnce");
+                    if (Boolean.FALSE.equals(value)) {
+                        iconifyFrame(frame);
+                    }
+                }
                 if ((frame.getParent() != null) && !componentListenerAdded) {
                     f.getParent().addComponentListener(componentListener);
                     componentListenerAdded = true;
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthDesktopPaneUI.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthDesktopPaneUI.java	Wed Jan 13 11:56:46 2016 +0530
@@ -351,12 +351,16 @@
             Container c = f.getParent();
             JDesktopPane d = f.getDesktopPane();
             boolean findNext = f.isSelected();
-
-            if (c == null) {
+            if (c == null || d == null) {
                 return;
             }
-
             desktopIcon = f.getDesktopIcon();
+            if (!wasIcon(f)) {
+                Rectangle r = getBoundsForIconOf(f);
+                desktopIcon.setBounds(r.x, r.y, r.width, r.height);
+                desktopIcon.revalidate();
+                setWasIcon(f, Boolean.TRUE);
+            }
 
             if (!f.isMaximum()) {
                 f.setNormalBounds(f.getBounds());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JInternalFrame/4769772/TestJInternalFrameIconify.java	Wed Jan 13 11:56:46 2016 +0530
@@ -0,0 +1,123 @@
+/*
+ * 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 4769772
+ * @summary JInternalFrame.setIcon(true) before JDesktopPane.add(JIF) causes wrong state
+ * @run main TestJInternalFrameIconify
+ */
+import java.beans.PropertyVetoException;
+import javax.swing.JFrame;
+import javax.swing.JDesktopPane;
+import javax.swing.JInternalFrame;
+import javax.swing.UIManager;
+import javax.swing.UnsupportedLookAndFeelException;
+import java.awt.Robot;
+import javax.swing.SwingUtilities;
+
+public class TestJInternalFrameIconify {
+
+    private static JDesktopPane desktopPane;
+    private static JFrame frame;
+    private static Robot robot;
+    private static volatile String errorMessage = "";
+
+    public static void main(String[] args) throws Exception {
+        robot = new java.awt.Robot();
+        UIManager.LookAndFeelInfo[] lookAndFeelArray
+                = UIManager.getInstalledLookAndFeels();
+        for (UIManager.LookAndFeelInfo lookAndFeelItem : lookAndFeelArray) {
+            String lookAndFeelString = lookAndFeelItem.getClassName();
+            if (tryLookAndFeel(lookAndFeelString)) {
+                createUI(lookAndFeelString);
+                robot.waitForIdle();
+                executeTest(lookAndFeelString);
+            }
+        }
+        if (!"".equals(errorMessage)) {
+            throw new RuntimeException(errorMessage);
+        }
+    }
+
+    private static boolean tryLookAndFeel(String lookAndFeelString) {
+        try {
+            UIManager.setLookAndFeel(lookAndFeelString);
+            return true;
+        } catch (UnsupportedLookAndFeelException | ClassNotFoundException |
+                InstantiationException | IllegalAccessException e) {
+            errorMessage += e.getMessage() + "\n";
+            System.err.println("Caught Exception: " + e.getMessage());
+            return false;
+        }
+    }
+
+    private static void createUI(String lookAndFeelString) throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+            @Override
+            public void run() {
+                frame = new JFrame(lookAndFeelString);
+                desktopPane = new JDesktopPane();
+                frame.getContentPane().add(desktopPane);
+                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+
+                JInternalFrame f = new JInternalFrame("Child ", true, true,
+                        true, true);
+                f.setSize(200, 300);
+                f.setLocation(20, 20);
+                try {
+                    f.setIcon(true);
+                } catch (PropertyVetoException ex) {
+                    errorMessage += ex.getMessage() + "\n";
+                }
+                desktopPane.add(f);
+                f.setVisible(true);
+
+                frame.setSize(500, 500);
+                frame.setLocationRelativeTo(null);
+                frame.setVisible(true);
+            }
+        });
+
+    }
+
+    private static void executeTest(String lookAndFeelString) throws Exception {
+        SwingUtilities.invokeAndWait(new Runnable() {
+
+            @Override
+            public void run() {
+                try {
+                    JInternalFrame internalFrames[]
+                            = desktopPane.getAllFrames();
+                    if (internalFrames[0].isShowing()) {
+                        errorMessage += "Test Failed for "
+                                + lookAndFeelString + " look and feel\n";
+                        System.err.println(errorMessage);
+                    }
+                } finally {
+                    frame.dispose();
+                }
+            }
+        });
+    }
+}