Search in sources :

Example 36 with Command

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

the class RedoCommandHandlerTest method testExecuteRemoveRedoCommands.

@Test
@SuppressWarnings("unchecked")
public void testExecuteRemoveRedoCommands() {
    Command command3 = mock(Command.class);
    assertTrue(tested.onUndoCommandExecuted(command1));
    assertTrue(tested.onUndoCommandExecuted(command2));
    assertFalse(tested.onCommandExecuted(command3));
    assertFalse(tested.isEnabled());
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) Test(org.junit.Test)

Example 37 with Command

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

the class AbstractNodeBuilder method afterNodeBuild.

@SuppressWarnings("unchecked")
protected void afterNodeBuild(final BuilderContext context, final T node) {
    // Outgoing connections.
    if (outgoingResourceIds != null && !outgoingResourceIds.isEmpty()) {
        for (String outgoingNodeId : outgoingResourceIds) {
            GraphObjectBuilder<?, ?> outgoingBuilder = getBuilder(context, outgoingNodeId);
            if (outgoingBuilder == null) {
                throw new RuntimeException("No outgoing edge builder for " + outgoingNodeId);
            }
            final List<Command<GraphCommandExecutionContext, RuleViolation>> commands = new LinkedList<>();
            // If outgoing element it's a node means that it's docked.
            if (outgoingBuilder instanceof AbstractNodeBuilder) {
                // Command - Create the docked node.
                Node docked = (Node) outgoingBuilder.build(context);
                commands.add(context.getCommandFactory().addDockedNode(node, docked));
                // Obtain docked position and use those for the docked node.
                final List<Double[]> dockers = ((AbstractNodeBuilder) outgoingBuilder).dockers;
                if (!dockers.isEmpty()) {
                    // TODO: Use not only first docker coordinates?
                    Double[] dCoords = dockers.get(0);
                    double x = dCoords[0];
                    double y = dCoords[1];
                    commands.add(context.getCommandFactory().updatePosition(docked, new Point2D(x, y)));
                }
            } else {
                // Create the outgoing edge.
                AbstractEdgeBuilder edgeBuilder = (AbstractEdgeBuilder) outgoingBuilder;
                Edge edge = (Edge) edgeBuilder.build(context);
                if (edge != null) {
                    // Command - Execute the graph command to set the node as the edge connection's source..
                    Double[] sourceDocker = null;
                    final List<Double[]> dockers = ((AbstractEdgeBuilder) outgoingBuilder).dockers;
                    if (dockers != null && dockers.size() > 1) {
                        sourceDocker = dockers.get(0);
                    }
                    Connection sourceConnection = null;
                    if (null != sourceDocker) {
                        sourceConnection = MagnetConnection.Builder.at(sourceDocker[0], sourceDocker[1]).setAuto(edgeBuilder.isSourceAutoConnection());
                    }
                    commands.add(context.getCommandFactory().setSourceNode(node, edge, sourceConnection));
                }
            }
            if (!commands.isEmpty()) {
                for (Command<GraphCommandExecutionContext, RuleViolation> command : commands) {
                    doExecuteCommand(context, command);
                }
            }
        }
    }
    // Children connections.
    if (childNodeIds != null && !childNodeIds.isEmpty()) {
        for (String childNodeId : childNodeIds) {
            GraphObjectBuilder<?, ?> childNodeBuilder = getBuilder(context, childNodeId);
            if (childNodeBuilder == null) {
                throw new RuntimeException("No child node builder for " + childNodeId);
            }
            Command<GraphCommandExecutionContext, RuleViolation> command = null;
            if (childNodeBuilder instanceof NodeObjectBuilder) {
                // Command - Create the child node and the parent-child relationship.
                Node childNode = (Node) childNodeBuilder.build(context);
                command = context.getCommandFactory().addChildNode(node, childNode);
            }
            if (null != command) {
                doExecuteCommand(context, command);
            }
        }
    }
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) LinkedList(java.util.LinkedList) Command(org.kie.workbench.common.stunner.core.command.Command) AddNodeCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AddNodeCommand) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 38 with Command

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

the class CreateNodeActionTest method testAction.

