Search in sources :

Example 21 with CanvasViolation

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

the class DrawCanvasCommand method execute.

@Override
@SuppressWarnings("unchecked")
public CommandResult<CanvasViolation> execute(final AbstractCanvasHandler context) {
    final Graph graph = context.getGraphIndex().getGraph();
    final String shapeSetId = getShapeSetId(context);
    final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> commandBuilder = new CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation>().forward();
    // Aggregate all nodes in the parent-child-dock hierarchy.
    childrenTraverseProcessor.traverse(graph, new AbstractChildrenTraverseCallback<Node<View, Edge>, Edge<Child, Node>>() {

        @Override
        public void startNodeTraversal(final Node<View, Edge> node) {
            super.startNodeTraversal(node);
            if (!isCanvasRoot().test(context, node.getUUID())) {
                addNode(node);
            }
        }

        @Override
        public boolean startNodeTraversal(final List<Node<View, Edge>> parents, final Node<View, Edge> node) {
            super.startNodeTraversal(parents, node);
            final Optional<Edge> dockEdge = node.getInEdges().stream().filter(e -> e.getContent() instanceof Dock).findAny();
            final Node parent = dockEdge.map(Edge::getSourceNode).orElseGet(() -> parents.get(parents.size() - 1));
            if (dockEdge.isPresent()) {
                addDockedNode(parent, node);
            } else if (isCanvasRoot().test(context, parent.getUUID())) {
                addNode(node);
            } else {
                addChildNode(parent, node);
            }
            return true;
        }

        private void addNode(final Node node) {
            commandBuilder.addCommand(new AddCanvasNodeCommand(node, shapeSetId));
        }

        private void addChildNode(final Node<View, Edge> parent, final Node<View, Edge> node) {
            commandBuilder.addCommand(new AddCanvasChildNodeCommand(parent, node, shapeSetId));
        }

        private void addDockedNode(final Node<View, Edge> parent, final Node<View, Edge> node) {
            commandBuilder.addCommand(new AddCanvasDockedNodeCommand(parent, node, shapeSetId));
        }
    });
    // Aggregate all connectors.
    viewTraverseProcessor.traverse(graph, new AbstractContentTraverseCallback<View<?>, Node<View, Edge>, Edge<View<?>, Node>>() {

        @Override
        public void startEdgeTraversal(final Edge<View<?>, Node> edge) {
            super.startEdgeTraversal(edge);
            commandBuilder.addCommand(new AddCanvasConnectorCommand(edge, shapeSetId));
        }
    });
    return commandBuilder.build().execute(context);
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) Optional(java.util.Optional) Node(org.kie.workbench.common.stunner.core.graph.Node) View(org.kie.workbench.common.stunner.core.graph.content.view.View) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Dock(org.kie.workbench.common.stunner.core.graph.content.relationship.Dock) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 22 with CanvasViolation

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

the class AbstractElementBuilderControl method getElementCommands.

