KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: BindingIssues.java,v 1.4 2005/01/26 11:56:25 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 javax.swing.JCheckBox JavaDoc;
11 import javax.swing.JTextField JavaDoc;
12 import javax.swing.text.JTextComponent JavaDoc;
13
14 import org.jdesktop.swing.data.DataModel;
15 import org.jdesktop.swing.data.AbstractDataModelTst;
16 import org.jdesktop.swing.data.DefaultDataModel;
17 import org.jdesktop.swing.data.MetaData;
18 import org.jdesktop.swing.data.StringMetaData;
19 import org.jdesktop.swing.data.TestDataModelFactory;
20 import org.jdesktop.swing.form.JForm;
21 import org.jdesktop.swing.util.PropertyChangeReport;
22
23 import junit.framework.TestCase;
24
25 /**
26  * Open binding issues exposed as TestCase. <p>
27  *
28  * NOTE: this test is expected to fail. Will be
29  * integrated into BindingUnitTest after resolving.
30  *
31  * @author Jeanette Winzenburg
32  */

33 public class BindingIssues extends TestCase {
34
35     private PropertyChangeReport tracker;
36     private DefaultDataModel personModel;
37     private JForm form;
38
39     /** Issue #154: handling of empty String.
40      *
41      * PENDING: what should be the default if required?
42      *
43      */

44     public void testEmptyStringRequired() {
45         DefaultDataModel model = new DefaultDataModel();
46         String JavaDoc fieldName = "required";
47         MetaData metaData = new StringMetaData(fieldName, "Required");
48         metaData.setRequired(true);
49         model.addField(metaData, "not-null");
50         JTextComponent JavaDoc textField= new JTextField JavaDoc();
51         assertEquals("", textField.getText());
52         Binding binding = new TextBinding(textField, model, fieldName);
53         binding.pull();
54         textField.setText("");
55         assertTrue(binding.isModified());
56         // JW: what to expect here?
57
assertTrue("push must succeed", binding.push());
58         assertEquals("value must be empty string", "", model.getValue(fieldName));
59     }
60
61     /** Issue #154: handling of empty String if not required.
62      *
63      * Majority opinion in forum:
64      * should return empty String instead of internally converted
65      * value to null?
66      */

67     public void testEmptyStringNotRequired() throws Exception JavaDoc {
68         DefaultDataModel model = new DefaultDataModel();
69         String JavaDoc fieldName = "notrequired";
70         MetaData metaData = new StringMetaData(fieldName, "NotRequired");
71         model.addField(metaData, "not-null");
72         JTextComponent JavaDoc textField= new JTextField JavaDoc();
73         assertEquals("", textField.getText());
74         Binding binding = new TextBinding(textField, model, fieldName);
75         binding.pull();
76         textField.setText("");
77         assertTrue(binding.isModified());
78         assertTrue("push must succeed", binding.push());
79         assertEquals("value must be empty string", "", model.getValue(fieldName));
80     }
81     
82     
83     /**
84      * Issue #115 - BooleanBinding cannot cope with boolean.class. <p>
85      * Don't allow primitiv type? DataModel can handle Object only
86      * anyway...
87      *
88      */

89     public void testBooleanBindingIsValid() {
90         DefaultDataModel model = new DefaultDataModel();
91         MetaData booleanMetaData = new MetaData("married", boolean.class);
92         model.addField(booleanMetaData);
93         JCheckBox JavaDoc checkbox = new JCheckBox JavaDoc(booleanMetaData.getLabel());
94         BooleanBinding binding = new BooleanBinding(checkbox, model,
95                                    "married");
96         binding.pull();
97         assertTrue("boolean binding must always be valid ", binding.isValid());
98         
99       }
100     
101 // -------------------- modify binding and events
102

103     /**
104      * test binding firing modified in simple form.
105      *
106      */

107     public void testSimpleBindingFiresModified() {
108       assertFiredModified(form, null, "lastname");
109     }
110
111     /**
112      * Issue: #??
113      *
114      * test FormBinding firing modified if contained binding
115      * is modified.
116      *
117      */

118     public void testFormBindingFiresModified() {
119       Binding formBinding = getBinding(form, "address");
120       assertTrue(formBinding instanceof FormBinding);
121       assertFiredModified(form, "address", "city");
122     }
123
124     private void assertFiredModified(JForm form, String JavaDoc subformFieldname,
125         String JavaDoc fieldname) {
126       Binding binding = getBinding(form, subformFieldname, fieldname);
127       ((JTextComponent JavaDoc) binding.getComponent()).setText("somevalue");
128       assertEquals("binding must have fired modified", 1,
129                  tracker.getEventCount("modified"));
130     }
131
132 //----------------------- helpers installing/accessing bindings
133

134     /** returns binding with given fieldName.
135      * if modelFieldName != null returns binding in nested
136      * form with modelFieldName.
137      *
138      * @param form
139      * @param modelFieldname
140      * @param fieldname
141      * @return
142      */

143     private Binding getBinding(JForm form, String JavaDoc modelFieldname,
144         String JavaDoc fieldname) {
145       if (modelFieldname != null) {
146         Binding formBinding = getBinding(form, modelFieldname);
147         form = (JForm) formBinding.getComponent();
148       }
149       return getBinding(form, fieldname);
150     }
151
152     /** returns direct binding with given fieldname.
153      *
154      * @param form
155      * @param fieldname
156      * @return
157      */

158     private Binding getBinding(JForm form, String JavaDoc fieldname) {
159       Binding[] bindings = form.getBindings();
160       for (int i = 0; i < bindings.length; i++) {
161         if (fieldname.equals(bindings[i].getFieldName())) {
162           return bindings[i];
163         }
164       }
165       return null;
166     }
167
168     /** create form, bind model and install tracker
169      * on all direct bindings.
170      *
171      * @param personModel
172      * @return
173      */

174     private JForm createForm(DataModel personModel) {
175       JForm form = new JForm();
176       try {
177         installTracker(form.bind(personModel));
178         form.pull();
179       } catch (BindException e) {
180
181         // TODO Auto-generated catch block
182
e.printStackTrace();
183       }
184       return form;
185     }
186
187     /** registers tracker with all all bindings.
188      *
189      * @param bindings
190      */

191     private void installTracker(Binding[] bindings) {
192       for (int i = 0; i < bindings.length; i++) {
193         bindings[i].addPropertyChangeListener(tracker);
194       }
195     }
196
197 //----------- super
198
protected void setUp()
199       throws Exception JavaDoc {
200       super.setUp();
201       tracker = new PropertyChangeReport();
202       personModel = TestDataModelFactory.createNestedPersonModel();
203       form = createForm(personModel);
204     }
205
206 }
207
Popular Tags