KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > form > JFormIssues


1 /*
2  * $Id: JFormIssues.java,v 1.2 2005/02/28 17:41:38 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 package org.jdesktop.swing.form;
8
9 import java.awt.Dimension JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.beans.PropertyChangeEvent JavaDoc;
12 import java.beans.PropertyChangeListener JavaDoc;
13
14 import javax.swing.*;
15 import javax.swing.JComponent JavaDoc;
16 import javax.swing.border.CompoundBorder JavaDoc;
17 import javax.swing.text.JTextComponent JavaDoc;
18
19 import org.jdesktop.swing.binding.Binding;
20 import org.jdesktop.swing.binding.TextBinding;
21 import org.jdesktop.swing.data.*;
22 import org.jdesktop.swing.data.DataModel;
23 import org.jdesktop.swing.data.Validator;
24 import org.jdesktop.swing.form.*;
25 import org.jdesktop.swing.form.DefaultFormFactory;
26 import org.jdesktop.swing.form.JForm;
27
28 /**
29  * @author Jeanette Winzenburg
30  */

31 public class JFormIssues extends JFormUnitTest {
32
33     /**
34      * Issue #128: JForm allows invalid data to be pushed. <p>
35      *
36      * assert that form detects invalid DataModel,
37      * invalid by datamodel validator, invalid data set in binding.
38      */

39     public void testFormInvalidOnInvalidModelBinding() {
40         JForm form = assertFormValiditySynchedOnFields(true);
41         DataModel personModel = form.getBindings()[0].getDataModel();
42         Validator marriedAgeValidator = createMarriedAgeValidator(personModel);
43         personModel.addValidator(marriedAgeValidator);
44
45         Binding binding = getBinding(form, "age");
46         setSpinnerValue(binding, new Integer JavaDoc(10));
47
48         assertFalse("form must be invalid", form.isFormValid());
49
50     }
51
52     public void testBindingListeningToMetaData() {
53         DataModel personModel = TestDataModelFactory
54             .createFilledPersonModel(true);
55         JForm form = createForm(personModel);
56         Binding binding = getBinding(form, "firstname");
57         JTextComponent JavaDoc textComponent = (JTextComponent JavaDoc) binding.getComponent();
58         assertTrue("textField must be editable", textComponent.isEditable());
59         MetaData metaData = binding.getDataModel().getMetaData("firstname");
60         metaData.setReadOnly(true);
61         assertFalse("textField must not be editable", textComponent.isEditable());
62     }
63     
64     public void testValueDependentMetaData() {
65         DataModel personModel = TestDataModelFactory
66             .createFilledPersonModel(true);
67         JForm form = createForm(personModel);
68         connectEditable(personModel, "married", "firstname");
69         // sanity assert
70
assertTrue(((Boolean JavaDoc) personModel.getValue("married")).booleanValue());
71         Binding binding = getBinding(form, "firstname");
72         JTextComponent JavaDoc textComponent = (JTextComponent JavaDoc) binding.getComponent();
73         
74         assertTrue("textField must be editable", textComponent.isEditable());
75 // textComponent.setText("newvalue");
76
// personModel.setValue("married", Boolean.FALSE);
77
JCheckBox checkBox = (JCheckBox) getBinding(form, "married").getComponent();
78         checkBox.setSelected(false);
79         assertFalse("textField must not be editable", textComponent.isEditable());
80     }
81     
82     public void testValueDependentMetaDataOnPull() {
83         DataModel personModel = TestDataModelFactory
84             .createFilledPersonModel(true);
85         
86         JForm form = createForm(personModel);
87         connectEditable(personModel, "married", "firstname");
88         // sanity assert
89
assertTrue(((Boolean JavaDoc) personModel.getValue("married")).booleanValue());
90         Binding binding = getBinding(form, "firstname");
91         JTextComponent JavaDoc textComponent = (JTextComponent JavaDoc) binding.getComponent();
92         
93         assertTrue("textField must be editable", textComponent.isEditable());
94 // textComponent.setText("newvalue");
95
// personModel.setValue("married", Boolean.FALSE);
96
Binding checkBoxBinding = getBinding(form, "married");
97         JCheckBox checkBox = (JCheckBox) checkBoxBinding.getComponent();
98         // sanity check
99
assertTrue(checkBox.isSelected());
100         checkBox.setSelected(false);
101         assertFalse(checkBox.isSelected());
102         personModel.addValueChangeListener(changeReport);
103         checkBoxBinding.push();
104         assertTrue("pushing the value must have fired changeEvent",
105                 changeReport.gotEvent("married"));
106         binding.pull();
107         assertFalse("textField must not be editable", textComponent.isEditable());
108     }
109     
110     private void connectEditable(final DataModel sourceModel, final String JavaDoc source,
111             final String JavaDoc target) {
112           sourceModel.addValueChangeListener(new ValueChangeListener() {
113
114             // not really working:
115
// we don't get ValueChangeEvents during
116
// edits... hmmmm
117
public void valueChanged(ValueChangeEvent e) {
118               if (source.equals(e.getFieldName())) {
119                 MetaData metaData = sourceModel.getMetaData(target);
120                 boolean isEditable = ((Boolean JavaDoc) sourceModel.getValue(source)).booleanValue();
121                 metaData.setReadOnly(!isEditable);
122               }
123             }
124           });
125         }
126     
127     
128 }
129
Popular Tags