Search in sources :

Example 1 with Command

use of com.vaadin.flow.server.Command in project flow by vaadin.

the class DndUtil method updateDropTargetActivation.

/**
 * Triggers drop target activation method in JS connector once when the
 * component has been attached. Will make sure the activation in JS is done
 * again when the component is detached and attached again, because
 * otherwise the element will not be a drop target again.
 *
 * @param dropTarget
 *            the drop target to update active status on
 * @param <T>
 *            the type of the drop target component
 */
public static <T extends Component> void updateDropTargetActivation(DropTarget<T> dropTarget) {
    Command command = () -> dropTarget.getElement().executeJs("window.Vaadin.Flow.dndConnector.updateDropTarget($0)", dropTarget.getElement());
    runOnAttachBeforeResponse(dropTarget.getDropTargetComponent(), command);
    // again on the client side if the component is removed and added again
    if (ComponentUtil.getData(dropTarget.getDropTargetComponent(), DETACH_LISTENER_FOR_DROP_TARGET) == null) {
        Registration detachRegistration = dropTarget.getElement().addDetachListener(event -> runOnAttachBeforeResponse(dropTarget.getDropTargetComponent(), command));
        ComponentUtil.setData(dropTarget.getDropTargetComponent(), DETACH_LISTENER_FOR_DROP_TARGET, detachRegistration);
    }
}
Also used : Command(com.vaadin.flow.server.Command) Registration(com.vaadin.flow.shared.Registration)

Example 2 with Command

use of com.vaadin.flow.server.Command in project flow by vaadin.

the class ElementAttributeMap method registerResource.

private void registerResource(String attribute, AbstractStreamResource resource) {
    ensureResourceRegistrations();
    ensurePendingRegistrations();
    assert !resourceRegistrations.containsKey(attribute);
    StreamRegistration registration = getSession().getResourceRegistry().registerResource(resource);
    resourceRegistrations.put(attribute, registration);
    Registration handle = pendingRegistrations.remove(attribute);
    if (handle != null) {
        handle.remove();
    }
    pendingRegistrations.put(attribute, getNode().addDetachListener(// see ElementAttributeMap#deferRegistration
    new Command() {

        @Override
        public void execute() {
            ElementAttributeMap.this.unsetResource(attribute);
        }
    }));
}
Also used : StreamRegistration(com.vaadin.flow.server.StreamRegistration) Command(com.vaadin.flow.server.Command) Registration(com.vaadin.flow.shared.Registration) StreamRegistration(com.vaadin.flow.server.StreamRegistration)

Example 3 with Command

use of com.vaadin.flow.server.Command in project flow by vaadin.

the class RouteConfigurationTest method setRoutes_allExpectedRoutesAreSet.

@Test
public void setRoutes_allExpectedRoutesAreSet() {
    RouteRegistry registry = mockRegistry();
    RouteConfiguration routeConfiguration = RouteConfiguration.forRegistry(registry);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ((Command) args[0]).execute();
        return null;
    }).when(registry).update(Mockito.any(Command.class));
    routeConfiguration.update(() -> {
        routeConfiguration.getHandledRegistry().clean();
        Arrays.asList(MyRoute.class, MyInfo.class, MyPalace.class, MyModular.class).forEach(routeConfiguration::setAnnotatedRoute);
    });
    Mockito.verify(registry).update(Mockito.any());
    Mockito.verify(registry).setRoute("home", MyRoute.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("info", MyInfo.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("palace", MyPalace.class, Collections.emptyList());
    Mockito.verify(registry).setRoute("modular", MyModular.class, Collections.emptyList());
}
Also used : ApplicationRouteRegistry(com.vaadin.flow.server.startup.ApplicationRouteRegistry) RouteRegistry(com.vaadin.flow.server.RouteRegistry) SessionRouteRegistry(com.vaadin.flow.server.SessionRouteRegistry) Command(com.vaadin.flow.server.Command) Test(org.junit.Test)

Example 4 with Command

use of com.vaadin.flow.server.Command in project flow by vaadin.

the class DndUtil method updateDragSourceActivation.

/**
 * Triggers drag source activation method in JS connector once when the
 * component has been attached.
 *
 * @param dragSource
 *            the drag source to update active status on
 * @param <T>
 *            the type of the drag source component
 */
public static <T extends Component> void updateDragSourceActivation(DragSource<T> dragSource) {
    Command command = () -> dragSource.getDraggableElement().executeJs("window.Vaadin.Flow.dndConnector.updateDragSource($0)", dragSource.getDraggableElement());
    runOnAttachBeforeResponse(dragSource.getDragSourceComponent(), command);
}
Also used : Command(com.vaadin.flow.server.Command)

Example 5 with Command

use of com.vaadin.flow.server.Command in project flow by vaadin.

the class RegistrationTest method once_onlyCalledOnce.

@Test
public void once_onlyCalledOnce() {
    AtomicBoolean invoked = new AtomicBoolean();
    Command action = () -> {
        boolean calledPreviously = invoked.getAndSet(true);
        Assert.assertFalse("Command should not invoked previously", calledPreviously);
    };
    Registration registration = Registration.once(action);
    Assert.assertFalse("Command should not yet be invoked", invoked.get());
    registration.remove();
    Assert.assertTrue("Command should be invoked", invoked.get());
    // Action will throw if invoked again
    registration.remove();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Command(com.vaadin.flow.server.Command) Test(org.junit.Test)

Aggregations

Command (com.vaadin.flow.server.Command)6 Registration (com.vaadin.flow.shared.Registration)3 StreamRegistration (com.vaadin.flow.server.StreamRegistration)2 Test (org.junit.Test)2 RouteRegistry (com.vaadin.flow.server.RouteRegistry)1 SessionRouteRegistry (com.vaadin.flow.server.SessionRouteRegistry)1 ApplicationRouteRegistry (com.vaadin.flow.server.startup.ApplicationRouteRegistry)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1