1 7 8 package org.jdesktop.swing.binding; 9 10 import javax.swing.JCheckBox ; 11 import javax.swing.JTextField ; 12 import javax.swing.text.JTextComponent ; 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 33 public class BindingIssues extends TestCase { 34 35 private PropertyChangeReport tracker; 36 private DefaultDataModel personModel; 37 private JForm form; 38 39 44 public void testEmptyStringRequired() { 45 DefaultDataModel model = new DefaultDataModel(); 46 String fieldName = "required"; 47 MetaData metaData = new StringMetaData(fieldName, "Required"); 48 metaData.setRequired(true); 49 model.addField(metaData, "not-null"); 50 JTextComponent textField= new JTextField (); 51 assertEquals("", textField.getText()); 52 Binding binding = new TextBinding(textField, model, fieldName); 53 binding.pull(); 54 textField.setText(""); 55 assertTrue(binding.isModified()); 56 assertTrue("push must succeed", binding.push()); 58 assertEquals("value must be empty string", "", model.getValue(fieldName)); 59 } 60 61 67 public void testEmptyStringNotRequired() throws Exception { 68 DefaultDataModel model = new DefaultDataModel(); 69 String fieldName = "notrequired"; 70 MetaData metaData = new StringMetaData(fieldName, "NotRequired"); 71 model.addField(metaData, "not-null"); 72 JTextComponent textField= new JTextField (); 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 89 public void testBooleanBindingIsValid() { 90 DefaultDataModel model = new DefaultDataModel(); 91 MetaData booleanMetaData = new MetaData("married", boolean.class); 92 model.addField(booleanMetaData); 93 JCheckBox checkbox = new JCheckBox (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 103 107 public void testSimpleBindingFiresModified() { 108 assertFiredModified(form, null, "lastname"); 109 } 110 111 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 subformFieldname, 125 String fieldname) { 126 Binding binding = getBinding(form, subformFieldname, fieldname); 127 ((JTextComponent ) binding.getComponent()).setText("somevalue"); 128 assertEquals("binding must have fired modified", 1, 129 tracker.getEventCount("modified")); 130 } 131 132 134 143 private Binding getBinding(JForm form, String modelFieldname, 144 String 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 158 private Binding getBinding(JForm form, String 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 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 e.printStackTrace(); 183 } 184 return form; 185 } 186 187 191 private void installTracker(Binding[] bindings) { 192 for (int i = 0; i < bindings.length; i++) { 193 bindings[i].addPropertyChangeListener(tracker); 194 } 195 } 196 197 protected void setUp() 199 throws Exception { 200 super.setUp(); 201 tracker = new PropertyChangeReport(); 202 personModel = TestDataModelFactory.createNestedPersonModel(); 203 form = createForm(personModel); 204 } 205 206 } 207 | Popular Tags |