001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import javax.swing.event.*;
005: import net.suberic.util.*;
006: import java.awt.FlowLayout;
007:
008: /**
009: * This is a Swing implemenation of a boolean PropertyEditorUI.
010: */
011: public class BooleanEditorPane extends SwingPropertyEditor {
012: JCheckBox inputField;
013: JLabel label;
014: boolean originalBoolean = false;
015:
016: /**
017: * @param propertyName The property to be edited.
018: * @param template The property that will define the layout of the
019: * editor.
020: * @param manager The PropertyEditorManager that will manage the
021: * changes.
022: * @param isEnabled Whether or not this editor is enabled by default.
023: */
024: public void configureEditor(String propertyName, String template,
025: PropertyEditorManager newManager, boolean isEnabled) {
026: debug = newManager.getProperty("editors.debug", "false")
027: .equalsIgnoreCase("true");
028:
029: property = propertyName;
030: editorTemplate = template;
031: manager = newManager;
032:
033: if (debug) {
034: System.out
035: .println("configuring Boolean editor with property "
036: + propertyName
037: + ", editorTemplate "
038: + editorTemplate);
039: }
040:
041: originalValue = manager.getProperty(property, manager
042: .getProperty(template, "false"));
043: originalBoolean = originalValue.equalsIgnoreCase("true");
044:
045: if (debug) {
046: System.out.println("configuring with value getProperty("
047: + property + ", manager.getProperty(" + template
048: + ", \"false\")) = " + originalBoolean);
049: }
050:
051: label = createLabel();
052:
053: inputField = new JCheckBox();
054:
055: inputField.setSelected(originalBoolean);
056:
057: inputField.addChangeListener(new ChangeListener() {
058: public void stateChanged(ChangeEvent e) {
059: String newValue;
060: if (inputField.isSelected()) {
061: newValue = "true";
062: } else {
063: newValue = "false";
064: }
065: try {
066: firePropertyChangingEvent(newValue);
067: firePropertyChangedEvent(newValue);
068: } catch (PropertyValueVetoException pvve) {
069: manager.getFactory().showError(
070: inputField,
071: "Error changing value " + label.getText()
072: + " to " + newValue + ": "
073: + pvve.getReason());
074: inputField.setSelected(!inputField.isSelected());
075: }
076: }
077: });
078:
079: this .add(label);
080: this .add(inputField);
081: this .setEnabled(isEnabled);
082:
083: labelComponent = label;
084: valueComponent = inputField;
085:
086: manager.registerPropertyEditor(property, this );
087: }
088:
089: /**
090: * as defined in net.suberic.util.gui.PropertyEditorUI
091: */
092: public void setValue() {
093: if (isEnabled()) {
094: if (inputField.isSelected() != originalBoolean
095: || manager.getProperty(property, "unset").equals(
096: "unset")) {
097: String newValue;
098: if (inputField.isSelected())
099: newValue = "true";
100: else
101: newValue = "false";
102:
103: manager.setProperty(property, newValue);
104: }
105: }
106: }
107:
108: /**
109: * Returns the current values of the edited properties as a
110: * java.util.Properties object.
111: */
112: public java.util.Properties getValue() {
113: java.util.Properties retProps = new java.util.Properties();
114:
115: if (inputField.isSelected())
116: retProps.setProperty(property, "true");
117: else
118: retProps.setProperty(property, "false");
119: return retProps;
120: }
121:
122: /**
123: * This resets the editor to the original (or latest set, if setValue()
124: * has been called) value of the edited property.
125: */
126: public void resetDefaultValue() {
127: // this will be handled by the listener on the inputField, so we don't
128: // have to send any events here.
129: inputField.setSelected(originalBoolean);
130: }
131:
132: /**
133: * Sets the enabled property of the PropertyEditorUI. Disabled
134: * editors should not be able to do setValue() calls.
135: */
136: public void setEnabled(boolean newValue) {
137: if (inputField != null) {
138: inputField.setEnabled(newValue);
139: enabled = newValue;
140: }
141: }
142:
143: }
|