@SuppressWarnings("unchecked")
public void getElementCommands(final Element element, final Node<View<?>, Edge> parent, final double x, final double y, final CommandsCallback commandsCallback) {
    Command<AbstractCanvasHandler, CanvasViolation> command = null;
    if (element instanceof Node) {
        if (null != parent) {
            command = canvasCommandFactory.addChildNode(parent, (Node) element, getShapeSetId());
        } else {
            command = canvasCommandFactory.addNode((Node) element, getShapeSetId());
        }
    } else if (element instanceof Edge && null != parent) {
        command = canvasCommandFactory.addConnector(parent, (Edge) element, MagnetConnection.Builder.forElement(parent), getShapeSetId());
    } else {
        throw new RuntimeException("Unrecognized element type for " + element);
    }
    // Execute both add element and move commands in batch, so undo will be done in batch as well.
    Command<AbstractCanvasHandler, CanvasViolation> moveCanvasElementCommand = canvasCommandFactory.updatePosition((Node<View<?>, Edge>) element, new Point2D(x, y));
    final List<Command<AbstractCanvasHandler, CanvasViolation>> commandList = new LinkedList<Command<AbstractCanvasHandler, CanvasViolation>>();
    commandList.add(command);
    commandList.add(moveCanvasElementCommand);
    commandsCallback.onComplete(element.getUUID(), commandList);
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Node(org.kie.workbench.common.stunner.core.graph.Node) Edge(org.kie.workbench.common.stunner.core.graph.Edge) View(org.kie.workbench.common.stunner.core.graph.content.view.View) LinkedList(java.util.LinkedList)

Example 23 with CanvasViolation

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

the class DeleteSelectionSessionCommand method execute.

@Override
@SuppressWarnings("unchecked")
public <V> void execute(final Callback<V> callback) {
    checkNotNull("callback", callback);
    if (null != getSession().getSelectionControl()) {
        final AbstractCanvasHandler canvasHandler = (AbstractCanvasHandler) getSession().getCanvasHandler();
        final SelectionControl<AbstractCanvasHandler, Element> selectionControl = getSession().getSelectionControl();
        final Collection<String> selectedItems = selectionControl.getSelectedItems();
        if (selectedItems != null && !selectedItems.isEmpty()) {
            // Execute the commands.
            final CommandResult<CanvasViolation> result = sessionCommandManager.execute(canvasHandler, canvasCommandFactory.delete(selectedItems.stream().map(uuid -> canvasHandler.getGraphIndex().get(uuid)).collect(Collectors.toList())));
            // Check the results.
            if (!CommandUtils.isError(result)) {
                callback.onSuccess();
            } else {
                callback.onError((V) new ClientRuntimeError("Error deleing elements [message=" + result.toString() + "]"));
            }
        } else {
            callback.onError((V) new ClientRuntimeError("Cannot delete element, no element selected on canvas"));
        }
        selectionControl.clearSelection();
        clearSelectionEvent.fire(new CanvasClearSelectionEvent(getCanvasHandler()));
    }
}
Also used : Event(javax.enterprise.event.Event) CanvasClearSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasClearSelectionEvent) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) Session(org.kie.workbench.common.stunner.core.client.session.Session) CanvasCommandFactory(org.kie.workbench.common.stunner.core.client.command.CanvasCommandFactory) Collection(java.util.Collection) PortablePreconditions.checkNotNull(org.kie.soup.commons.validation.PortablePreconditions.checkNotNull) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) CanvasElementsClearEvent(org.kie.workbench.common.stunner.core.client.canvas.event.registration.CanvasElementsClearEvent) Logger(java.util.logging.Logger) ClientRuntimeError(org.kie.workbench.common.stunner.core.client.service.ClientRuntimeError) Collectors(java.util.stream.Collectors) SessionCommandManager(org.kie.workbench.common.stunner.core.client.command.SessionCommandManager) Level(java.util.logging.Level) Inject(javax.inject.Inject) KeysMatcher(org.kie.workbench.common.stunner.core.client.canvas.controls.keyboard.KeysMatcher) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) Dependent(javax.enterprise.context.Dependent) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Element(org.kie.workbench.common.stunner.core.graph.Element) KeyboardEvent(org.kie.workbench.common.stunner.core.client.event.keyboard.KeyboardEvent) ClientFullSession(org.kie.workbench.common.stunner.core.client.session.ClientFullSession) SelectionControl(org.kie.workbench.common.stunner.core.client.canvas.controls.select.SelectionControl) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) CanvasClearSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasClearSelectionEvent) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Element(org.kie.workbench.common.stunner.core.graph.Element) ClientRuntimeError(org.kie.workbench.common.stunner.core.client.service.ClientRuntimeError)

Example 24 with CanvasViolation

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

the class PasteSelectionSessionCommand method processConnectors.

