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