提交 3164b3db 编写于 作者: M malenkov

6348456: BasicColorChooserUI ignores JColorChooser selection model changes

Summary: Some methods are moved from AbstractColorChooserPanel to BasicColorChooserUI
Reviewed-by: peterz, alexp
上级 3abf54aa
/*
* 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
......@@ -54,17 +52,6 @@ public abstract class AbstractColorChooserPanel extends JPanel {
*/
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
......@@ -157,8 +144,6 @@ public abstract class AbstractColorChooserPanel extends JPanel {
chooser = enclosingChooser;
buildChooser();
updateChooser();
colorListener = new ModelListener();
getColorSelectionModel().addChangeListener(colorListener);
}
/**
......@@ -166,7 +151,6 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* If override this, be sure to call <code>super</code>.
*/
public void uninstallChooserPanel(JColorChooser enclosingChooser) {
getColorSelectionModel().removeChangeListener(colorListener);
chooser = null;
}
......@@ -192,10 +176,6 @@ public abstract class AbstractColorChooserPanel extends JPanel {
* @param g the <code>Graphics</code> object
*/
public void paint(Graphics g) {
if (dirty) {
updateChooser();
dirty = false;
}
super.paint(g);
}
......@@ -222,18 +202,4 @@ public abstract class AbstractColorChooserPanel extends JPanel {
}
return defaultValue;
}
/**
*
*/
class ModelListener implements ChangeListener, Serializable {
public void stateChanged(ChangeEvent e) {
if (isShowing()) { // isVisible
updateChooser();
dirty = false;
} else {
dirty = true;
}
}
}
}
/*
* 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.colorchooser.*;
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 @@ public class BasicColorChooserUI extends ColorChooserUI
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 @@ public class BasicColorChooserUI extends ColorChooserUI
// 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 @@ public class BasicColorChooserUI extends ColorChooserUI
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();
......
<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>
/*
* 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);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册