6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5
authormalenkov
Fri, 30 Jul 2010 19:21:27 +0400
changeset 6105 de4af6e215ad
parent 6104 f3aa980b09a0
child 6106 c5ba8eff9aca
6199676: REGRESSION: ColorChooser loses preview when change LandF in Java5 Reviewed-by: alexp, peterz
jdk/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java
jdk/test/javax/swing/JColorChooser/Test6199676.java
--- a/jdk/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java	Thu Jul 29 13:22:16 2010 -0700
+++ b/jdk/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java	Fri Jul 30 19:21:27 2010 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2010, 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
@@ -113,7 +113,6 @@
             previewPanelHolder.setBorder(new TitledBorder(previewString));
         }
         previewPanelHolder.setInheritsPopupMenu(true);
-        chooser.add(previewPanelHolder, BorderLayout.SOUTH);
 
         installPreviewPanel();
         chooser.applyComponentOrientation(c.getComponentOrientation());
@@ -126,13 +125,9 @@
 
         uninstallDefaultChoosers();
         uninstallListeners();
+        uninstallPreviewPanel();
         uninstallDefaults();
 
-        previewPanelHolder.remove(previewPanel);
-        if (previewPanel instanceof UIResource) {
-            chooser.setPreviewPanel(null);
-        }
-
         previewPanelHolder = null;
         previewPanel = null;
         defaultChoosers = null;
@@ -143,29 +138,37 @@
     }
 
     protected void installPreviewPanel() {
-        if (previewPanel != null) {
-            previewPanelHolder.remove(previewPanel);
-            previewPanel.removeMouseListener(getHandler());
+        JComponent previewPanel = this.chooser.getPreviewPanel();
+        if (previewPanel == null) {
+            previewPanel = ColorChooserComponentFactory.getPreviewPanel();
         }
-
-        previewPanel = chooser.getPreviewPanel();
-        Dimension layoutSize = new Dimension(); // fix for bug 4759306
-        if (previewPanel != null) {
-            layoutSize = new BorderLayout().minimumLayoutSize(previewPanel);
-            if ((previewPanelHolder != null) && (chooser != null) &&
-            (layoutSize.getWidth() + layoutSize.getHeight() == 0)) {
-              chooser.remove(previewPanelHolder);
-              return;
+        else {
+            Dimension size = new BorderLayout().minimumLayoutSize(previewPanel);
+            if ((size.width == 0) && (size.height == 0)) {
+                previewPanel = null;
             }
         }
-        if (previewPanel == null || previewPanel instanceof UIResource) {
-          previewPanel = ColorChooserComponentFactory.getPreviewPanel(); // get from table?
-            chooser.setPreviewPanel(previewPanel);
+        this.previewPanel = previewPanel;
+        if (previewPanel != null) {
+            chooser.add(previewPanelHolder, BorderLayout.SOUTH);
+            previewPanel.setForeground(chooser.getColor());
+            previewPanelHolder.add(previewPanel);
+            previewPanel.addMouseListener(getHandler());
+            previewPanel.setInheritsPopupMenu(true);
         }
-        previewPanel.setForeground(chooser.getColor());
-        previewPanelHolder.add(previewPanel);
-        previewPanel.addMouseListener(getHandler());
-        previewPanel.setInheritsPopupMenu(true);
+    }
+
+    /**
+     * Removes installed preview panel from the UI delegate.
+     *
+     * @since 1.7
+     */
+    protected void uninstallPreviewPanel() {
+        if (this.previewPanel != null) {
+            this.previewPanel.removeMouseListener(getHandler());
+            this.previewPanelHolder.remove(this.previewPanel);
+        }
+        this.chooser.remove(this.previewPanelHolder);
     }
 
     protected void installDefaults() {
@@ -209,7 +212,6 @@
         chooser.removePropertyChangeListener( propertyChangeListener );
         chooser.getSelectionModel().removeChangeListener(previewListener);
         previewListener = null;
-        previewPanel.removeMouseListener(getHandler());
     }
 
     private void selectionChanged(ColorSelectionModel model) {
@@ -312,9 +314,8 @@
                 }
             }
             else if (prop == JColorChooser.PREVIEW_PANEL_PROPERTY) {
-                if (evt.getNewValue() != previewPanel) {
-                    installPreviewPanel();
-                }
+                uninstallPreviewPanel();
+                installPreviewPanel();
             }
             else if (prop == JColorChooser.SELECTION_MODEL_PROPERTY) {
                 ColorSelectionModel oldModel = (ColorSelectionModel) evt.getOldValue();
@@ -352,5 +353,4 @@
             super("color");
         }
     }
-
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JColorChooser/Test6199676.java	Fri Jul 30 19:21:27 2010 +0400
@@ -0,0 +1,117 @@
+/*
+ * Copyright (c) 2010, 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 6199676
+ * @summary Tests preview panel after L&F changing
+ * @author Sergey Malenkov
+ */
+
+import java.awt.Component;
+import java.awt.Container;
+import javax.swing.JColorChooser;
+import javax.swing.JFrame;
+import javax.swing.JPanel;
+import javax.swing.SwingUtilities;
+import javax.swing.UIManager;
+import javax.swing.UIManager.LookAndFeelInfo;
+
+public class Test6199676 implements Runnable {
+    public static void main(String[] args) {
+        SwingUtilities.invokeLater(new Test6199676());
+    }
+
+    private static void exit(String error) {
+        if (error != null) {
+            System.err.println(error);
+            System.exit(1);
+        }
+        else {
+            System.exit(0);
+        }
+    }
+
+    private static Component getPreview(Container container) {
+        String name = "ColorChooser.previewPanelHolder";
+        for (Component component : container.getComponents()) {
+            if (!name.equals(component.getName())) {
+                component = (component instanceof Container)
+                        ? getPreview((Container) component)
+                        : null;
+            }
+            if (component instanceof Container) {
+                container = (Container) component;
+                return 1 == container.getComponentCount()
+                        ? container.getComponent(0)
+                        : null;
+            }
+        }
+        return null;
+    }
+
+    private static boolean isShowing(Component component) {
+        return (component != null) && component.isShowing();
+    }
+
+    private int index;
+    private boolean updated;
+    private JColorChooser chooser;
+
+    public synchronized void run() {
+        if (this.chooser == null) {
+            this.chooser = new JColorChooser();
+
+            JFrame frame = new JFrame(getClass().getName());
+            frame.add(this.chooser);
+            frame.setVisible(true);
+        }
+        else if (this.updated) {
+            if (isShowing(this.chooser.getPreviewPanel())) {
+                exit("custom preview panel is showing");
+            }
+            exit(null);
+        }
+        else {
+            Component component = this.chooser.getPreviewPanel();
+            if (component == null) {
+                component = getPreview(this.chooser);
+            }
+            if (!isShowing(component)) {
+                exit("default preview panel is not showing");
+            }
+            this.updated = true;
+            this.chooser.setPreviewPanel(new JPanel());
+        }
+        LookAndFeelInfo[] infos = UIManager.getInstalledLookAndFeels();
+        LookAndFeelInfo info = infos[++this.index % infos.length];
+        try {
+            UIManager.setLookAndFeel(info.getClassName());
+        }
+        catch (Exception exception) {
+            exit("could not change L&F");
+        }
+        SwingUtilities.updateComponentTreeUI(this.chooser);
+        SwingUtilities.invokeLater(this);
+    }
+}