BindingContext { public Binding bind(String propertyName, ComponentAdapter componentAdapter) { Assert.notNull(propertyName, "propertyName"); Assert.notNull(componentAdapter, "componentAdapter"); BindingImpl binding = new BindingImpl(this, propertyName, componentAdapter); addBinding(binding); componentAdapter.setBinding(binding); componentAdapter.bindComponents(); binding.bindProperty(); binding.adjustComponents(); configureComponents(binding); return binding; } BindingContext();  BindingContext(PropertySet propertySet);  BindingContext(PropertySet propertySet, BindingProblemListener problemHandler); PropertySet getPropertySet(); boolean hasProblems(); BindingProblem[] getProblems(); void addProblemListener(BindingProblemListener listener); void removeProblemListener(BindingProblemListener listener); BindingProblemListener[] getProblemListeners(); void adjustComponents(); Binding getBinding(String propertyName); Binding bind(String propertyName, ComponentAdapter componentAdapter); void unbind(Binding binding); Binding bind(final String propertyName, final JTextComponent component); Binding bind(final String propertyName, final JTextField component); Binding bind(final String propertyName, final JFormattedTextField component); Binding bind(final String propertyName, final JCheckBox component); Binding bind(String propertyName, JRadioButton component); Binding bind(final String propertyName, final JList component, final boolean selectionIsValue); Binding bind(final String propertyName, final JSpinner component); Binding bind(final String propertyName, final JComboBox component); Binding bind(final String propertyName, final ButtonGroup buttonGroup); Binding bind(final String propertyName, final ButtonGroup buttonGroup,
                        final Map<AbstractButton, Object> valueSet); void addPropertyChangeListener(PropertyChangeListener l); void addPropertyChangeListener(String name, PropertyChangeListener l); void removePropertyChangeListener(PropertyChangeListener l); void removePropertyChangeListener(String name, PropertyChangeListener l); @SuppressWarnings({"MethodMayBeStatic"}) void permitPropertyChanges(JComponent component); @SuppressWarnings({"MethodMayBeStatic"}) void preventPropertyChanges(JComponent component); Enablement bindEnabledState(final String targetPropertyName,
                                       final boolean targetState,
                                       final String sourcePropertyName,
                                       final Object sourcePropertyValue); Enablement bindEnabledState(final String targetPropertyName,
                                       final boolean targetState,
                                       final Enablement.Condition condition); void setComponentsEnabled(String propertyName, boolean enabled);  }
