changeset 242:56cae54e668c

6348456: BasicColorChooserUI ignores JColorChooser selection model changes Summary: Some methods are moved from AbstractColorChooserPanel to BasicColorChooserUI Reviewed-by: peterz, alexp
author malenkov
date Wed, 07 May 2008 21:54:03 +0400
parents 4cf10bc1973d
children 5bcff22d837d
files src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java test/javax/swing/JColorChooser/Test6348456.html test/javax/swing/JColorChooser/Test6348456.java
diffstat 4 files changed, 99 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Wed May 07 20:26:32 2008 +0400
+++ b/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Wed May 07 21:54:03 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 1998-2001 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 1998-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
@@ -26,9 +26,7 @@
 package javax.swing.colorchooser;
 
 import java.awt.*;
-import java.io.Serializable;
 import javax.swing.*;
-import javax.swing.event.*;
 
 /**
  * This is the abstract superclass for color choosers.  If you want to add
@@ -55,17 +53,6 @@
     private JColorChooser chooser;
 
     /**
-     *
-     */
-    private ChangeListener colorListener;
-
-    /**
-     *
-     */
-    private boolean dirty  = true;
-
-
-    /**
       * Invoked automatically when the model's state changes.
       * It is also called by <code>installChooserPanel</code> to allow
       * you to set up the initial state of your chooser.
@@ -157,8 +144,6 @@
         chooser = enclosingChooser;
         buildChooser();
         updateChooser();
-        colorListener = new ModelListener();
-        getColorSelectionModel().addChangeListener(colorListener);
     }
 
     /**
@@ -166,7 +151,6 @@
      * If override this, be sure to call <code>super</code>.
      */
   public void uninstallChooserPanel(JColorChooser enclosingChooser) {
-        getColorSelectionModel().removeChangeListener(colorListener);
         chooser = null;
     }
 
@@ -192,10 +176,6 @@
      * @param g  the <code>Graphics</code> object
      */
     public void paint(Graphics g) {
-        if (dirty) {
-            updateChooser();
-            dirty = false;
-        }
         super.paint(g);
     }
 
@@ -222,18 +202,4 @@
         }
         return defaultValue;
     }
-
-    /**
-     *
-     */
-    class ModelListener implements ChangeListener, Serializable {
-        public void stateChanged(ChangeEvent e) {
-          if (isShowing()) {  // isVisible
-                updateChooser();
-                dirty = false;
-            } else {
-                dirty = true;
-            }
-        }
-    }
 }
--- a/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java	Wed May 07 20:26:32 2008 +0400
+++ b/src/share/classes/javax/swing/plaf/basic/BasicColorChooserUI.java	Wed May 07 21:54:03 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 1997-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
@@ -30,16 +30,12 @@
 import javax.swing.event.*;
 import javax.swing.border.*;
 import javax.swing.plaf.*;
-import java.util.*;
 import java.awt.*;
-import java.awt.image.*;
 import java.awt.event.*;
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
-import java.io.Serializable;
 
 import sun.swing.DefaultLookup;
-import sun.swing.UIAction;
 
 /**
  * Provides the basic look and feel for a JColorChooser.
@@ -212,9 +208,24 @@
     protected void uninstallListeners() {
         chooser.removePropertyChangeListener( propertyChangeListener );
         chooser.getSelectionModel().removeChangeListener(previewListener);
+        previewListener = null;
         previewPanel.removeMouseListener(getHandler());
     }
 
+    private void selectionChanged(ColorSelectionModel model) {
+        if (this.previewPanel != null) {
+            this.previewPanel.setForeground(model.getSelectedColor());
+            this.previewPanel.repaint();
+        }
+        AbstractColorChooserPanel[] panels = this.chooser.getChooserPanels();
+        if (panels != null) {
+            for (AbstractColorChooserPanel panel : panels) {
+                if (panel != null) {
+                    panel.updateChooser();
+                }
+            }
+        }
+    }
 
     private class Handler implements ChangeListener, MouseListener,
             PropertyChangeListener {
@@ -222,11 +233,7 @@
         // ChangeListener
         //
         public void stateChanged(ChangeEvent evt) {
-            ColorSelectionModel model = (ColorSelectionModel)evt.getSource();
-            if (previewPanel != null) {
-                previewPanel.setForeground(model.getSelectedColor());
-                previewPanel.repaint();
-            }
+            selectionChanged((ColorSelectionModel) evt.getSource());
         }
 
         //
@@ -302,13 +309,19 @@
                     newPanels[i].installChooserPanel(chooser);
                 }
             }
-
-            if (prop == JColorChooser.PREVIEW_PANEL_PROPERTY) {
+            else if (prop == JColorChooser.PREVIEW_PANEL_PROPERTY) {
                 if (evt.getNewValue() != previewPanel) {
                     installPreviewPanel();
                 }
             }
-            if (prop == "componentOrientation") {
+            else if (prop == JColorChooser.SELECTION_MODEL_PROPERTY) {
+                ColorSelectionModel oldModel = (ColorSelectionModel) evt.getOldValue();
+                oldModel.removeChangeListener(previewListener);
+                ColorSelectionModel newModel = (ColorSelectionModel) evt.getNewValue();
+                newModel.addChangeListener(previewListener);
+                selectionChanged(newModel);
+            }
+            else if (prop == "componentOrientation") {
                 ComponentOrientation o =
                     (ComponentOrientation)evt.getNewValue();
                 JColorChooser cc = (JColorChooser)evt.getSource();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/JColorChooser/Test6348456.html	Wed May 07 21:54:03 2008 +0400
@@ -0,0 +1,9 @@
+<html>
+<body>
+When applet starts, you'll see that the preview is white.
+When you swap models, you'll see that the preview color is changed.
+
+<applet width="500" height="400" code="Test6348456.class">
+</applet>
+</body>
+</html>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/JColorChooser/Test6348456.java	Wed May 07 21:54:03 2008 +0400
@@ -0,0 +1,63 @@
+/*
+ * 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 6348456
+ * @summary Tests model changing
+ * @author Sergey Malenkov
+ * @run applet/manual=yesno Test6348456.html
+ */
+
+import java.awt.BorderLayout;
+import java.awt.Color;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import javax.swing.JApplet;
+import javax.swing.JButton;
+import javax.swing.JColorChooser;
+import javax.swing.colorchooser.DefaultColorSelectionModel;
+
+public final class Test6348456 extends JApplet implements ActionListener {
+
+    private static final DefaultColorSelectionModel WHITE = new DefaultColorSelectionModel(Color.WHITE);
+    private static final DefaultColorSelectionModel BLACK = new DefaultColorSelectionModel(Color.BLACK);
+
+    private JColorChooser chooser;
+
+    @Override
+    public void init() {
+        JButton button = new JButton("Swap models");
+        button.addActionListener(this);
+
+        this.chooser = new JColorChooser(Color.RED);
+        this.chooser.setSelectionModel(WHITE);
+
+        add(BorderLayout.NORTH, button);
+        add(BorderLayout.CENTER, this.chooser);
+    }
+
+    public void actionPerformed(ActionEvent event){
+        this.chooser.setSelectionModel(this.chooser.getSelectionModel() == BLACK ? WHITE : BLACK);
+    }
+}