private CommandResult<CanvasViolation> processConnectors(Counter processedNodesCountdown) {
    if (processedNodesCountdown.equalsToValue(0)) {
        final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> commandBuilder = createCommandBuilder();
        commandBuilder.addCommands(clipboardControl.getElements().stream().filter(element -> element instanceof Edge).filter(Objects::nonNull).map(edge -> (Edge) edge).filter(edge -> Objects.nonNull(edge.getSourceNode()) && Objects.nonNull(clonedElements.get(edge.getSourceNode().getUUID())) && Objects.nonNull(edge.getTargetNode()) && Objects.nonNull(clonedElements.get(edge.getTargetNode().getUUID()))).map(edge -> canvasCommandFactory.cloneConnector(edge, clonedElements.get(edge.getSourceNode().getUUID()), clonedElements.get(edge.getTargetNode().getUUID()), getCanvasHandler().getDiagram().getMetadata().getShapeSetId(), cloneEdgeCallback(edge))).collect(Collectors.toList()));
        return sessionCommandManager.execute(getCanvasHandler(), commandBuilder.build());
    }
    return new CanvasCommandResultBuilder().build();
}
Also used : Session(org.kie.workbench.common.stunner.core.client.session.Session) Edge(org.kie.workbench.common.stunner.core.graph.Edge) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) PortablePreconditions.checkNotNull(org.kie.soup.commons.validation.PortablePreconditions.checkNotNull) HashMap(java.util.HashMap) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) 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) AbstractClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.AbstractClientSessionCommand) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) Map(java.util.Map) Observes(javax.enterprise.event.Observes) Element(org.kie.workbench.common.stunner.core.graph.Element) ClipboardControl(org.kie.workbench.common.stunner.core.client.canvas.controls.clipboard.ClipboardControl) StreamSupport(java.util.stream.StreamSupport) ClientFullSession(org.kie.workbench.common.stunner.core.client.session.ClientFullSession) Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Event(javax.enterprise.event.Event) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) 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) SessionCommandManager(org.kie.workbench.common.stunner.core.client.command.SessionCommandManager) Objects(java.util.Objects) Consumer(java.util.function.Consumer) KeysMatcher.doKeysMatch(org.kie.workbench.common.stunner.core.client.canvas.controls.keyboard.KeysMatcher.doKeysMatch) Stream(java.util.stream.Stream) Dependent(javax.enterprise.context.Dependent) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Optional(java.util.Optional) Counter(org.kie.workbench.common.stunner.core.util.Counter) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Collections(java.util.Collections) Node(org.kie.workbench.common.stunner.core.graph.Node) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) Edge(org.kie.workbench.common.stunner.core.graph.Edge) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)

Example 25 with CanvasViolation

use of org.kie.workbench.common.stunner.core.client.command.CanvasViolation 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 -> command.undo(getCanvasHandler()));
            finalResult = executeCommands(nodesCommandBuilder, processedNodesCountdown);
            // after the clone execution than delete source elements again
            clipboardControl.getRollbackCommands().forEach(command -> command.execute(getCanvasHandler()));
        } 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 : Session(org.kie.workbench.common.stunner.core.client.session.Session) Edge(org.kie.workbench.common.stunner.core.graph.Edge) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) PortablePreconditions.checkNotNull(org.kie.soup.commons.validation.PortablePreconditions.checkNotNull) HashMap(java.util.HashMap) AbstractCanvas(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) 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) AbstractClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.AbstractClientSessionCommand) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) Map(java.util.Map) Observes(javax.enterprise.event.Observes) Element(org.kie.workbench.common.stunner.core.graph.Element) ClipboardControl(org.kie.workbench.common.stunner.core.client.canvas.controls.clipboard.ClipboardControl) StreamSupport(java.util.stream.StreamSupport) ClientFullSession(org.kie.workbench.common.stunner.core.client.session.ClientFullSession) Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Event(javax.enterprise.event.Event) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) 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) SessionCommandManager(org.kie.workbench.common.stunner.core.client.command.SessionCommandManager) Objects(java.util.Objects) Consumer(java.util.function.Consumer) KeysMatcher.doKeysMatch(org.kie.workbench.common.stunner.core.client.canvas.controls.keyboard.KeysMatcher.doKeysMatch) Stream(java.util.stream.Stream) Dependent(javax.enterprise.context.Dependent) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Optional(java.util.Optional) Counter(org.kie.workbench.common.stunner.core.util.Counter) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Collections(java.util.Collections) Node(org.kie.workbench.common.stunner.core.graph.Node) 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) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)

Aggregations

CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)48 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)35 Test (org.junit.Test)32 DMNGridRow (org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridRow)14 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)11 Node (org.kie.workbench.common.stunner.core.graph.Node)11 Edge (org.kie.workbench.common.stunner.core.graph.Edge)9 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)8 List (org.kie.workbench.common.dmn.api.definition.v1_1.List)7 Command (org.kie.workbench.common.stunner.core.command.Command)7 GraphCommandExecutionContext (org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext)7 BaseGridRow (org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridRow)6 DecisionRule (org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule)5 View (org.kie.workbench.common.stunner.core.graph.content.view.View)5 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)4 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)4 Element (org.kie.workbench.common.stunner.core.graph.Element)4 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)4 Collection (java.util.Collection)3 Optional (java.util.Optional)3