Search in sources :

Example 1 with Subscription

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

the class LogClientUtil method createObservableListFromLocalCache.

public static BoundLogList<LogMessage> createObservableListFromLocalCache() {
    final ObservableList<LogMessage> list = FXCollections.observableArrayList(DolphinLoggerFactory.getLogCache());
    final Subscription subscription = DolphinLoggerFactory.addListener(l -> {
        final List<LogMessage> currentCache = Collections.unmodifiableList(DolphinLoggerFactory.getLogCache());
        Platform.runLater(() -> {
            final List<LogMessage> toRemove = list.stream().filter(e -> !currentCache.contains(l)).collect(Collectors.toList());
            list.removeAll(toRemove);
            final List<LogMessage> toAdd = currentCache.stream().filter(e -> !list.contains(e)).collect(Collectors.toList());
            list.addAll(toAdd);
        });
    });
    return new BoundLogList(subscription, list);
}
Also used : Platform(javafx.application.Platform) List(java.util.List) Subscription(com.canoo.platform.core.functional.Subscription) DolphinLoggerFactory(com.canoo.impl.dp.logging.DolphinLoggerFactory) FXCollections(javafx.collections.FXCollections) ObservableList(javafx.collections.ObservableList) LogMessage(com.canoo.platform.logging.spi.LogMessage) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) LogMessage(com.canoo.platform.logging.spi.LogMessage) Subscription(com.canoo.platform.core.functional.Subscription)

Example 2 with Subscription

use of com.canoo.platform.core.functional.Subscription 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();
    };
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Subscription(com.canoo.platform.core.functional.Subscription)

Example 3 with Subscription

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

the class DefaultJavaFXBinder method to.

@Override
public <T> Binding to(final Property<T> dolphinProperty, final Converter<? super T, ? extends S> converter) {
    Assert.requireNonNull(dolphinProperty, "dolphinProperty");
    Assert.requireNonNull(converter, "converter");
    final Subscription subscription = dolphinProperty.onChanged(event -> javaFxValue.setValue(converter.convert(dolphinProperty.get())));
    javaFxValue.setValue(converter.convert(dolphinProperty.get()));
    return () -> subscription.unsubscribe();
}
Also used : Subscription(com.canoo.platform.core.functional.Subscription)

Example 4 with Subscription

use of com.canoo.platform.core.functional.Subscription 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);
    };
}
Also used : Binding(com.canoo.platform.core.functional.Binding) Subscription(com.canoo.platform.core.functional.Subscription) IdentityHashMap(java.util.IdentityHashMap) ListChangeListener(javafx.collections.ListChangeListener) ObservableList(com.canoo.platform.remoting.ObservableList) Assert(com.canoo.dp.impl.platform.core.Assert) JavaFXListBinder(com.canoo.platform.remoting.client.javafx.binding.JavaFXListBinder) API(org.apiguardian.api.API) ListChangeEvent(com.canoo.platform.remoting.ListChangeEvent) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) INTERNAL(org.apiguardian.api.API.Status.INTERNAL) Subscription(com.canoo.platform.core.functional.Subscription)

Example 5 with Subscription

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

the class ContextManagerImplTests method testRemoveGlobalContext.

@Test
public void testRemoveGlobalContext() {
    // given:
    final ContextManager manager = new ContextManagerImpl();
    // when:
    final Subscription subscription = manager.addGlobalContext("KEY", "VALUE");
    subscription.unsubscribe();
    // then:
    Assert.assertEquals(manager.getGlobalContexts().size(), 5);
    checkForGlobalContextMissing(manager, "KEY");
}
Also used : ContextManager(com.canoo.platform.core.context.ContextManager) ContextManagerImpl(com.canoo.dp.impl.platform.core.context.ContextManagerImpl) Subscription(com.canoo.platform.core.functional.Subscription) Test(org.testng.annotations.Test)

Aggregations

Subscription (com.canoo.platform.core.functional.Subscription)30 Test (org.testng.annotations.Test)10 BeanManager (com.canoo.platform.remoting.BeanManager)7 ValueChangeEvent (com.canoo.platform.remoting.ValueChangeEvent)6 ValueChangeListener (com.canoo.platform.remoting.ValueChangeListener)6 TransformedPropertyImpl (com.canoo.dp.impl.reactive.TransformedPropertyImpl)4 AbstractDolphinBasedTest (com.canoo.dolphin.client.util.AbstractDolphinBasedTest)3 ClientModelStore (com.canoo.dp.impl.client.legacy.ClientModelStore)3 BeanRepository (com.canoo.dp.impl.remoting.BeanRepository)3 EventDispatcher (com.canoo.dp.impl.remoting.EventDispatcher)3 ServerModelStore (com.canoo.dp.impl.server.legacy.ServerModelStore)3 AbstractDolphinBasedTest (com.canoo.impl.server.util.AbstractDolphinBasedTest)3 Binding (com.canoo.platform.core.functional.Binding)3 SimpleTestModel (com.canoo.dolphin.client.util.SimpleTestModel)2 Assert (com.canoo.dp.impl.platform.core.Assert)2 ContextManagerImpl (com.canoo.dp.impl.platform.core.context.ContextManagerImpl)2 ObservableArrayList (com.canoo.dp.impl.remoting.collections.ObservableArrayList)2 SimpleTestModel (com.canoo.impl.server.util.SimpleTestModel)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2