1 7 package org.jdesktop.swing.form; 8 9 import java.awt.Dimension ; 10 import java.awt.Insets ; 11 import java.beans.PropertyChangeEvent ; 12 import java.beans.PropertyChangeListener ; 13 14 import javax.swing.*; 15 import javax.swing.JComponent ; 16 import javax.swing.border.CompoundBorder ; 17 import javax.swing.text.JTextComponent ; 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 31 public class JFormIssues extends JFormUnitTest { 32 33 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 (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 textComponent = (JTextComponent ) 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 assertTrue(((Boolean ) personModel.getValue("married")).booleanValue()); 71 Binding binding = getBinding(form, "firstname"); 72 JTextComponent textComponent = (JTextComponent ) binding.getComponent(); 73 74 assertTrue("textField must be editable", textComponent.isEditable()); 75 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 assertTrue(((Boolean ) personModel.getValue("married")).booleanValue()); 90 Binding binding = getBinding(form, "firstname"); 91 JTextComponent textComponent = (JTextComponent ) binding.getComponent(); 92 93 assertTrue("textField must be editable", textComponent.isEditable()); 94 Binding checkBoxBinding = getBinding(form, "married"); 97 JCheckBox checkBox = (JCheckBox) checkBoxBinding.getComponent(); 98 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 source, 111 final String target) { 112 sourceModel.addValueChangeListener(new ValueChangeListener() { 113 114 public void valueChanged(ValueChangeEvent e) { 118 if (source.equals(e.getFieldName())) { 119 MetaData metaData = sourceModel.getMetaData(target); 120 boolean isEditable = ((Boolean ) sourceModel.getValue(source)).booleanValue(); 121 metaData.setReadOnly(!isEditable); 122 } 123 } 124 }); 125 } 126 127 128 } 129 | Popular Tags |