Search in sources :

Example 1 with Command

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

the class SafeDeleteNodeCommand method initialize.

@Override
@SuppressWarnings("unchecked")
protected SafeDeleteNodeCommand initialize(final GraphCommandExecutionContext context) {
    super.initialize(context);
    final Graph<?, Node> graph = getGraph(context);
    final Node<Definition<?>, Edge> candidate = (Node<Definition<?>, Edge>) getCandidate(context);
    new SafeDeleteNodeProcessor(new ChildrenTraverseProcessorImpl(new TreeWalkTraverseProcessorImpl()), graph, candidate).run(new SafeDeleteNodeProcessor.Callback() {

        private final Set<String> processedConnectors = new HashSet<String>();

        @Override
        public void deleteCandidateConnector(final Edge<? extends View<?>, Node> edge) {
        // This command will delete candidate's connectors once deleting the candidate node later on,
        // as it potentially performs the connectors shortcut operation.
        }

        @Override
        public void deleteConnector(final Edge<? extends View<?>, Node> edge) {
            doDeleteConnector(edge);
        }

        @Override
        public void removeChild(final Element<?> parent, final Node<?, Edge> candidate) {
            log("RemoveChildCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
            addCommand(new RemoveChildCommand((Node<?, Edge>) parent, candidate));
            safeDeleteCallback.ifPresent(c -> c.removeChild(parent, candidate));
        }

        @Override
        public void removeDock(final Node<?, Edge> parent, final Node<?, Edge> candidate) {
            log("UnDockNodeCommand [parent=" + parent.getUUID() + ", candidate=" + candidate.getUUID() + "]");
            addCommand(new UnDockNodeCommand(parent, candidate));
            safeDeleteCallback.ifPresent(c -> c.removeDock(parent, candidate));
        }

        @Override
        public void deleteCandidateNode(final Node<?, Edge> node) {
            processCandidateConnectors();
            deleteNode(node);
        }

        @Override
        public void deleteNode(final Node<?, Edge> node) {
            log("DeregisterNodeCommand [node=" + node.getUUID() + "]");
            addCommand(new DeregisterNodeCommand(node));
            safeDeleteCallback.ifPresent(c -> c.deleteNode(node));
        }

        private void processCandidateConnectors() {
            if (options.isDeleteCandidateConnectors()) {
                if (options.isShortcutCandidateConnectors() && hasSingleIncomingEdge().and(hasSingleOutgoingEdge()).test(candidate)) {
                    final Edge<? extends ViewConnector<?>, Node> in = getViewConnector().apply(candidate.getInEdges());
                    final Edge<? extends ViewConnector<?>, Node> out = getViewConnector().apply(candidate.getOutEdges());
                    shortcut(in, out);
                } else {
                    Stream.concat(candidate.getInEdges().stream(), candidate.getOutEdges().stream()).filter(e -> e.getContent() instanceof ViewConnector).forEach(this::deleteConnector);
                }
            }
        }

        private void shortcut(final Edge<? extends ViewConnector<?>, Node> in, final Edge<? extends ViewConnector<?>, Node> out) {
            final ViewConnector<?> outContent = out.getContent();
            final Node targetNode = out.getTargetNode();
            addCommand(new DeleteConnectorCommand(out));
            safeDeleteCallback.ifPresent(c -> c.deleteCandidateConnector(out));
            addCommand(new SetConnectionTargetNodeCommand(targetNode, in, outContent.getTargetConnection().orElse(null)));
            safeDeleteCallback.ifPresent(c -> c.setEdgeTargetNode(targetNode, in));
        }

        private void doDeleteConnector(final Edge<? extends View<?>, Node> edge) {
            if (!processedConnectors.contains(edge.getUUID())) {
                log("IN DoDeleteConnector [edge=" + edge.getUUID() + "]");
                addCommand(new DeleteConnectorCommand(edge));
                safeDeleteCallback.ifPresent(c -> c.deleteConnector(edge));
                processedConnectors.add(edge.getUUID());
            }
        }
    });
    return this;
}
Also used : Edge(org.kie.workbench.common.stunner.core.graph.Edge) Portable(org.jboss.errai.common.client.api.annotations.Portable) Function(java.util.function.Function) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Level(java.util.logging.Level) HashSet(java.util.HashSet) SafeDeleteNodeProcessor(org.kie.workbench.common.stunner.core.graph.util.SafeDeleteNodeProcessor) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) NonPortable(org.jboss.errai.common.client.api.annotations.NonPortable) ChildrenTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ChildrenTraverseProcessorImpl) Element(org.kie.workbench.common.stunner.core.graph.Element) PortablePreconditions(org.kie.soup.commons.validation.PortablePreconditions) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) CardinalityContext(org.kie.workbench.common.stunner.core.rule.context.CardinalityContext) Predicate(java.util.function.Predicate) Collection(java.util.Collection) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) Set(java.util.Set) Logger(java.util.logging.Logger) RuleContextBuilder(org.kie.workbench.common.stunner.core.rule.context.impl.RuleContextBuilder) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) List(java.util.List) Graph(org.kie.workbench.common.stunner.core.graph.Graph) Stream(java.util.stream.Stream) CommandUtils(org.kie.workbench.common.stunner.core.command.util.CommandUtils) Optional(java.util.Optional) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) MapsTo(org.jboss.errai.common.client.api.annotations.MapsTo) Node(org.kie.workbench.common.stunner.core.graph.Node) ViewConnector(org.kie.workbench.common.stunner.core.graph.content.view.ViewConnector) Node(org.kie.workbench.common.stunner.core.graph.Node) Definition(org.kie.workbench.common.stunner.core.graph.content.definition.Definition) SafeDeleteNodeProcessor(org.kie.workbench.common.stunner.core.graph.util.SafeDeleteNodeProcessor) TreeWalkTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.tree.TreeWalkTraverseProcessorImpl) ChildrenTraverseProcessorImpl(org.kie.workbench.common.stunner.core.graph.processing.traverse.content.ChildrenTraverseProcessorImpl) Edge(org.kie.workbench.common.stunner.core.graph.Edge) HashSet(java.util.HashSet)

