changeset 13632:b83c88388896

4769772: JInternalFrame.setIcon(true) before JDesktopPane.add(JIF) causes wrong state Reviewed-by: alexsch, aivanov
author rchamyal
date Wed, 13 Jan 2016 11:56:46 +0530
parents dfb7f209f1a2
children 6ccec088c302
files src/java.desktop/macosx/classes/com/apple/laf/AquaInternalFrameManager.java src/java.desktop/share/classes/javax/swing/JDesktopPane.java src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java src/java.desktop/share/classes/javax/swing/plaf/synth/SynthDesktopPaneUI.java test/javax/swing/JInternalFrame/4769772/TestJInternalFrameIconify.java
diffstat 5 files changed, 146 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.desktop/macosx/classes/com/apple/laf/AquaInternalFrameManager.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/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/src/java.desktop/share/classes/javax/swing/JDesktopPane.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/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/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicInternalFrameUI.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/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/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthDesktopPaneUI.java	Tue Jan 12 13:13:16 2016 -0800
+++ b/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/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();
+                }
+            }
+        });
+    }
+}