KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > gui > propedit > ColorSelectorPane


1 package net.suberic.util.gui.propedit;
2 import net.suberic.util.gui.*;
3 import java.awt.Color JavaDoc;
4 import javax.swing.*;
5 import java.awt.event.*;
6
7
8 /**
9  * This displays the currently selected file (if any), along with a
10  * button which will bring up a JColorChooser to choose any other file(s).
11  *
12  * If property._enabledBox is set to true, then this also adds a
13  * checkbox to show whether or not to use this property, or just to use
14  * the defaults.
15  *
16  * Note that the value that gets set is actually property.rgb (which is
17  * the rgb value of the color selected), and, if the enabled checkbox is
18  * there, property.enabled. the property value itself is not set.
19  */

20
21 public class ColorSelectorPane extends LabelValuePropertyEditor {
22
23   JLabel label;
24   JButton inputButton;
25
26   int originalRgb = -1;
27   Color JavaDoc currentColor;
28
29   boolean useEnabledBox = false;
30   JCheckBox enabledBox = null;
31   boolean origEnabled = false;
32
33   /**
34    * @param propertyName The property to be edited.
35    * @param template The property that will define the layout of the
36    * editor.
37    * @param manager The PropertyEditorManager that will manage the
38    * changes.
39    */

40   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
41     property=propertyName;
42     manager=newManager;
43     editorTemplate = template;
44     propertyBase=propertyBaseName;
45
46     label = createLabel();
47
48     inputButton = createInputButton();
49
50     int defaultValue = inputButton.getBackground().getRGB();
51
52     originalValue = manager.getProperty(property + ".rgb", Integer.toString(defaultValue));
53     originalRgb = Integer.parseInt(originalValue);
54
55     setCurrentColor(new Color JavaDoc(originalRgb));
56
57     inputButton.setPreferredSize(new java.awt.Dimension JavaDoc(150, label.getMinimumSize().height));
58
59     this.add(label);
60     labelComponent = label;
61
62     JPanel tmpPanel = new JPanel();
63     SpringLayout layout = new SpringLayout();
64     tmpPanel.setLayout(layout);
65
66     tmpPanel.add(inputButton);
67     layout.putConstraint(SpringLayout.NORTH, inputButton, 0, SpringLayout.NORTH, tmpPanel);
68     layout.putConstraint(SpringLayout.WEST, inputButton, 0, SpringLayout.WEST, tmpPanel);
69     layout.putConstraint(SpringLayout.SOUTH, tmpPanel, 0, SpringLayout.SOUTH, inputButton);
70
71     useEnabledBox = manager.getProperty(editorTemplate + "._enabledBox", "false").equalsIgnoreCase("true");
72     if (useEnabledBox) {
73       enabledBox = new JCheckBox();
74       origEnabled = manager.getProperty(property + "._enabled", "false").equalsIgnoreCase("true");
75       enabledBox.setSelected(origEnabled);
76       enabledBox.addItemListener(new ItemListener() {
77           public void itemStateChanged(ItemEvent e) {
78             enabledBoxUpdated(enabledBox.isSelected());
79           }
80         });
81       tmpPanel.add(enabledBox);
82       layout.putConstraint(SpringLayout.WEST, enabledBox, 5, SpringLayout.EAST, inputButton);
83       layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, enabledBox);
84     } else {
85       layout.putConstraint(SpringLayout.EAST, tmpPanel, 5, SpringLayout.EAST, inputButton);
86     }
87
88     tmpPanel.setPreferredSize(new java.awt.Dimension JavaDoc(150, inputButton.getMinimumSize().height));
89     tmpPanel.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, inputButton.getMinimumSize().height));
90
91     valueComponent = tmpPanel;
92     this.add(tmpPanel);
93
94     updateEditorEnabled();
95
96     manager.registerPropertyEditor(property, this);
97   }
98
99   /**
100    * Creates a button that will bring up a way to select a new Color.
101    */

102   public JButton createInputButton() {
103     JButton newButton = new JButton();
104
105     newButton.addActionListener(new AbstractAction() {
106         public void actionPerformed(ActionEvent e) {
107           selectNewColor();
108         }
109       });
110
111     return newButton;
112   }
113
114   /**
115    * This actually brings up a JColorChooser to select a new Color for
116    * the value of the property.
117    */

