changeset 612:91722ce41508

6707406: BasicColorChooserUI tests throw NPE while getColorSelectionModel if isPropertyChanged() returns true Reviewed-by: peterz, rupashka
author malenkov
date Thu, 24 Jul 2008 17:30:59 +0400
parents b334982cdefe
children a2266940ae33
files src/share/classes/com/sun/java/swing/plaf/gtk/GTKColorChooserPanel.java src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java src/share/classes/javax/swing/colorchooser/ColorPanel.java src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java test/javax/swing/JColorChooser/Test6707406.java
diffstat 6 files changed, 99 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/java/swing/plaf/gtk/GTKColorChooserPanel.java	Thu Jul 24 16:43:36 2008 +0400
+++ b/src/share/classes/com/sun/java/swing/plaf/gtk/GTKColorChooserPanel.java	Thu Jul 24 17:30:59 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2004 Sun Microsystems, Inc.  All Rights Reserved.
+ * Copyright 2002-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
@@ -442,7 +442,10 @@
         }
 
         if (updateModel) {
-            getColorSelectionModel().setSelectedColor(color);
+            ColorSelectionModel model = getColorSelectionModel();
+            if (model != null) {
+                model.setSelectedColor(color);
+            }
         }
 
         triangle.setColor(hue, saturation, brightness);
--- a/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Thu Jul 24 16:43:36 2008 +0400
+++ b/src/share/classes/javax/swing/colorchooser/AbstractColorChooserPanel.java	Thu Jul 24 17:30:59 2008 +0400
@@ -160,7 +160,9 @@
       *         is editing
       */
     public ColorSelectionModel getColorSelectionModel() {
-        return chooser.getSelectionModel();
+        return (this.chooser != null)
+                ? this.chooser.getSelectionModel()
+                : null;
     }
 
     /**
@@ -168,7 +170,17 @@
      * @return the <code>Color</code> that is selected
      */
     protected Color getColorFromModel() {
-        return getColorSelectionModel().getSelectedColor();
+        ColorSelectionModel model = getColorSelectionModel();
+        return (model != null)
+                ? model.getSelectedColor()
+                : null;
+    }
+
+    void setSelectedColor(Color color) {
+        ColorSelectionModel model = getColorSelectionModel();
+        if (model != null) {
+            model.setSelectedColor(color);
+        }
     }
 
     /**
--- a/src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java	Thu Jul 24 16:43:36 2008 +0400
+++ b/src/share/classes/javax/swing/colorchooser/ColorChooserPanel.java	Thu Jul 24 17:30:59 2008 +0400
@@ -59,10 +59,12 @@
     @Override
     public void updateChooser() {
         Color color = getColorFromModel();
-        this.panel.setColor(color);
-        this.text.setValue(Integer.valueOf(color.getRGB()));
-        this.slider.repaint();
-        this.diagram.repaint();
+        if (color != null) {
+            this.panel.setColor(color);
+            this.text.setValue(Integer.valueOf(color.getRGB()));
+            this.slider.repaint();
+            this.diagram.repaint();
+        }
     }
 
     @Override
@@ -157,10 +159,13 @@
     }
 
     public void propertyChange(PropertyChangeEvent event) {
-        Object object = event.getNewValue();
-        if (object instanceof Integer) {
-            int value = MASK & getColorFromModel().getRGB() | (Integer) object;
-            getColorSelectionModel().setSelectedColor(new Color(value, true));
+        ColorSelectionModel model = getColorSelectionModel();
+        if (model != null) {
+            Object object = event.getNewValue();
+            if (object instanceof Integer) {
+                int value = MASK & model.getSelectedColor().getRGB() | (Integer) object;
+                model.setSelectedColor(new Color(value, true));
+            }
         }
         this.text.selectAll();
     }
--- a/src/share/classes/javax/swing/colorchooser/ColorPanel.java	Thu Jul 24 16:43:36 2008 +0400
+++ b/src/share/classes/javax/swing/colorchooser/ColorPanel.java	Thu Jul 24 17:30:59 2008 +0400
@@ -138,7 +138,7 @@
         Object parent = getParent();
         if (parent instanceof ColorChooserPanel) {
             ColorChooserPanel chooser = (ColorChooserPanel) parent;
-            chooser.getColorSelectionModel().setSelectedColor(this.color);
+            chooser.setSelectedColor(this.color);
             chooser.repaint();
         }
     }
--- a/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java	Thu Jul 24 16:43:36 2008 +0400
+++ b/src/share/classes/javax/swing/colorchooser/DefaultSwatchChooserPanel.java	Thu Jul 24 17:30:59 2008 +0400
@@ -1,5 +1,5 @@
 /*
- * Copyright 1998-2005 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
@@ -213,17 +213,15 @@
     class RecentSwatchListener extends MouseAdapter implements Serializable {
         public void mousePressed(MouseEvent e) {
             Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
-            getColorSelectionModel().setSelectedColor(color);
-
+            setSelectedColor(color);
         }
     }
 
     class MainSwatchListener extends MouseAdapter implements Serializable {
         public void mousePressed(MouseEvent e) {
             Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
-            getColorSelectionModel().setSelectedColor(color);
+            setSelectedColor(color);
             recentSwatchPanel.setMostRecentColor(color);
-
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/swing/JColorChooser/Test6707406.java	Thu Jul 24 17:30:59 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 6707406
+ * @summary Tests color chooser with invalid UI
+ * @author Sergey Malenkov
+ */
+
+import java.awt.Color;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import javax.swing.JColorChooser;
+import javax.swing.UIManager;
+import javax.swing.UIManager.LookAndFeelInfo;
+import javax.swing.plaf.basic.BasicColorChooserUI;
+
+public class Test6707406 extends BasicColorChooserUI implements PropertyChangeListener {
+    public static void main(String[] args) throws Exception {
+        test();
+        for (LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
+            System.out.println(laf.getName());
+            UIManager.setLookAndFeel(laf.getClassName());
+            test();
+        }
+    }
+
+    private static void test() {
+        JColorChooser chooser = new JColorChooser();
+        chooser.getUI().uninstallUI(chooser);
+        new Test6707406().installUI(chooser);
+        chooser.getSelectionModel().setSelectedColor(Color.BLUE);
+    }
+
+    @Override
+    protected PropertyChangeListener createPropertyChangeListener() {
+        return this;
+    }
+
+    public void propertyChange(PropertyChangeEvent event) {
+    }
+}