Example 2 with Command

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

the class CommandRegistryListenerTest method testUndoFailed.

@Test
@SuppressWarnings("unchecked")
public void testUndoFailed() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.ERROR);
    tested.onUndo(context, command, result);
    verify(commandRegistry, times(0)).pop();
    verify(commandRegistry, times(0)).peek();
    verify(commandRegistry, times(0)).register(any(Command.class));
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 3 with Command

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

the class CommandRegistryListenerTest method testExecuteSuccess.

@Test
@SuppressWarnings("unchecked")
public void testExecuteSuccess() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.INFO);
    tested.onExecute(context, command, result);
    verify(commandRegistry, times(1)).register(eq(command));
    verify(commandRegistry, times(0)).pop();
    verify(commandRegistry, times(0)).peek();
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 4 with Command

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

the class CommandRegistryListenerTest method testUndoSuccess.

@Test
@SuppressWarnings("unchecked")
public void testUndoSuccess() {
    Object context = mock(Object.class);
    Command command = mock(Command.class);
    CommandResult result = mock(CommandResult.class);
    when(result.getType()).thenReturn(CommandResult.Type.INFO);
    tested.onUndo(context, command, result);
    verify(commandRegistry, times(1)).pop();
    verify(commandRegistry, times(0)).peek();
    verify(commandRegistry, times(0)).register(any(Command.class));
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Example 5 with Command

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

the class CompositeCommandTest method testExecuteFailedThenUndo.

@Test
@SuppressWarnings("unchecked")
public void testExecuteFailedThenUndo() {
    Command c1 = mock(Command.class);
    when(c1.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c1.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    Command c2 = mock(Command.class);
    when(c2.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c2.execute(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    CommandResult<RuleViolation> failed = new CommandResultImpl<>(CommandResult.Type.ERROR, Collections.singletonList(new RuleViolationImpl("failed")));
    Command c3 = mock(Command.class);
    when(c3.allow(eq(commandExecutionContext))).thenReturn(GraphCommandResultBuilder.SUCCESS);
    when(c3.execute(eq(commandExecutionContext))).thenReturn(failed);
    CompositeCommand composite = new CompositeCommand.Builder<>().addCommand(c1).addCommand(c2).addCommand(c3).build();
    CommandResult result = composite.execute(commandExecutionContext);
    assertEquals(CommandResult.Type.ERROR, result.getType());
    verify(c1, times(1)).undo(eq(commandExecutionContext));
    verify(c2, times(1)).undo(eq(commandExecutionContext));
    verify(c3, times(1)).undo(eq(commandExecutionContext));
}
Also used : RuleViolationImpl(org.kie.workbench.common.stunner.core.rule.violations.RuleViolationImpl) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Test(org.junit.Test)

Aggregations

Command (org.kie.workbench.common.stunner.core.command.Command)74 Test (org.junit.Test)53 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)23 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)22 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)19 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)16 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)14 Edge (org.kie.workbench.common.stunner.core.graph.Edge)12 Node (org.kie.workbench.common.stunner.core.graph.Node)12 List (java.util.List)10 GraphCommandResultBuilder (org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder)10 AbstractGraphCommand (org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand)10 View (org.kie.workbench.common.stunner.core.graph.content.view.View)9 SetCellValueCommand (org.kie.workbench.common.dmn.client.commands.general.SetCellValueCommand)7 Consumer (java.util.function.Consumer)6 UpdateElementPositionCommand (org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand)6 ArrayList (java.util.ArrayList)5 AddNodeCommand (org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand)5 Element (org.kie.workbench.common.stunner.core.graph.Element)5 GraphCommandExecutionContext (org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext)5