001: package net.suberic.util.gui.propedit;
002:
003: import javax.swing.*;
004: import java.awt.event.*;
005: import net.suberic.util.*;
006: import java.awt.FlowLayout;
007:
008: /**
009: * The default EditorPane. Just shows a text field in which a user
010: * can enter a String.
011: */
012: public class StringEditorPane extends SwingPropertyEditor {
013:
014: JLabel label;
015: JTextField inputField;
016: String currentValue;
017:
018: /**
019: * @param propertyName The property to be edited.
020: * @param template The property that will define the layout of the
021: * editor.
022: * @param manager The PropertyEditorManager that will manage the
023: * changes.
024: * @param isEnabled Whether or not this editor is enabled by default.
025: */
026: public void configureEditor(String propertyName, String template,
027: PropertyEditorManager newManager, boolean isEnabled) {
028: property = propertyName;
029: manager = newManager;
030: editorTemplate = template;
031: originalValue = manager.getProperty(property, "");
032: currentValue = originalValue;
033:
034: if (debug) {
035: System.out
036: .println("configuring StringEditorPane. property is "
037: + property
038: + "; editorTemplate is "
039: + editorTemplate);
040: }
041:
042: label = createLabel();
043: inputField = new JTextField(originalValue);
044: inputField.setPreferredSize(new java.awt.Dimension(150,
045: inputField.getMinimumSize().height));
046: inputField.setMinimumSize(new java.awt.Dimension(150,
047: inputField.getMinimumSize().height));
048: inputField.addFocusListener(new FocusAdapter() {
049: public void focusLost(FocusEvent e) {
050: if (!inputField.getText().equals(currentValue)) {
051: try {
052: firePropertyChangingEvent(inputField.getText());
053: currentValue = inputField.getText();
054: firePropertyChangedEvent(currentValue);
055: } catch (PropertyValueVetoException pvve) {
056: inputField.setText(currentValue);
057: manager.getFactory().showError(
058: inputField,
059: "Error changing value "
060: + label.getText() + " to "
061: + pvve.getRejectedValue()
062: + ": " + pvve.getReason());
063: inputField.requestFocusInWindow();
064: }
065: }
066: }
067: });
068: this .add(label);
069: this .add(inputField);
070: this .setEnabled(isEnabled);
071:
072: labelComponent = label;
073: valueComponent = inputField;
074:
075: manager.registerPropertyEditor(property, this );
076: addDefaultListeners();
077: }
078:
079: /**
080: * This writes the currently configured value in the PropertyEditorUI
081: * to the source PropertyEditorManager.
082: */
083: public void setValue() throws PropertyValueVetoException {
084: if (isEnabled() && !(inputField.getText().equals(currentValue))) {
085: firePropertyChangingEvent(inputField.getText());
086: firePropertyChangedEvent(inputField.getText());
087: }
088:
089: if (isEnabled()
090: && !(inputField.getText().equals(originalValue))) {
091: manager.setProperty(property, inputField.getText());
092: }
093: }
094:
095: /**
096: * Returns the current values of the edited properties as a
097: * java.util.Properties object.
098: */
099: public java.util.Properties getValue() {
100: java.util.Properties retProps = new java.util.Properties();
101: retProps.setProperty(property, inputField.getText());
102: return retProps;
103: }
104:
105: /**
106: * This resets the editor to the original (or latest set, if setValue()
107: * has been called) value of the edited property.
108: */
109: public void resetDefaultValue() throws PropertyValueVetoException {
110: String fieldValue = inputField.getText();
111: if (!(fieldValue.equals(currentValue) && fieldValue
112: .equals(originalValue))) {
113: // something has changed, so we'll have to deal with it.
114: if (!currentValue.equals(originalValue)) {
115: firePropertyChangingEvent(originalValue);
116: currentValue = originalValue;
117: firePropertyChangedEvent(originalValue);
118: }
119: inputField.setText(originalValue);
120: }
121: }
122:
123: /**
124: * Sets the enabled property of the PropertyEditorUI. Disabled
125: * editors should not be able to do setValue() calls.
126: */
127: public void setEnabled(boolean newValue) {
128: if (inputField != null) {
129: inputField.setEnabled(newValue);
130: enabled = newValue;
131: }
132: }
133: }
|