KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.util.gui.propedit;
2 import javax.swing.*;
3 import java.awt.event.*;
4 import net.suberic.util.*;
5 import java.awt.FlowLayout JavaDoc;
6
7 /**
8  * The default EditorPane. Just shows a text field in which a user
9  * can enter a String.
10  */

11 public class StringEditorPane extends LabelValuePropertyEditor {
12
13   String JavaDoc currentValue = "";
14   JLabel label;
15   JTextField inputField;
16
17   /**
18    * @param propertyName The property to be edited.
19    * @param template The property that will define the layout of the
20    * editor.
21    * @param manager The PropertyEditorManager that will manage the
22    * changes.
23    */

24   public void configureEditor(String JavaDoc propertyName, String JavaDoc template, String JavaDoc propertyBaseName, PropertyEditorManager newManager) {
25     configureBasic(propertyName, template, propertyBaseName, newManager);
26
27     getLogger().fine("configuring StringEditorPane. property is " + property + "; editorTemplate is " + editorTemplate);
28
29     label = createLabel();
30     currentValue = originalValue == null ? "" : originalValue;
31
32     inputField = new JTextField(originalValue);
33     inputField.setPreferredSize(new java.awt.Dimension JavaDoc(150, inputField.getMinimumSize().height));
34     inputField.setMinimumSize(new java.awt.Dimension JavaDoc(150, inputField.getMinimumSize().height));
35     inputField.setMaximumSize(new java.awt.Dimension JavaDoc(Integer.MAX_VALUE, inputField.getMinimumSize().height));
36     inputField.addFocusListener(new FocusAdapter() {
37         public void focusLost(FocusEvent e) {
38           if (! inputField.getText().equals(currentValue)) {
39             try {
40               firePropertyChangingEvent(inputField.getText());
41               currentValue = inputField.getText();
42               firePropertyChangedEvent(currentValue);
43             } catch (PropertyValueVetoException pvve) {
44               inputField.setText(currentValue);
45               manager.getFactory().showError(inputField, "Error changing value " + label.getText() + " to " + pvve.getRejectedValue() + ": " + pvve.getReason());
46               inputField.requestFocusInWindow();
47             }
48           }
49         }
50
51         public void focusGained(FocusEvent e) {
52           if (inputField.getText() != null && inputField.getText().length() > 0) {
53             inputField.setSelectionStart(0);
54             inputField.setSelectionEnd(inputField.getText().length());
55           }
56         }
57
58       });
59     this.add(label);
60     this.add(inputField);
61
62     labelComponent = label;
63     valueComponent = inputField;
64
65     // have to set enabled now, with the current enabled value, since it
66
// may have changed.
67
updateEditorEnabled();
68
69     manager.registerPropertyEditor(property, this);
70   }
71
72   /**
73    * This writes the currently configured value in the PropertyEditorUI
74    * to the source PropertyEditorManager.
75    */

76   public void setValue() throws PropertyValueVetoException {
77     validateProperty();
78     if (isEnabled() && !(inputField.getText().equals(originalValue))) {
79       manager.setProperty(property, inputField.getText());
80       originalValue = inputField.getText();
81     }
82   }
83
84   /**
85    * This checks that the currently configured value is valid.
86    */

87   public void validateProperty() throws PropertyValueVetoException {
88     if (isEnabled() && !(inputField.getText().equals(currentValue))) {
89       firePropertyChangingEvent(inputField.getText());
90       firePropertyChangedEvent(inputField.getText());
91     }
92
93     firePropertyCommittingEvent(inputField.getText());
94   }
95
96
97   /**
98    * Returns the current values of the edited properties as a
99    * java.util.Properties object.
100    */

101   public java.util.Properties JavaDoc getValue() {
102     java.util.Properties JavaDoc retProps = new java.util.Properties JavaDoc();
103     retProps.setProperty(property, inputField.getText());
104     return retProps;
105   }
106
107
108   /**
109    * This resets the editor to the original (or latest set, if setValue()
110    * has been called) value of the edited property.
111    */

112   public void resetDefaultValue() throws PropertyValueVetoException {
113     String JavaDoc fieldValue = inputField.getText();
114     if (! (fieldValue.equals(currentValue) && fieldValue.equals(originalValue))) {
115       // something has changed, so we'll have to deal with it.
116
if (! currentValue.equals(originalValue)) {
117         firePropertyChangingEvent(originalValue);
118         currentValue = originalValue;
119         firePropertyChangedEvent(originalValue);
120       }
121       inputField.setText(originalValue);
122     }
123   }
124
125   /**
126    * Run when the PropertyEditor may have changed enabled states.
127    */

128   protected void updateEditorEnabled() {
129     if (inputField != null) {
130       inputField.setEnabled(isEditorEnabled());
131     }
132     if (label != null) {
133       label.setEnabled(isEditorEnabled());
134     }
135   }
136 }
137
Popular Tags