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