118   public void selectNewColor() {
119     Color JavaDoc newColor = JColorChooser.showDialog(this, "title", currentColor);
120     if (newColor != null) {
121       setCurrentColor(newColor);
122     }
123   }
124
125   /**
126    * Sets the currently selected color to the given Color.
127    */

128   public void setCurrentColor(Color JavaDoc newColor) {
129     String JavaDoc newValue = Integer.toString(newColor.getRGB());
130     try {
131       if (currentColor != newColor) {
132         firePropertyChangingEvent(newValue);
133         firePropertyChangedEvent(newValue);
134         currentColor = newColor;
135         inputButton.setBackground(currentColor);
136       }
137     } catch (PropertyValueVetoException pvve) {
138       manager.getFactory().showError(this, "Error changing value " + label.getText() + " to " + newColor.toString() + ": " + pvve.getReason());
139
140     }
141   }
142
143   /**
144    * Returns the current color.
145    */

146   public Color JavaDoc getCurrentColor() {
147     return currentColor;
148   }
149
150   // as defined in net.suberic.util.gui.propedit.PropertyEditorUI
151

152
153   /**
154    * This writes the currently configured value in the PropertyEditorUI
155    * to the source VariableBundle.
156    */

157   public void setValue() {
158     if (isEditorEnabled() && isChanged()) {
159       manager.setProperty(property + ".rgb", Integer.toString(currentColor.getRGB()));
160       originalValue = Integer.toString(currentColor.getRGB());
161       if (useEnabledBox) {
162         if (enabledBox.isSelected())
163           manager.setProperty(property + "._enabled", "true");
164         else
165           manager.setProperty(property + "._enabled", "false");
166
167         origEnabled = enabledBox.isSelected();
168       }
169     }
170   }
171
172   public void validateProperty() throws PropertyValueVetoException {
173     if (isEditorEnabled()) {
174       firePropertyCommittingEvent(Integer.toString(currentColor.getRGB()));
175     }
176   }
177
178   /**
179    * Returns the current values of the edited properties as a
180    * java.util.Properties object.
181    */

182   public java.util.Properties JavaDoc getValue() {
183     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
184
185     retProps.setProperty(property + ".rgb", Integer.toString(currentColor.getRGB()));
186     if (useEnabledBox) {
187       if (enabledBox.isSelected())
188         retProps.setProperty(property + "._enabled", "true");
189       else
190         retProps.setProperty(property + "._enabled", "false");
191     }
192     return retProps;
193   }
194
195   /**
196    * This resets the editor to the original (or latest set, if setValue()
197    * has been called) value of the edited property.
198    */

199   public void resetDefaultValue() {
200     setCurrentColor(new Color JavaDoc(originalRgb));
201
202     if (useEnabledBox)
203       enabledBox.setSelected(origEnabled);
204   }
205
206   /**
207    * Returns whether or not we've changed the original setting.
208    */

209   public boolean isChanged() {
210     if (useEnabledBox) {
211       return (! (enabledBox.isSelected() == origEnabled && originalValue.equals(Integer.toString(currentColor.getRGB()))));
212     } else {
213       return (!(originalValue.equals(Integer.toString(currentColor.getRGB()))));
214     }
215   }
216
217   /**
218    * Run when the PropertyEditor may have changed enabled states.
219    */

220   protected void updateEditorEnabled() {
221     if (useEnabledBox) {
222       enabledBox.setEnabled(isEditorEnabled());
223       if (inputButton != null) {
224         inputButton.setEnabled(isEditorEnabled() && enabledBox.isSelected());
225       }
226     } else {
227       if (inputButton != null) {
228         inputButton.setEnabled(isEditorEnabled());
229       }
230     }
231   }
232
233   /**
234    * Called when the enabledBox's value is updated.
235    */

236   private void enabledBoxUpdated(boolean newValue) {
237     inputButton.setEnabled(newValue);
238   }
239
240 }
241
Popular Tags