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