提交 bd5a8fca 编写于 作者: M malenkov

6707406: BasicColorChooserUI tests throw NPE while getColorSelectionModel if...

6707406: BasicColorChooserUI tests throw NPE while getColorSelectionModel if isPropertyChanged() returns true
Reviewed-by: peterz, rupashka
上级 24f54a15
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -442,7 +442,10 @@ class GTKColorChooserPanel extends AbstractColorChooserPanel implements ...@@ -442,7 +442,10 @@ class GTKColorChooserPanel extends AbstractColorChooserPanel implements
} }
if (updateModel) { if (updateModel) {
getColorSelectionModel().setSelectedColor(color); ColorSelectionModel model = getColorSelectionModel();
if (model != null) {
model.setSelectedColor(color);
}
} }
triangle.setColor(hue, saturation, brightness); triangle.setColor(hue, saturation, brightness);
......
...@@ -160,7 +160,9 @@ public abstract class AbstractColorChooserPanel extends JPanel { ...@@ -160,7 +160,9 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* is editing * is editing
*/ */
public ColorSelectionModel getColorSelectionModel() { public ColorSelectionModel getColorSelectionModel() {
return chooser.getSelectionModel(); return (this.chooser != null)
? this.chooser.getSelectionModel()
: null;
} }
/** /**
...@@ -168,7 +170,17 @@ public abstract class AbstractColorChooserPanel extends JPanel { ...@@ -168,7 +170,17 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* @return the <code>Color</code> that is selected * @return the <code>Color</code> that is selected
*/ */
protected Color getColorFromModel() { 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);
}
} }
/** /**
......
...@@ -59,11 +59,13 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope ...@@ -59,11 +59,13 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope
@Override @Override
public void updateChooser() { public void updateChooser() {
Color color = getColorFromModel(); Color color = getColorFromModel();
if (color != null) {
this.panel.setColor(color); this.panel.setColor(color);
this.text.setValue(Integer.valueOf(color.getRGB())); this.text.setValue(Integer.valueOf(color.getRGB()));
this.slider.repaint(); this.slider.repaint();
this.diagram.repaint(); this.diagram.repaint();
} }
}
@Override @Override
protected void buildChooser() { protected void buildChooser() {
...@@ -157,10 +159,13 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope ...@@ -157,10 +159,13 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope
} }
public void propertyChange(PropertyChangeEvent event) { public void propertyChange(PropertyChangeEvent event) {
ColorSelectionModel model = getColorSelectionModel();
if (model != null) {
Object object = event.getNewValue(); Object object = event.getNewValue();
if (object instanceof Integer) { if (object instanceof Integer) {
int value = MASK & getColorFromModel().getRGB() | (Integer) object; int value = MASK & model.getSelectedColor().getRGB() | (Integer) object;
getColorSelectionModel().setSelectedColor(new Color(value, true)); model.setSelectedColor(new Color(value, true));
}
} }
this.text.selectAll(); this.text.selectAll();
} }
......
...@@ -138,7 +138,7 @@ final class ColorPanel extends JPanel implements ActionListener { ...@@ -138,7 +138,7 @@ final class ColorPanel extends JPanel implements ActionListener {
Object parent = getParent(); Object parent = getParent();
if (parent instanceof ColorChooserPanel) { if (parent instanceof ColorChooserPanel) {
ColorChooserPanel chooser = (ColorChooserPanel) parent; ColorChooserPanel chooser = (ColorChooserPanel) parent;
chooser.getColorSelectionModel().setSelectedColor(this.color); chooser.setSelectedColor(this.color);
chooser.repaint(); chooser.repaint();
} }
} }
......
/* /*
* 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. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -213,17 +213,15 @@ class DefaultSwatchChooserPanel extends AbstractColorChooserPanel { ...@@ -213,17 +213,15 @@ class DefaultSwatchChooserPanel extends AbstractColorChooserPanel {
class RecentSwatchListener extends MouseAdapter implements Serializable { class RecentSwatchListener extends MouseAdapter implements Serializable {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY()); Color color = recentSwatchPanel.getColorForLocation(e.getX(), e.getY());
getColorSelectionModel().setSelectedColor(color); setSelectedColor(color);
} }
} }
class MainSwatchListener extends MouseAdapter implements Serializable { class MainSwatchListener extends MouseAdapter implements Serializable {
public void mousePressed(MouseEvent e) { public void mousePressed(MouseEvent e) {
Color color = swatchPanel.getColorForLocation(e.getX(), e.getY()); Color color = swatchPanel.getColorForLocation(e.getX(), e.getY());
getColorSelectionModel().setSelectedColor(color); setSelectedColor(color);
recentSwatchPanel.setMostRecentColor(color); recentSwatchPanel.setMostRecentColor(color);
} }
} }
......
/*
* 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) {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册