@Test
@SuppressWarnings("unchecked")
public void testAction() {
    when(canvasLayoutUtils.getNext(eq(canvasHandler), eq(element), eq(targetNode))).thenReturn(new Point2D(100d, 500d));
    final MouseClickEvent event = mock(MouseClickEvent.class);
    when(event.getX()).thenReturn(100d);
    when(event.getY()).thenReturn(500d);
    ToolboxAction<AbstractCanvasHandler> cascade = tested.onMouseClick(canvasHandler, NODE_UUID, event);
    assertEquals(tested, cascade);
    ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
    verify(sessionCommandManager, times(1)).execute(eq(canvasHandler), commandArgumentCaptor.capture());
    DeferredCompositeCommand command = (DeferredCompositeCommand) commandArgumentCaptor.getValue();
    DeferredCommand c0 = (DeferredCommand) command.getCommands().get(0);
    DeferredCommand c1 = (DeferredCommand) command.getCommands().get(1);
    DeferredCommand c2 = (DeferredCommand) command.getCommands().get(2);
    DeferredCommand c3 = (DeferredCommand) command.getCommands().get(3);
    AddNodeCommand addNodeCommand = (AddNodeCommand) c0.getCommand();
    UpdateElementPositionCommand updateElementPositionCommand = (UpdateElementPositionCommand) c1.getCommand();
    AddConnectorCommand addConnectorCommand = (AddConnectorCommand) c2.getCommand();
    SetConnectionTargetNodeCommand setTargetNodeCommand = (SetConnectionTargetNodeCommand) c3.getCommand();
    assertEquals(targetNode, addNodeCommand.getCandidate());
    assertEquals("ss1", addNodeCommand.getShapeSetId());
    assertEquals(edge, addConnectorCommand.getCandidate());
    assertEquals(element, addConnectorCommand.getSource());
    assertEquals("ss1", addConnectorCommand.getShapeSetId());
    assertEquals(edge, setTargetNodeCommand.getEdge());
    assertEquals(targetNode, setTargetNodeCommand.getNode());
    assertEquals(targetNode, updateElementPositionCommand.getElement());
    assertEquals(new Point2D(100d, 500d), updateElementPositionCommand.getLocation());
    final ArgumentCaptor<CanvasSelectionEvent> eventArgumentCaptor = ArgumentCaptor.forClass(CanvasSelectionEvent.class);
    verify(canvasElementSelectedEvent, times(1)).fire(eventArgumentCaptor.capture());
    final CanvasSelectionEvent eCaptured = eventArgumentCaptor.getValue();
    assertEquals(TARGET_NODE_UUID, eCaptured.getIdentifiers().iterator().next());
}
Also used : DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) CanvasSelectionEvent(org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) SetConnectionTargetNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.SetConnectionTargetNodeCommand) Command(org.kie.workbench.common.stunner.core.command.Command) DeferredCompositeCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCompositeCommand) AddNodeCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddNodeCommand) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) UpdateElementPositionCommand(org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand) AddConnectorCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AddConnectorCommand) MouseClickEvent(org.kie.workbench.common.stunner.core.client.shape.view.event.MouseClickEvent) DeferredCommand(org.kie.workbench.common.stunner.core.command.impl.DeferredCommand) Test(org.junit.Test)

Example 39 with Command

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

the class UndoSessionCommandTest method testOnCommandUndoExecuted.

@Test
@SuppressWarnings("unchecked")
public void testOnCommandUndoExecuted() {
    command.bind(session);
    command.listen(statusCallback);
    ((UndoSessionCommand) command).onCommandUndoExecuted(new CanvasUndoCommandExecutedEvent(null, null, null));
    assertFalse(command.isEnabled());
    commandHistory.add(mock(Command.class));
    ((UndoSessionCommand) command).onCommandUndoExecuted(new CanvasUndoCommandExecutedEvent(null, null, null));
    assertTrue(command.isEnabled());
    verify(statusCallback, times(2)).execute();
}
Also used : AbstractClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.AbstractClientSessionCommand) ClientSessionCommand(org.kie.workbench.common.stunner.core.client.session.command.ClientSessionCommand) Command(org.kie.workbench.common.stunner.core.command.Command) CanvasUndoCommandExecutedEvent(org.kie.workbench.common.stunner.core.client.canvas.event.command.CanvasUndoCommandExecutedEvent) Test(org.junit.Test)

Example 40 with Command

use of org.kie.workbench.common.stunner.core.command.Command 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)

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