@Test public void testBindSpinner() throws ValidationException { JSpinner spinner = new JSpinner(); Binding binding = bindingContextVB.bind("intValue", spinner); assertNotNull(binding); assertSame(spinner, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("intValue", spinner.getName()); spinner.setValue(3); assertEquals(3, (int)propertyContainerVB.getValue("intValue")); propertyContainerVB.setValue("intValue", 76); assertEquals(76, spinner.getValue()); }
@Test public void testBindTextField() throws Exception { JTextField textField = new JTextField(); Binding binding = bindingContextVB.bind("stringValue", textField); Thread.sleep(1000); assertNotNull(binding); assertSame(textField, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("stringValue", textField.getName()); textField.setText("Bibo"); textField.postActionEvent(); assertEquals("Bibo", propertyContainerVB.getValue("stringValue")); propertyContainerVB.setValue("stringValue", "Samson"); Thread.sleep(1000); assertEquals("Samson", textField.getText()); }
@Test public void testBindTextField2() throws Exception { JTextField textField = new JTextField(); Binding binding = bindingContextOB.bind("stringValue", textField); assertNotNull(binding); assertSame(textField, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("stringValue", textField.getName()); textField.setText("Bibo"); textField.postActionEvent(); Thread.sleep(10); assertEquals("Bibo", propertyContainerOB.getValue("stringValue")); propertyContainerOB.setValue("stringValue", "Samson"); Thread.sleep(100); assertEquals("Samson", pojo.stringValue); assertEquals("Samson", textField.getText()); pojo.stringValue = "Oscar"; assertSame("Oscar", propertyContainerOB.getValue("stringValue")); assertNotSame("Oscar", textField.getText()); }
@Test public void testBindFormattedTextFieldToString() throws ValidationException { JFormattedTextField textField = new JFormattedTextField(); Binding binding = bindingContextVB.bind("stringValue", textField); assertNotNull(binding); assertSame(textField, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("stringValue", textField.getName()); textField.setValue("Bibo"); assertEquals("Bibo", propertyContainerVB.getValue("stringValue")); propertyContainerVB.setValue("stringValue", "Samson"); assertEquals("Samson", textField.getValue()); }
@Test public void testBindFormattedTextFieldToDouble() throws ValidationException { JFormattedTextField textField = new JFormattedTextField(); Binding binding = bindingContextVB.bind("doubleValue", textField); assertNotNull(binding); assertSame(textField, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("doubleValue", textField.getName()); textField.setValue(3.14); assertEquals(3.14, (double)propertyContainerVB.getValue("doubleValue"), 1.0e-6); propertyContainerVB.setValue("doubleValue", 2.71); assertEquals(2.71, textField.getValue()); }
@Test public void testBindTextArea() throws Exception { JTextArea textArea = new JTextArea(); TextComponentAdapter textComponentAdapter = new TextComponentAdapter(textArea); Binding binding = bindingContextVB.bind("stringValue", textComponentAdapter); assertNotNull(binding); assertSame(textArea, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("stringValue", textArea.getName()); textArea.setText("Bibo"); textComponentAdapter.actionPerformed(null); assertEquals("Bibo", propertyContainerVB.getValue("stringValue")); propertyContainerVB.setValue("stringValue", "Samson"); Thread.sleep(100); assertEquals("Samson", textArea.getText()); }
@Test public void testBindCheckBox() throws ValidationException { JCheckBox checkBox = new JCheckBox(); Binding binding = bindingContextVB.bind("booleanValue", checkBox); assertNotNull(binding); assertSame(checkBox, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("booleanValue", checkBox.getName()); checkBox.doClick(); assertEquals(true, propertyContainerVB.getValue("booleanValue")); propertyContainerVB.setValue("booleanValue", false); assertEquals(false, checkBox.isSelected()); }
@Test public void testBindRadioButton() throws ValidationException { JRadioButton radioButton = new JRadioButton(); Binding binding = bindingContextVB.bind("booleanValue", radioButton); assertNotNull(binding); assertSame(radioButton, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("booleanValue", radioButton.getName()); radioButton.doClick(); assertEquals(true, propertyContainerVB.getValue("booleanValue")); propertyContainerVB.setValue("booleanValue", false); assertEquals(false, radioButton.isSelected()); }
@Test public void testBindButtonGroup() throws ValidationException { JRadioButton radioButton1 = new JRadioButton(); JRadioButton radioButton2 = new JRadioButton(); JRadioButton radioButton3 = new JRadioButton(); ButtonGroup buttonGroup = new ButtonGroup(); buttonGroup.add(radioButton1); buttonGroup.add(radioButton2); buttonGroup.add(radioButton3); Property m = propertyContainerVB.getProperty("valueSetBoundIntValue"); m.setValue(TestPojo.intValueSet[0]); Binding binding = bindingContextVB.bind("valueSetBoundIntValue", buttonGroup); assertNotNull(binding); assertSame(radioButton1, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(3, binding.getComponents().length); assertSame(radioButton1, binding.getComponents()[0]); assertSame(radioButton2, binding.getComponents()[1]); assertSame(radioButton3, binding.getComponents()[2]); assertEquals(true, radioButton1.isSelected()); assertEquals(false, radioButton2.isSelected()); assertEquals(false, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[0], m.getValue()); radioButton3.doClick(); assertEquals(false, radioButton1.isSelected()); assertEquals(false, radioButton2.isSelected()); assertEquals(true, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[2], m.getValue()); radioButton2.doClick(); assertEquals(false, radioButton1.isSelected()); assertEquals(true, radioButton2.isSelected()); assertEquals(false, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[1], m.getValue()); m.setValue(TestPojo.intValueSet[0]); assertEquals(true, radioButton1.isSelected()); assertEquals(false, radioButton2.isSelected()); assertEquals(false, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[0], m.getValue()); m.setValue(TestPojo.intValueSet[2]); assertEquals(false, radioButton1.isSelected()); assertEquals(false, radioButton2.isSelected()); assertEquals(true, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[2], m.getValue()); m.setValue(TestPojo.intValueSet[1]); assertEquals(false, radioButton1.isSelected()); assertEquals(true, radioButton2.isSelected()); assertEquals(false, radioButton3.isSelected()); assertEquals(TestPojo.intValueSet[1], m.getValue()); }
@Test public void testBindListSelection() throws ValidationException { JList<Integer> list = new JList<>(new Integer[]{3, 4, 5, 6, 7}); Binding binding = bindingContextVB.bind("listValue", list, true); assertNotNull(binding); assertSame(list, getPrimaryComponent(binding)); assertNotNull(binding.getComponents()); assertEquals(1, binding.getComponents().length); assertEquals("listValue", list.getName()); list.setSelectedIndex(2); assertTrue(Arrays.equals(new int[]{5}, (int[]) propertyContainerVB.getValue("listValue"))); propertyContainerVB.setValue("listValue", new int[]{6}); assertEquals(6, list.getSelectedValue().intValue()); }
@Test public void testSecondaryComponent() throws Exception { JTextField textField = new JTextField(); Binding binding = bindingContextVB.bind("stringValue", textField); JLabel label = new JLabel("myLabel"); binding.addComponent(label); JComponent[] components = binding.getComponents(); assertNotNull(components); assertEquals(2, components.length); assertSame(getPrimaryComponent(binding), components[0]); assertSame(label, components[1]); }