Search in sources :

Example 16 with Binding

use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.

the class PropertyBinderImpl method bind.

public <T> Binding bind(final Property<T> property, final Qualifier<T> qualifier) {
    Assert.requireNonNull(property, "property");
    Assert.requireNonNull(qualifier, "qualifier");
    if (property instanceof PropertyImpl) {
        try {
            final PropertyImpl p = (PropertyImpl) property;
            final Field attributeField = ReflectionHelper.getInheritedDeclaredField(PropertyImpl.class, "attribute");
            final ServerAttribute attribute = (ServerAttribute) ReflectionHelper.getPrivileged(attributeField, p);
            if (attribute == null) {
                throw new NullPointerException("attribute == null");
            }
            attribute.setQualifier(qualifier.getIdentifier());
            return new Binding() {

                @Override
                public void unbind() {
                    attribute.setQualifier(null);
                }
            };
        } catch (Exception e) {
            throw new BindingException("Can not bind the given property to the qualifier! Property: " + property + ", qualifier: " + qualifier, e);
        }
    } else {
        throw new BindingException("Can not bind the given property to the qualifier! Property: " + property + ", qualifier: " + qualifier);
    }
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Field(java.lang.reflect.Field) PropertyImpl(com.canoo.dp.impl.remoting.PropertyImpl) ServerAttribute(com.canoo.dp.impl.server.legacy.ServerAttribute) BindingException(com.canoo.dp.impl.remoting.BindingException) BindingException(com.canoo.dp.impl.remoting.BindingException)

Example 17 with Binding

use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.

the class FXBinderTest method testSeveralBinds.

@Test
public void testSeveralBinds() {
    ObservableList<String> dolphinList = new ObservableArrayList<>();
    javafx.collections.ObservableList<String> javaFXList = FXCollections.observableArrayList();
    for (int i = 0; i < 10; i++) {
        Binding binding = FXBinder.bind(javaFXList).to(dolphinList);
        binding.unbind();
    }
    dolphinList.addAll(Arrays.asList("A", "B", "C"));
    for (int i = 0; i < 10; i++) {
        Binding binding = FXBinder.bind(javaFXList).to(dolphinList);
        binding.unbind();
    }
}
Also used : Binding(com.canoo.platform.core.functional.Binding) ObservableArrayList(com.canoo.dp.impl.remoting.collections.ObservableArrayList) Test(org.testng.annotations.Test)

Example 18 with Binding

use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.

the class FXBinderTest method testJavaFXStringBidirectionalWithConverter.

@Test
public void testJavaFXStringBidirectionalWithConverter() {
    Property<Double> doubleDolphinProperty = new MockedProperty<>();
    StringProperty stringJavaFXProperty = new SimpleStringProperty();
    Converter<String, Double> doubleStringConverter = s -> s == null ? null : Double.parseDouble(s);
    Converter<Double, String> stringDoubleConverter = d -> d == null ? null : d.toString();
    BidirectionalConverter<Double, String> doubleStringBidirectionalConverter = new DefaultBidirectionalConverter<>(stringDoubleConverter, doubleStringConverter);
    doubleDolphinProperty.set(0.1);
    assertNotEquals(stringJavaFXProperty.get(), "0.1");
    Binding binding = FXBinder.bind(stringJavaFXProperty).bidirectionalTo(doubleDolphinProperty, doubleStringBidirectionalConverter);
    assertEquals(stringJavaFXProperty.get(), "0.1");
    doubleDolphinProperty.set(0.2);
    assertEquals(stringJavaFXProperty.get(), "0.2");
    doubleDolphinProperty.set(null);
    assertEquals(stringJavaFXProperty.get(), null);
    stringJavaFXProperty.set("0.1");
    assertEquals(doubleDolphinProperty.get(), 0.1);
    stringJavaFXProperty.setValue("0.2");
    assertEquals(doubleDolphinProperty.get(), 0.2);
    binding.unbind();
    doubleDolphinProperty.set(0.3);
    assertEquals(stringJavaFXProperty.get(), "0.2");
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Arrays(java.util.Arrays) WritableBooleanValue(javafx.beans.value.WritableBooleanValue) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) FXCollections(javafx.collections.FXCollections) Assert.assertEquals(org.testng.Assert.assertEquals) DoubleProperty(javafx.beans.property.DoubleProperty) Test(org.testng.annotations.Test) FXBinder(com.canoo.platform.remoting.client.javafx.FXBinder) DefaultBidirectionalConverter(com.canoo.dp.impl.client.javafx.DefaultBidirectionalConverter) Property(com.canoo.platform.remoting.Property) IntegerProperty(javafx.beans.property.IntegerProperty) ArrayList(java.util.ArrayList) WritableStringValue(javafx.beans.value.WritableStringValue) SimpleIntegerProperty(javafx.beans.property.SimpleIntegerProperty) MockedProperty(com.canoo.dp.impl.remoting.MockedProperty) ObservableArrayList(com.canoo.dp.impl.remoting.collections.ObservableArrayList) Assert.assertNotEquals(org.testng.Assert.assertNotEquals) ObservableList(com.canoo.platform.remoting.ObservableList) Converter(com.canoo.platform.remoting.client.javafx.Converter) BidirectionalConverter(com.canoo.platform.remoting.client.javafx.BidirectionalConverter) BooleanProperty(javafx.beans.property.BooleanProperty) List(java.util.List) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) WritableIntegerValue(javafx.beans.value.WritableIntegerValue) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) Assert.assertTrue(org.testng.Assert.assertTrue) StringProperty(javafx.beans.property.StringProperty) WritableDoubleValue(javafx.beans.value.WritableDoubleValue) Binding(com.canoo.platform.core.functional.Binding) MockedProperty(com.canoo.dp.impl.remoting.MockedProperty) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) StringProperty(javafx.beans.property.StringProperty) SimpleStringProperty(javafx.beans.property.SimpleStringProperty) DefaultBidirectionalConverter(com.canoo.dp.impl.client.javafx.DefaultBidirectionalConverter) Test(org.testng.annotations.Test)

Example 19 with Binding

use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.

the class FXBinderTest method testJavaFXDoubleUnidirectional.

@Test
public void testJavaFXDoubleUnidirectional() {
    Property<Double> doubleDolphinProperty = new MockedProperty<>();
    Property<Number> numberDolphinProperty = new MockedProperty<>();
    DoubleProperty doubleJavaFXProperty = new SimpleDoubleProperty();
    WritableDoubleValue writableDoubleValue = new SimpleDoubleProperty();
    doubleDolphinProperty.set(47.0);
    assertNotEquals(doubleJavaFXProperty.doubleValue(), 47.0, EPSILON);
    Binding binding = FXBinder.bind(doubleJavaFXProperty).to(doubleDolphinProperty);
    assertEquals(doubleJavaFXProperty.doubleValue(), 47.0, EPSILON);
    doubleDolphinProperty.set(100.0);
    assertEquals(doubleJavaFXProperty.doubleValue(), 100.0, EPSILON);
    doubleDolphinProperty.set(null);
    assertEquals(doubleJavaFXProperty.doubleValue(), 0.0, EPSILON);
    binding.unbind();
    doubleDolphinProperty.set(100.0);
    assertEquals(doubleJavaFXProperty.doubleValue(), 0.0, EPSILON);
    numberDolphinProperty.set(12.0);
    binding = FXBinder.bind(doubleJavaFXProperty).to(numberDolphinProperty);
    assertEquals(doubleJavaFXProperty.doubleValue(), 12.0, EPSILON);
    numberDolphinProperty.set(null);
    assertEquals(doubleJavaFXProperty.doubleValue(), 0.0, EPSILON);
    binding.unbind();
    numberDolphinProperty.set(100.0);
    assertEquals(doubleJavaFXProperty.doubleValue(), 0.0, EPSILON);
    doubleDolphinProperty.set(47.0);
    binding = FXBinder.bind(writableDoubleValue).to(doubleDolphinProperty);
    assertEquals(writableDoubleValue.get(), 47.0, EPSILON);
    doubleDolphinProperty.set(100.0);
    assertEquals(writableDoubleValue.get(), 100.0, EPSILON);
    doubleDolphinProperty.set(null);
    assertEquals(writableDoubleValue.get(), 0.0, EPSILON);
    binding.unbind();
    doubleDolphinProperty.set(100.0);
    assertEquals(writableDoubleValue.get(), 0.0, EPSILON);
}
Also used : Binding(com.canoo.platform.core.functional.Binding) MockedProperty(com.canoo.dp.impl.remoting.MockedProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) DoubleProperty(javafx.beans.property.DoubleProperty) SimpleDoubleProperty(javafx.beans.property.SimpleDoubleProperty) WritableDoubleValue(javafx.beans.value.WritableDoubleValue) Test(org.testng.annotations.Test)

Example 20 with Binding

use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.

the class FXBinderTest method testJavaFXBooleanBidirectional.

@Test
public void testJavaFXBooleanBidirectional() {
    Property<Boolean> booleanDolphinProperty = new MockedProperty<>();
    BooleanProperty booleanJavaFXProperty = new SimpleBooleanProperty();
    booleanDolphinProperty.set(true);
    assertNotEquals(booleanJavaFXProperty.get(), true);
    Binding binding = FXBinder.bind(booleanJavaFXProperty).bidirectionalTo(booleanDolphinProperty);
    assertEquals(booleanJavaFXProperty.get(), true);
    booleanDolphinProperty.set(false);
    assertEquals(booleanJavaFXProperty.get(), false);
    booleanDolphinProperty.set(null);
    assertEquals(booleanJavaFXProperty.get(), false);
    booleanJavaFXProperty.set(true);
    assertEquals(booleanDolphinProperty.get().booleanValue(), true);
    booleanJavaFXProperty.setValue(null);
    assertEquals(booleanDolphinProperty.get().booleanValue(), false);
    binding.unbind();
    booleanDolphinProperty.set(true);
    assertEquals(booleanJavaFXProperty.get(), false);
}
Also used : Binding(com.canoo.platform.core.functional.Binding) MockedProperty(com.canoo.dp.impl.remoting.MockedProperty) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) BooleanProperty(javafx.beans.property.BooleanProperty) SimpleBooleanProperty(javafx.beans.property.SimpleBooleanProperty) Test(org.testng.annotations.Test)

Aggregations

Binding (com.canoo.platform.core.functional.Binding)28 Test (org.testng.annotations.Test)21 MockedProperty (com.canoo.dp.impl.remoting.MockedProperty)15 ObservableArrayList (com.canoo.dp.impl.remoting.collections.ObservableArrayList)11 SimpleStringProperty (javafx.beans.property.SimpleStringProperty)9 StringProperty (javafx.beans.property.StringProperty)9 Property (com.canoo.platform.remoting.Property)8 BidirectionalConverter (com.canoo.platform.remoting.client.javafx.BidirectionalConverter)8 BooleanProperty (javafx.beans.property.BooleanProperty)7 DoubleProperty (javafx.beans.property.DoubleProperty)7 IntegerProperty (javafx.beans.property.IntegerProperty)7 SimpleBooleanProperty (javafx.beans.property.SimpleBooleanProperty)7 SimpleDoubleProperty (javafx.beans.property.SimpleDoubleProperty)7 SimpleIntegerProperty (javafx.beans.property.SimpleIntegerProperty)7 ObservableList (com.canoo.platform.remoting.ObservableList)6 WritableBooleanValue (javafx.beans.value.WritableBooleanValue)6 WritableDoubleValue (javafx.beans.value.WritableDoubleValue)6 WritableIntegerValue (javafx.beans.value.WritableIntegerValue)6 WritableStringValue (javafx.beans.value.WritableStringValue)6 DefaultBidirectionalConverter (com.canoo.dp.impl.client.javafx.DefaultBidirectionalConverter)5