提交 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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -442,7 +442,10 @@ class GTKColorChooserPanel extends AbstractColorChooserPanel implements
}
if (updateModel) {
getColorSelectionModel().setSelectedColor(color);
ColorSelectionModel model = getColorSelectionModel();
if (model != null) {
model.setSelectedColor(color);
}
}
triangle.setColor(hue, saturation, brightness);
......
......@@ -160,7 +160,9 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* is editing
*/
public ColorSelectionModel getColorSelectionModel() {
return chooser.getSelectionModel();
return (this.chooser != null)
? this.chooser.getSelectionModel()
: null;
}
/**
......@@ -168,7 +170,17 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* @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);
}
}
/**
......
......@@ -59,10 +59,12 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope
@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 @@ final class ColorChooserPanel extends AbstractColorChooserPanel implements Prope
}
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();
}
......
......@@ -138,7 +138,7 @@ final class ColorPanel extends JPanel implements ActionListener {
Object parent = getParent();
if (parent instanceof ColorChooserPanel) {
ColorChooserPanel chooser = (ColorChooserPanel) parent;
chooser.getColorSelectionModel().setSelectedColor(this.color);
chooser.setSelectedColor(this.color);
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.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -213,17 +213,15 @@ class DefaultSwatchChooserPanel extends AbstractColorChooserPanel {
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);
}
}
......
/*
* 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.
先完成此消息的编辑!
想要评论请 注册