Skip to content

Instantly share code, notes, and snippets.

@lebesnec
Created January 26, 2009 09:23
Show Gist options
  • Save lebesnec/52763 to your computer and use it in GitHub Desktop.
Save lebesnec/52763 to your computer and use it in GitHub Desktop.
package ilist.ui;
import ilist.ui.generic.components.TransparentPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import org.jdesktop.jxlayer.JXLayer;
import org.jdesktop.jxlayer.plaf.ext.LockableUI;
import com.bric.swing.ColorPicker;
/**
* a popup with a color wheel which allow to change the color of part of the main frame
* @author Christophe Le Besnerais
* {@link http://swing-fx.blogspot.com/2009/01/jrating-jtagcloud-and-gui-colorization.html}
*/
@SuppressWarnings("serial")
public class UIColorChooserPanel extends TransparentPanel {
// the selected color :
private Color color = Color.RED;
public UIColorChooserPanel(Frame owner) {
super(owner);
this.setSize(new Dimension(230, 250));
this.setTitle("Edit item");
JPanel panel = new JPanel(true);
panel.setOpaque(false);
panel.add(Box.createRigidArea(new Dimension(150, 5)));
final ColorPicker colorPicker = new ColorPicker(false);
colorPicker.setBorder(new EmptyBorder(0, 0, 0,0));
colorPicker.getColorPanel().setBorder(new EmptyBorder(0, 0, 0,0));
colorPicker.setOpaque(false);
colorPicker.getColorPanel().setOpaque(false);
((JComponent) colorPicker.getComponents()[1]).setOpaque(false);
colorPicker.getColorPanel().setRGB(color.getRed(), color.getGreen(), color.getBlue());
colorPicker.setPreferredSize(new Dimension(190, 150));
colorPicker.addPropertyChangeListener(ColorPicker.SELECTED_COLOR_PROPERTY, new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent e) {
if (colorPicker.getColor() != null)
color = colorPicker.getColor();
}
});
colorPicker.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(colorPicker);
getContent().add(panel, BorderLayout.CENTER);
}
@Override
public void setVisible(boolean visible) {
super.setVisible(visible);
if (visible) {
// when this popup is show, create a layer which block user interaction
// on the main frame, and detect mouse click :
MouseEventFilterLayerUI ui = new MouseEventFilterLayerUI();
MainFrame.getInstance().getMainLayer().setUI(ui);
}
}
@Override
public void dispose() {
super.dispose();
// remove the layer when closing :
MainFrame.getInstance().getMainLayer().setUI(null);
}
private class MouseEventFilterLayerUI extends LockableUI {
@Override
protected void processMouseEvent(MouseEvent e, JXLayer l) {
if (e.getID() == MouseEvent.MOUSE_RELEASED && e.getButton() == MouseEvent.BUTTON1) {
// left click on the main frame :
Point p = SwingUtilities.convertPoint(e.getComponent(), e.getPoint(), l.getView());
setColorAt(color, p, l.getView());
}
e.consume();
}
/**
* set the client property "color" to color for all sub-components
* of c which are at the position p
*/
private void setColorAt(Color color, Point p, JComponent c) {
for (Component component : c.getComponents()) {
Point p2 = SwingUtilities.convertPoint(c, p, component);
if (component instanceof JComponent && component.contains(p2)) {
JComponent jc = (JComponent) component;
jc.putClientProperty("color", color);
setColorAt(color, p2, jc);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment