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);
}
}
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();
}
}
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");
}
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);
}
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);
}
Aggregations