use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.
the class AbstractNumericDolphinBinder method bidirectionalToNumeric.
@Override
public Binding bidirectionalToNumeric(final javafx.beans.property.Property<Number> javaFxProperty) {
if (javaFxProperty == null) {
throw new IllegalArgumentException("javaFxProperty must not be null");
}
final Binding unidirectionalBinding = toNumeric(javaFxProperty);
final Subscription subscription = property.onChanged(e -> {
if (!equals(javaFxProperty.getValue(), property.get())) {
javaFxProperty.setValue(getConverter().convertBack(property.get()));
}
});
return () -> {
unidirectionalBinding.unbind();
subscription.unsubscribe();
};
}
use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.
the class DefaultJavaFXBidirectionalBinder method bidirectionalTo.
@Override
public <T> Binding bidirectionalTo(final Property<T> property, final BidirectionalConverter<T, S> converter) {
final Binding unidirectionalBinding = to(property, converter);
final ChangeListener<S> listener = (obs, oldVal, newVal) -> property.set(converter.convertBack(newVal));
javaFxProperty.addListener(listener);
return () -> {
javaFxProperty.removeListener(listener);
unidirectionalBinding.unbind();
};
}
use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.
the class DefaultJavaFXListBinder method to.
@Override
public <T> Binding to(final ObservableList<T> dolphinList, final Function<? super T, ? extends S> converter) {
Assert.requireNonNull(dolphinList, "dolphinList");
Assert.requireNonNull(converter, "converter");
if (boundLists.containsKey(list)) {
throw new UnsupportedOperationException("A JavaFX list can only be bound to one Dolphin Platform list!");
}
boundLists.put(list, list);
final InternalListChangeListener<T> listChangeListener = new InternalListChangeListener<>(converter);
final Subscription subscription = dolphinList.onChanged(listChangeListener);
list.setAll(dolphinList.stream().map(converter).collect(Collectors.toList()));
ListChangeListener<S> readOnlyListener = c -> {
if (!listChangeListener.onChange) {
throw new UnsupportedOperationException("A JavaFX list that is bound to a dolphin list can only be modified by the binding!");
}
};
list.addListener(readOnlyListener);
return () -> {
subscription.unsubscribe();
list.removeListener(readOnlyListener);
boundLists.remove(list);
};
}
use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.
the class FXBinderTest method testJavaFXIntegerBidirectional.
@Test
public void testJavaFXIntegerBidirectional() {
Property<Integer> integerDolphinProperty = new MockedProperty<>();
Property<Number> numberDolphinProperty = new MockedProperty<>();
IntegerProperty integerJavaFXProperty = new SimpleIntegerProperty();
integerDolphinProperty.set(47);
assertNotEquals(integerJavaFXProperty.get(), 47);
Binding binding = FXBinder.bind(integerJavaFXProperty).bidirectionalToNumeric(integerDolphinProperty);
assertEquals(integerJavaFXProperty.get(), 47);
integerDolphinProperty.set(100);
assertEquals(integerJavaFXProperty.get(), 100);
integerDolphinProperty.set(null);
assertEquals(integerJavaFXProperty.get(), 0);
integerJavaFXProperty.set(12);
assertEquals(integerDolphinProperty.get().intValue(), 12);
integerJavaFXProperty.setValue(null);
assertEquals(integerDolphinProperty.get().intValue(), 0);
binding.unbind();
integerDolphinProperty.set(100);
assertEquals(integerJavaFXProperty.get(), 0);
numberDolphinProperty.set(12);
binding = FXBinder.bind(integerJavaFXProperty).bidirectionalTo(numberDolphinProperty);
assertEquals(integerJavaFXProperty.get(), 12);
numberDolphinProperty.set(null);
assertEquals(integerJavaFXProperty.get(), 0);
integerJavaFXProperty.set(12);
assertEquals(numberDolphinProperty.get().intValue(), 12);
integerJavaFXProperty.setValue(null);
assertEquals(numberDolphinProperty.get().intValue(), 0);
binding.unbind();
numberDolphinProperty.set(100);
assertEquals(integerJavaFXProperty.get(), 0);
}
use of com.canoo.platform.core.functional.Binding in project dolphin-platform by canoo.
the class FXBinderTest method testJavaFXBooleanUnidirectionalWithConverter.
@Test
public void testJavaFXBooleanUnidirectionalWithConverter() {
Property<String> stringDolphinProperty = new MockedProperty<>();
BooleanProperty booleanJavaFXProperty = new SimpleBooleanProperty();
WritableBooleanValue writableBooleanValue = new SimpleBooleanProperty();
Converter<String, Boolean> stringBooleanConverter = s -> s == null ? null : Boolean.parseBoolean(s);
stringDolphinProperty.set("Hello");
assertEquals(booleanJavaFXProperty.get(), false);
Binding binding = FXBinder.bind(booleanJavaFXProperty).to(stringDolphinProperty, stringBooleanConverter);
assertEquals(booleanJavaFXProperty.get(), false);
stringDolphinProperty.set("true");
assertEquals(booleanJavaFXProperty.get(), true);
stringDolphinProperty.set(null);
assertEquals(booleanJavaFXProperty.get(), false);
binding.unbind();
stringDolphinProperty.set("true");
assertEquals(booleanJavaFXProperty.get(), false);
stringDolphinProperty.set("false");
binding = FXBinder.bind(writableBooleanValue).to(stringDolphinProperty, stringBooleanConverter);
assertEquals(writableBooleanValue.get(), false);
stringDolphinProperty.set("true");
assertEquals(writableBooleanValue.get(), true);
stringDolphinProperty.set(null);
assertEquals(writableBooleanValue.get(), false);
binding.unbind();
stringDolphinProperty.set("true");
assertEquals(writableBooleanValue.get(), false);
}
Aggregations