001: package net.suberic.util.gui.propedit;
002:
003: import net.suberic.util.gui.*;
004: import java.awt.Color;
005: import javax.swing.*;
006: import java.awt.event.*;
007:
008: /**
009: * This displays the currently selected file (if any), along with a
010: * button which will bring up a JColorChooser to choose any other file(s).
011: *
012: * If property._enabledBox is set to true, then this also adds a
013: * checkbox to show whether or not to use this property, or just to use
014: * the defaults.
015: *
016: * Note that the value that gets set is actually property.rgb (which is
017: * the rgb value of the color selected), and, if the enabled checkbox is
018: * there, property.enabled. the property value itself is not set.
019: */
020:
021: public class ColorSelectorPane extends SwingPropertyEditor {
022:
023: JLabel label;
024: JButton inputButton;
025:
026: int originalRgb = -1;
027: Color currentColor;
028:
029: boolean useEnabledBox = false;
030: JCheckBox enabledBox = null;
031: boolean origEnabled = false;
032:
033: /**
034: * @param propertyName The property to be edited.
035: * @param template The property that will define the layout of the
036: * editor.
037: * @param manager The PropertyEditorManager that will manage the
038: * changes.
039: * @param isEnabled Whether or not this editor is enabled by default.
040: */
041: public void configureEditor(String propertyName, String template,
042: PropertyEditorManager newManager, boolean isEnabled) {
043: property = propertyName;
044: manager = newManager;
045: editorTemplate = template;
046:
047: label = createLabel();
048:
049: inputButton = createInputButton();
050:
051: int defaultValue = inputButton.getBackground().getRGB();
052:
053: originalValue = manager.getProperty(property + ".rgb", Integer
054: .toString(defaultValue));
055: originalRgb = Integer.parseInt(originalValue);
056:
057: setCurrentColor(new Color(originalRgb));
058:
059: inputButton.setPreferredSize(new java.awt.Dimension(150, label
060: .getMinimumSize().height));
061:
062: this .add(label);
063: labelComponent = label;
064:
065: JPanel tmpPanel = new JPanel(new java.awt.FlowLayout(
066: java.awt.FlowLayout.LEFT, 0, 0));
067: tmpPanel.add(inputButton);
068:
069: useEnabledBox = manager.getProperty(
070: editorTemplate + "._enabledBox", "false")
071: .equalsIgnoreCase("true");
072: if (useEnabledBox) {
073: enabledBox = new JCheckBox();
074: origEnabled = manager.getProperty(property + "._enabled",
075: "false").equalsIgnoreCase("true");
076: enabledBox.setSelected(origEnabled);
077: enabledBox.addItemListener(new ItemListener() {
078: public void itemStateChanged(ItemEvent e) {
079: enabledBoxUpdated(enabledBox.isSelected());
080: }
081: });
082: tmpPanel.add(enabledBox);
083: }
084:
085: valueComponent = tmpPanel;
086: this .add(tmpPanel);
087:
088: this .setEnabled(isEnabled);
089:
090: manager.registerPropertyEditor(property, this );
091: }
092:
093: /**
094: * Creates a button that will bring up a way to select a new Color.
095: */
096: public JButton createInputButton() {
097: JButton newButton = new JButton();
098:
099: newButton.addActionListener(new AbstractAction() {
100: public void actionPerformed(ActionEvent e) {
101: selectNewColor();
102: }
103: });
104:
105: return newButton;
106: }
107:
108: /**
109: * This actually brings up a JColorChooser to select a new Color for
110: * the value of the property.
111: */
112: public void selectNewColor() {
113: Color newColor = JColorChooser.showDialog(this , "title",
114: currentColor);
115: if (newColor != null) {
116: setCurrentColor(newColor);
117: }
118: }
119:
120: /**
121: * Sets the currently selected color to the given Color.
122: */
123: public void setCurrentColor(Color newColor) {
124: String newValue = Integer.toString(newColor.getRGB());
125: try {
126: if (currentColor != newColor) {
127: firePropertyChangingEvent(newValue);
128: firePropertyChangedEvent(newValue);
129: currentColor = newColor;
130: inputButton.setBackground(currentColor);
131: }
132: } catch (PropertyValueVetoException pvve) {
133: manager.getFactory().showError(
134: this ,
135: "Error changing value " + label.getText() + " to "
136: + newColor.toString() + ": "
137: + pvve.getReason());
138:
139: }
140: }
141:
142: /**
143: * Returns the current color.
144: */
145: public Color getCurrentColor() {
146: return currentColor;
147: }
148:
149: // as defined in net.suberic.util.gui.propedit.PropertyEditorUI
150:
151: /**
152: * This writes the currently configured value in the PropertyEditorUI
153: * to the source VariableBundle.
154: */
155: public void setValue() {
156: if (isEnabled() && isChanged()) {
157: manager.setProperty(property + ".rgb", Integer
158: .toString(currentColor.getRGB()));
159: if (useEnabledBox) {
160: if (enabledBox.isSelected())
161: manager.setProperty(property + "._enabled", "true");
162: else
163: manager
164: .setProperty(property + "._enabled",
165: "false");
166: }
167: }
168: }
169:
170: /**
171: * Returns the current values of the edited properties as a
172: * java.util.Properties object.
173: */
174: public java.util.Properties getValue() {
175: java.util.Properties retProps = new java.util.Properties();
176:
177: retProps.setProperty(property + ".rgb", Integer
178: .toString(currentColor.getRGB()));
179: if (useEnabledBox) {
180: if (enabledBox.isSelected())
181: retProps.setProperty(property + "._enabled", "true");
182: else
183: retProps.setProperty(property + "._enabled", "false");
184: }
185: return retProps;
186: }
187:
188: /**
189: * This resets the editor to the original (or latest set, if setValue()
190: * has been called) value of the edited property.
191: */
192: public void resetDefaultValue() {
193: setCurrentColor(new Color(originalRgb));
194:
195: if (useEnabledBox)
196: enabledBox.setSelected(origEnabled);
197: }
198:
199: /**
200: * Returns whether or not we've changed the original setting.
201: */
202: public boolean isChanged() {
203: if (useEnabledBox) {
204: return (!(enabledBox.isSelected() == origEnabled && originalValue
205: .equals(Integer.toString(currentColor.getRGB()))));
206: } else {
207: return (!(originalValue.equals(Integer
208: .toString(currentColor.getRGB()))));
209: }
210: }
211:
212: /**
213: * Sets whether or not this editor is enabled.
214: */
215: public void setEnabled(boolean newValue) {
216: if (useEnabledBox) {
217: enabledBox.setEnabled(newValue);
218: if (inputButton != null) {
219: inputButton.setEnabled(newValue
220: && enabledBox.isSelected());
221: }
222: } else {
223: if (inputButton != null) {
224: inputButton.setEnabled(newValue);
225: }
226: }
227: enabled = newValue;
228: }
229:
230: /**
231: * Called when the enabledBox's value is updated.
232: */
233: private void enabledBoxUpdated(boolean newValue) {
234: inputButton.setEnabled(newValue);
235: }
236:
237: }
|