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 ; 6 7 11 public class StringEditorPane extends LabelValuePropertyEditor { 12 13 String currentValue = ""; 14 JLabel label; 15 JTextField inputField; 16 17 24 public void configureEditor(String propertyName, String template, String 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 (150, inputField.getMinimumSize().height)); 34 inputField.setMinimumSize(new java.awt.Dimension (150, inputField.getMinimumSize().height)); 35 inputField.setMaximumSize(new java.awt.Dimension (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 updateEditorEnabled(); 68 69 manager.registerPropertyEditor(property, this); 70 } 71 72 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 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 101 public java.util.Properties getValue() { 102 java.util.Properties retProps = new java.util.Properties (); 103 retProps.setProperty(property, inputField.getText()); 104 return retProps; 105 } 106 107 108 112 public void resetDefaultValue() throws PropertyValueVetoException { 113 String fieldValue = inputField.getText(); 114 if (! (fieldValue.equals(currentValue) && fieldValue.equals(originalValue))) { 115 if (! currentValue.equals(originalValue)) { 117 firePropertyChangingEvent(originalValue); 118 currentValue = originalValue; 119 firePropertyChangedEvent(originalValue); 120 } 121 inputField.setText(originalValue); 122 } 123 } 124 125 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 |