01: package net.suberic.pooka.gui.filter;
02:
03: import javax.swing.*;
04: import java.awt.*;
05:
06: /**
07: * This class allows you to choose colors for a ColorFilter.
08: */
09: public class ColorFilterEditor extends FilterEditor implements
10: java.awt.event.ActionListener {
11: JButton colorButton;
12: Color currentColor;
13: int originalRgb = NO_VALUE;
14:
15: public static int NO_VALUE = -1;
16:
17: public static String FILTER_CLASS = "net.suberic.pooka.gui.filter.ColorDisplayFilter";
18:
19: /**
20: * Configures the given FilterEditor from the given VariableBundle and
21: * property.
22: */
23: public void configureEditor(
24: net.suberic.util.gui.propedit.PropertyEditorManager newManager,
25: String propertyName) {
26: property = propertyName;
27: manager = newManager;
28:
29: String origRgbString = manager.getProperty(propertyName
30: + ".rgb", Integer.toString(NO_VALUE));
31: originalRgb = Integer.parseInt(origRgbString);
32:
33: colorButton = new JButton();
34: if (originalRgb != NO_VALUE) {
35: setCurrentColor(new Color(originalRgb));
36: } else {
37: setCurrentColor(Color.blue);
38: }
39: colorButton.addActionListener(this );
40: this .add(colorButton);
41: }
42:
43: public void setCurrentColor(Color newColor) {
44: currentColor = newColor;
45: colorButton.setBackground(currentColor);
46: }
47:
48: public Color getCurrentColor() {
49: return currentColor;
50: }
51:
52: /**
53: * Shows a dialog for choosing a new color when this is selected.
54: */
55: public void actionPerformed(java.awt.event.ActionEvent e) {
56: Color newColor = JColorChooser.showDialog(this , "title",
57: currentColor);
58: if (newColor != null)
59: setCurrentColor(newColor);
60: }
61:
62: /**
63: * Gets the values that would be set by this FilterEditor.
64: */
65: public java.util.Properties getValue() {
66: java.util.Properties props = new java.util.Properties();
67: props.setProperty(property + ".rgb", Integer
68: .toString(currentColor.getRGB()));
69: props.setProperty(property + ".class", FILTER_CLASS);
70: return props;
71: }
72:
73: /**
74: * Sets the values represented by this FilterEditor in the manager.
75: */
76: public void setValue() {
77: int newValue = currentColor.getRGB();
78: if (newValue != originalRgb)
79: manager.setProperty(property + ".rgb", Integer
80: .toString(newValue));
81:
82: String oldClassName = manager.getProperty(property + ".class",
83: "");
84: if (!oldClassName.equals(FILTER_CLASS))
85: manager.setProperty(property + ".class", FILTER_CLASS);
86: }
87:
88: /**
89: * Returns the class that will be set for this FilterEditor.
90: */
91: public String getFilterClassValue() {
92: return FILTER_CLASS;
93: }
94:
95: }
|