1 7 8 package org.jdesktop.swing.binding; 9 10 import java.beans.PropertyChangeEvent ; 11 import java.beans.PropertyChangeListener ; 12 13 import javax.swing.JComponent ; 14 import javax.swing.UIManager ; 15 import javax.swing.event.DocumentEvent ; 16 import javax.swing.event.DocumentListener ; 17 import javax.swing.text.BadLocationException ; 18 import javax.swing.text.Document ; 19 import javax.swing.text.JTextComponent ; 20 21 import org.jdesktop.swing.data.DataModel; 22 23 24 38 public class TextBinding extends AbstractBinding { 39 private JComponent component; 40 private Document document; 41 42 public TextBinding(JTextComponent textComponent, 43 DataModel model, String fieldName) { 44 super(textComponent, model, fieldName, AbstractBinding.AUTO_VALIDATE); 45 initDocument(textComponent.getDocument()); 46 } 47 48 public TextBinding(JTextComponent textComponent, 49 DataModel model, String fieldName, 50 int validationPolicy) { 51 super(textComponent, model, fieldName, validationPolicy); 52 initDocument(textComponent.getDocument()); 53 } 54 55 public TextBinding(JComponent component, Document document, 56 DataModel dataModel, String fieldName, 57 int validationPolicy) { 58 super(component, dataModel, fieldName, validationPolicy); 59 initDocument(document); 60 } 61 62 public JComponent getComponent() { 63 return component; 64 } 65 66 protected void setComponent(JComponent component) { 67 this.component = component; 68 configureEditability(); 69 } 70 71 protected void configureEditability() { 72 if (!(component instanceof JTextComponent )) return; 73 JTextComponent textComponent = (JTextComponent ) component; 74 textComponent.setEditable(!metaData.isReadOnly()); 75 76 } 77 78 protected void installMetaDataListener() { 79 PropertyChangeListener l = new PropertyChangeListener () { 80 81 public void propertyChange(PropertyChangeEvent evt) { 82 if ("readOnly".equals(evt.getPropertyName())) { 83 configureEditability(); 84 } 85 86 } 87 88 }; 89 metaData.addPropertyChangeListener(l); 91 } 93 94 protected Object getComponentValue() { 95 String txt; 96 try { 97 txt = document.getText(0, document.getLength()); 98 } 99 catch (BadLocationException e) { 100 txt = null; 101 } 102 return txt; 103 } 104 105 protected void setComponentValue(Object value) { 106 try { 107 document.remove(0, document.getLength()); 108 document.insertString(0, convertFromModelType(value), null); 109 } 110 catch (BadLocationException e) { 111 UIManager.getLookAndFeel().provideErrorFeedback(component); 112 } 113 114 } 115 116 private void initDocument(Document document) { 117 this.document = document; 118 document.addDocumentListener(new DocumentListener () { 119 public void changedUpdate(DocumentEvent e) { 120 maybeModified(); 121 } 122 123 public void insertUpdate(DocumentEvent e) { 124 maybeModified(); 125 } 126 127 public void removeUpdate(DocumentEvent e) { 128 maybeModified(); 129 } 130 131 public void maybeModified() { 132 if (!pulling) { 133 setModified(true); 134 } 135 } 136 }); 137 } 138 } | Popular Tags |