4222508: JColorChooser ignores setEnabled() function call
authormalenkov
Fri, 23 Jan 2009 18:31:08 +0300
changeset 1849 b7b930157ae5
parent 1848 6ecbe9158c6e
child 1850 be2da26d2b21
4222508: JColorChooser ignores setEnabled() function call Reviewed-by: peterz, rupashka
jdk/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java
jdk/src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java
jdk/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java
jdk/test/javax/swing/JColorChooser/Test4222508.html
jdk/test/javax/swing/JColorChooser/Test4222508.java
--- a/jdk/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Wed Jan 21 13:58:46 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Fri Jan 23 18:31:08 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.colorchooser;
 
 import java.awt.*;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import javax.swing.*;
 
 /**
@@ -47,6 +49,15 @@
  */
 public abstract class AbstractColorChooserPanel extends JPanel {
 
+    private final PropertyChangeListener enabledListener = new PropertyChangeListener() {
+        public void propertyChange(PropertyChangeEvent event) {
+            Object value = event.getNewValue();
+            if (value instanceof Boolean) {
+                setEnabled((Boolean) value);
+            }
+        }
+    };
+
     /**
      *
      */
@@ -142,6 +153,8 @@
             throw new RuntimeException ("This chooser panel is already installed");
         }
         chooser = enclosingChooser;
+        chooser.addPropertyChangeListener("enabled", enabledListener);
+        setEnabled(chooser.isEnabled());
         buildChooser();
         updateChooser();
     }
@@ -151,6 +164,7 @@
      * If override this, be sure to call <code>super</code>.
      */
   public void uninstallChooserPanel(JColorChooser enclosingChooser) {
+        chooser.removePropertyChangeListener("enabled", enabledListener);
         chooser = null;
     }
 
--- a/jdk/src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java	Wed Jan 21 13:58:46 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java	Fri Jan 23 18:31:08 2009 +0300
@@ -26,6 +26,8 @@
 package javax.swing.colorchooser;
 
 import java.awt.Color;
+import java.awt.Component;
+import java.awt.Container;
 import java.awt.GridBagConstraints;
 import java.awt.GridBagLayout;
 import java.beans.PropertyChangeEvent;
@@ -57,6 +59,21 @@
     }
 
     @Override
+    public void setEnabled(boolean enabled) {
+        super.setEnabled(enabled);
+        setEnabled(this, enabled);
+    }
+
+    private static void setEnabled(Container container, boolean enabled) {
+        for (Component component : container.getComponents()) {
+            component.setEnabled(enabled);
+            if (component instanceof Container) {
+                setEnabled((Container) component, enabled);
+            }
+        }
+    }
+
+    @Override
     public void updateChooser() {
         Color color = getColorFromModel();
         if (color != null) {
--- a/jdk/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java	Wed Jan 21 13:58:46 2009 -0800
+++ b/jdk/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java	Fri Jan 23 18:31:08 2009 +0300
@@ -170,7 +170,6 @@
         superHolder.add(mainHolder, gbc);
         gbc.insets = oldInsets;
 
-        recentSwatchPanel.addMouseListener(recentSwatchListener);
         recentSwatchPanel.setInheritsPopupMenu(true);
         JPanel recentHolder = new JPanel( new BorderLayout() );
         recentHolder.setBorder(border);
@@ -212,16 +211,20 @@
 
     class RecentSwatchListener extends MouseAdapter implements Serializable {
         public void mousePressed(MouseEvent e) {
-            Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
-            setSelectedColor(color);
+            if (isEnabled()) {
+                Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
+                setSelectedColor(color);
+            }
         }
     }
 
     class MainSwatchListener extends MouseAdapter implements Serializable {
         public void mousePressed(MouseEvent e) {
-            Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
-            setSelectedColor(color);
-            recentSwatchPanel.setMostRecentColor(color);
+            if (isEnabled()) {
+                Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
+                setSelectedColor(color);
+                recentSwatchPanel.setMostRecentColor(color);
+            }
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JColorChooser/Test4222508.html	Fri Jan 23 18:31:08 2009 +0300
@@ -0,0 +1,9 @@
+<html>
+<body>
+Use the check box above the color chooser to disable it.
+You could not choose a color using by the disable color chooser.
+
+<applet width="600" height="400" code="Test4222508.class">
+</applet>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/JColorChooser/Test4222508.java	Fri Jan 23 18:31:08 2009 +0300
@@ -0,0 +1,56 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 4222508
+ * @summary Tests the color chooser disabling
+ * @author Sergey Malenkov
+ * @run applet/manual=yesno Test4222508.html
+ */
+
+import java.awt.BorderLayout;
+import java.awt.event.ItemEvent;
+import java.awt.event.ItemListener;
+import javax.swing.JApplet;
+import javax.swing.JCheckBox;
+import javax.swing.JColorChooser;
+
+public final class Test4222508 extends JApplet implements ItemListener {
+
+    private JCheckBox checkbox;
+    private JColorChooser chooser;
+
+    @Override
+    public void init() {
+        this.chooser = new JColorChooser();
+        this.checkbox = new JCheckBox("Enable the color chooser below", true);
+        this.checkbox.addItemListener(this);
+        add(BorderLayout.NORTH, this.checkbox);
+        add(BorderLayout.CENTER, this.chooser);
+    }
+
+    public void itemStateChanged(ItemEvent event) {
+        this.chooser.setEnabled(this.checkbox.isSelected());
+    }
+}