Search in sources :

Example 1 with ReverseCommand

use of org.kie.workbench.common.stunner.core.command.impl.ReverseCommand in project kie-wb-common by kiegroup.

the class PasteSelectionSessionCommand method execute.

@Override
public <V> void execute(final Callback<V> callback) {
    checkNotNull("callback", callback);
    if (clipboardControl.hasElements()) {
        final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> nodesCommandBuilder = createCommandBuilder();
        Counter processedNodesCountdown = new Counter((int) clipboardControl.getElements().stream().filter(element -> element instanceof Node).count());
        // first processing nodes
        nodesCommandBuilder.addCommands(clipboardControl.getElements().stream().filter(element -> element instanceof Node).filter(Objects::nonNull).map(node -> (Node<View<?>, Edge>) node).map(node -> {
            String newParentUUID = getNewParentUUID(node);
            return canvasCommandFactory.cloneNode(node, newParentUUID, calculateNewLocation(node, newParentUUID), cloneNodeCallback(node, processedNodesCountdown));
        }).collect(Collectors.toList()));
        if (Objects.equals(nodesCommandBuilder.size(), 0)) {
            return;
        }
        // Execute the command for cloning nodes
        CommandResult<CanvasViolation> finalResult;
        if (wasNodesDeletedFromGraph()) {
            // in case of a cut command the source elements were deleted from graph, so first undo the command to take node back into canvas
            clipboardControl.getRollbackCommands().forEach(command -> nodesCommandBuilder.addFirstCommand(new ReverseCommand(command)));
            // after the clone execution than delete source elements again
            clipboardControl.getRollbackCommands().forEach(node -> nodesCommandBuilder.addCommand(node));
            finalResult = executeCommands(nodesCommandBuilder, processedNodesCountdown);
        } else {
            // if elements are still on the graph, in case copy command, just execute the clone commands
            finalResult = executeCommands(nodesCommandBuilder, processedNodesCountdown);
        }
        if (CommandUtils.isError(finalResult)) {
            LOGGER.severe("Error pasting selection." + getCanvasViolations(finalResult));
            return;
        }
        fireSelectedElementEvent();
        callback.onSuccess();
        clear();
        // copy the cloned node to the clipboard to allow pasting several times
        copySelectionSessionCommand.execute();
    }
}
Also used : DefinitionUtils(org.kie.workbench.common.stunner.core.util.DefinitionUtils) ManagedInstance(org.jboss.errai.ioc.client.api.ManagedInstance) Edge(org.kie.workbench.common.stunner.core.graph.Edge) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) AbstractClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.AbstractClientSessionCommand) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) EditorSession(org.kie.workbench.common.stunner.core.client.session.impl.EditorSession) Map(java.util.Map) Observes(javax.enterprise.event.Observes) Any(javax.enterprise.inject.Any) Registry(org.appformer.client.stateControl.registry.Registry) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) CanvasCommandFactory(org.kie.workbench.common.stunner.core.client.command.CanvasCommandFactory) Collection(java.util.Collection) Key(org.kie.workbench.common.stunner.core.client.event.keyboard.KeyboardEvent.Key) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) ClientSession(org.kie.workbench.common.stunner.core.client.session.ClientSession) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Stream(java.util.stream.Stream) EdgeClipboard(org.kie.workbench.common.stunner.core.client.canvas.controls.EdgeClipboard) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Optional(java.util.Optional) Counter(org.kie.workbench.common.stunner.core.util.Counter) Node(org.kie.workbench.common.stunner.core.graph.Node) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) PortablePreconditions.checkNotNull(org.kie.soup.commons.validation.PortablePreconditions.checkNotNull) HashMap(java.util.HashMap) View(org.kie.workbench.common.stunner.core.graph.content.view.View) GraphUtils(org.kie.workbench.common.stunner.core.graph.util.GraphUtils) Inject(javax.inject.Inject) Element(org.kie.workbench.common.stunner.core.graph.Element) StreamSupport(java.util.stream.StreamSupport) Command(org.kie.workbench.common.stunner.core.command.Command) SessionManager(org.kie.workbench.common.stunner.core.client.api.SessionManager) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) ReverseCommand(org.kie.workbench.common.stunner.core.command.impl.ReverseCommand) Event(javax.enterprise.event.Event) Default(javax.enterprise.inject.Default) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) ClipboardControl(org.kie.workbench.common.stunner.core.client.canvas.controls.ClipboardControl) SessionCommandManager(org.kie.workbench.common.stunner.core.client.command.SessionCommandManager) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) Consumer(java.util.function.Consumer) KeysMatcher.doKeysMatch(org.kie.workbench.common.stunner.core.client.canvas.controls.keyboard.KeysMatcher.doKeysMatch) Dependent(javax.enterprise.context.Dependent) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Collections(java.util.Collections) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) Counter(org.kie.workbench.common.stunner.core.util.Counter) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Node(org.kie.workbench.common.stunner.core.graph.Node) Objects(java.util.Objects) ReverseCommand(org.kie.workbench.common.stunner.core.command.impl.ReverseCommand) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)

Aggregations

Collection (java.util.Collection)1 Collections (java.util.Collections)1 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 Consumer (java.util.function.Consumer)1 Logger (java.util.logging.Logger)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Dependent (javax.enterprise.context.Dependent)1 Event (javax.enterprise.event.Event)1 Observes (javax.enterprise.event.Observes)1 Any (javax.enterprise.inject.Any)1 Default (javax.enterprise.inject.Default)1 Inject (javax.inject.Inject)1 Registry (org.appformer.client.stateControl.registry.Registry)1 ManagedInstance (org.jboss.errai.ioc.client.api.ManagedInstance)1