KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > TextBinding


1 /*
2  * $Id: TextBinding.java,v 1.4 2005/02/28 17:40:18 kleopatra Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.binding;
9
10 import java.beans.PropertyChangeEvent JavaDoc;
11 import java.beans.PropertyChangeListener JavaDoc;
12
13 import javax.swing.JComponent JavaDoc;
14 import javax.swing.UIManager JavaDoc;
15 import javax.swing.event.DocumentEvent JavaDoc;
16 import javax.swing.event.DocumentListener JavaDoc;
17 import javax.swing.text.BadLocationException JavaDoc;
18 import javax.swing.text.Document JavaDoc;
19 import javax.swing.text.JTextComponent JavaDoc;
20
21 import org.jdesktop.swing.data.DataModel;
22
23
24 /**
25  * Class which binds a component that supports editing text values
26  * (JTextField, JTextArea, JEditorPane) to a data model field. <p>
27  *
28  * JW: hmm, what's the use case? I would prefer to better not do it now,
29  * can cope with that later if necessary.
30  *
31  * [Although this binding is most commonly used for Swing's text
32  * components, it may be used with any component that defines a
33  * <code>javax.swing.text.Document</code> to represent its contents.]
34  *
35  * @author Amy Fowler
36  * @version 1.0
37  */

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