1 56 package org.objectstyle.cayenne.modeler.util; 57 58 import java.awt.Color ; 59 import java.awt.event.ActionEvent ; 60 import java.awt.event.ActionListener ; 61 62 import javax.swing.InputVerifier ; 63 import javax.swing.JComponent ; 64 import javax.swing.JTextArea ; 65 import javax.swing.JTextField ; 66 import javax.swing.event.DocumentEvent ; 67 import javax.swing.event.DocumentListener ; 68 import javax.swing.text.JTextComponent ; 69 70 import org.objectstyle.cayenne.modeler.dialog.validator.ValidatorDialog; 71 import org.objectstyle.cayenne.validation.ValidationException; 72 73 79 public abstract class TextAdapter { 80 81 protected Color defaultBGColor; 82 protected Color errorColor; 83 protected JTextComponent textComponent; 84 protected String defaultToolTip; 85 protected boolean modelUpdateDisabled; 86 87 public TextAdapter(JTextField textField) { 88 this(textField, true, false, true); 89 } 90 91 public TextAdapter(JTextField textField, boolean checkOnFocusLost, 92 boolean checkOnTyping, boolean checkOnEnter) { 93 this(textField, true, false); 94 95 if (checkOnEnter) { 96 97 textField.addActionListener(new ActionListener () { 98 99 public void actionPerformed(ActionEvent e) { 100 updateModel(); 101 } 102 }); 103 } 104 } 105 106 public TextAdapter(JTextArea textField) { 107 this(textField, false, true); 108 } 109 110 public TextAdapter(JTextComponent textComponent, boolean checkOnFocusLost, 111 boolean checkOnTyping) { 112 this.errorColor = ValidatorDialog.WARNING_COLOR; 113 this.defaultBGColor = textComponent.getBackground(); 114 this.defaultToolTip = textComponent.getToolTipText(); 115 this.textComponent = textComponent; 116 117 if (checkOnFocusLost) { 118 textComponent.setInputVerifier(new InputVerifier () { 119 120 public boolean verify(JComponent c) { 121 updateModel(); 122 return true; 124 } 125 }); 126 } 127 128 if (checkOnTyping) { 129 textComponent.getDocument().addDocumentListener(new DocumentListener () { 130 131 public void insertUpdate(DocumentEvent e) { 132 verifyTextChange(e); 133 } 134 135 public void changedUpdate(DocumentEvent e) { 136 verifyTextChange(e); 137 } 138 139 public void removeUpdate(DocumentEvent e) { 140 verifyTextChange(e); 141 } 142 143 void verifyTextChange(DocumentEvent e) { 144 if (!modelUpdateDisabled) { 145 updateModel(); 146 } 147 } 148 }); 149 } 150 } 151 152 155 protected abstract void updateModel(String text) throws ValidationException; 156 157 160 public JTextComponent getComponent() { 161 return textComponent; 162 } 163 164 167 public void setText(String text) { 168 modelUpdateDisabled = true; 169 try { 170 clear(); 171 textComponent.setText(text); 172 } 173 finally { 174 modelUpdateDisabled = false; 175 } 176 } 177 178 protected void updateModel() { 179 try { 180 updateModel(textComponent.getText()); 181 clear(); 182 } 183 catch (ValidationException vex) { 184 textComponent.setBackground(errorColor); 185 textComponent.setToolTipText(vex.getUnlabeledMessage()); 186 } 187 } 188 189 protected void clear() { 190 textComponent.setBackground(defaultBGColor); 191 textComponent.setToolTipText(defaultToolTip); 192 } 193 } | Popular Tags |