use of org.kie.workbench.common.stunner.core.command.Command in project kie-wb-common by kiegroup.
the class AbstractElementBuilderControl method build.
@Override
@SuppressWarnings("unchecked")
public void build(final ElementBuildRequest<AbstractCanvasHandler> request, final BuildCallback buildCallback) {
if (null == canvasHandler) {
buildCallback.onSuccess(null);
return;
}
double x = request.getX();
double y = request.getY();
final Object definition = request.getDefinition();
// Notify processing starts.
fireProcessingStarted();
final Node<View<?>, Edge> parent = getParent(x, y);
final Point2D childCoordinates = getComputedChildCoordinates(parent, x, y);
getCommands(definition, parent, childCoordinates.getX(), childCoordinates.getY(), new CommandsCallback() {
@Override
public void onComplete(final String uuid, final List<Command<AbstractCanvasHandler, CanvasViolation>> commands) {
final CommandResult<CanvasViolation> result = getCommandManager().execute(canvasHandler, new CompositeCommand.Builder().addCommands(commands).build());
if (!CommandUtils.isError(result)) {
buildCallback.onSuccess(uuid);
} else {
final String message = translationMessages.getCanvasCommandValidationsErrorMessage(result.getViolations());
buildCallback.onError(new ClientRuntimeError(message));
}
// Notify processing ends.
fireProcessingCompleted();
}
@Override
public void onError(final ClientRuntimeError error) {
buildCallback.onError(error);
// Notify processing ends.
fireProcessingCompleted();
}
});
}
use of org.kie.workbench.common.stunner.core.command.Command in project kie-wb-common by kiegroup.
the class GeneralCreateNodeActionTest method testExecuteAction.
@Test
public void testExecuteAction() {
final AbstractCanvasHandler canvasHandler = mock(AbstractCanvasHandler.class);
final String sourceNodeId = "src-id";
final String targetNodeId = "dest-id";
final String connectorId = "edge-id";
final AbstractCanvas canvas = mock(AbstractCanvas.class);
doReturn(canvas).when(canvasHandler).getCanvas();
doReturn(canvas).when(canvasHandler).getAbstractCanvas();
final Diagram diagram = mock(Diagram.class);
doReturn(diagram).when(canvasHandler).getDiagram();
final Metadata metadata = mock(Metadata.class);
doReturn(metadata).when(diagram).getMetadata();
final String shapeSetId = "shape-set-id";
doReturn(shapeSetId).when(metadata).getShapeSetId();
final Index graphIndex = mock(Index.class);
doReturn(graphIndex).when(canvasHandler).getGraphIndex();
final Element sourceElement = mock(Element.class);
doReturn(sourceElement).when(graphIndex).get(sourceNodeId);
final Node sourceNode = mock(Node.class);
final View sourceElementContent = mock(View.class);
doReturn(sourceElementContent).when(sourceNode).getContent();
doReturn(Bounds.create(10d, 0d, 200d, 100d)).when(sourceElementContent).getBounds();
doReturn(sourceNode).when(sourceElement).asNode();
doReturn(Collections.emptyList()).when(sourceNode).getInEdges();
final Element targetNodeElement = mock(Element.class);
doReturn(targetNodeElement).when(clientFactoryManager).newElement(anyString(), eq(targetNodeId));
final Node targetNode = mock(Node.class);
final View targetElementContent = mock(View.class);
doReturn(targetElementContent).when(targetNode).getContent();
doReturn(Bounds.create(-100d, 0d, 0d, 100d)).when(targetElementContent).getBounds();
doReturn(targetNode).when(targetNodeElement).asNode();
final String targetNodeUuid = "target-uuid";
doReturn(targetNodeUuid).when(targetNode).getUUID();
final Element connectorElement = mock(Element.class);
doReturn(connectorElement).when(clientFactoryManager).newElement(anyString(), eq(connectorId));
final Edge connectorEdge = mock(Edge.class);
doReturn(connectorEdge).when(connectorElement).asEdge();
when(canvasLayoutUtils.getNext(eq(canvasHandler), eq(sourceNode), eq(targetNode))).thenReturn(new Point2D(100d, 500d));
createNodeAction.executeAction(canvasHandler, sourceNodeId, targetNodeId, connectorId);
final ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
verify(sessionCommandManager).execute(eq(canvasHandler), commandArgumentCaptor.capture());
final DeferredCompositeCommand command = (DeferredCompositeCommand) commandArgumentCaptor.getValue();
final DeferredCommand c0 = (DeferredCommand) command.getCommands().get(0);
final DeferredCommand c1 = (DeferredCommand) command.getCommands().get(1);
final DeferredCommand c2 = (DeferredCommand) command.getCommands().get(2);
final DeferredCommand c3 = (DeferredCommand) command.getCommands().get(3);
final AddNodeCommand addNodeCommand = (AddNodeCommand) c0.getCommand();
final UpdateElementPositionCommand updateElementPositionCommand = (UpdateElementPositionCommand) c1.getCommand();
final AddConnectorCommand addConnectorCommand = (AddConnectorCommand) c2.getCommand();
final SetConnectionTargetNodeCommand setTargetNodeCommand = (SetConnectionTargetNodeCommand) c3.getCommand();
Assertions.assertThat(targetNode).isEqualTo(addNodeCommand.getCandidate());
Assertions.assertThat(shapeSetId).isEqualTo(addNodeCommand.getShapeSetId());
Assertions.assertThat(connectorEdge).isEqualTo(addConnectorCommand.getCandidate());
Assertions.assertThat(sourceNode).isEqualTo(addConnectorCommand.getSource());
Assertions.assertThat(shapeSetId).isEqualTo(addConnectorCommand.getShapeSetId());
Assertions.assertThat(connectorEdge).isEqualTo(setTargetNodeCommand.getEdge());
Assertions.assertThat(targetNode).isEqualTo(setTargetNodeCommand.getNode());
Assertions.assertThat(targetNode).isEqualTo(updateElementPositionCommand.getElement());
Assertions.assertThat(new Point2D(100d, 500d)).isEqualTo(updateElementPositionCommand.getLocation());
final ArgumentCaptor<CanvasSelectionEvent> eventArgumentCaptor = ArgumentCaptor.forClass(CanvasSelectionEvent.class);
verify(selectionEvent).fire(eventArgumentCaptor.capture());
final CanvasSelectionEvent eCaptured = eventArgumentCaptor.getValue();
Assertions.assertThat(targetNodeUuid).isEqualTo(eCaptured.getIdentifiers().iterator().next());
Assertions.assertThat(addConnectorCommand.getConnection()).isInstanceOf(MagnetConnection.class);
Assertions.assertThat(((MagnetConnection) addConnectorCommand.getConnection()).getMagnetIndex().getAsInt()).isEqualTo(MagnetConnection.MAGNET_LEFT);
Assertions.assertThat(setTargetNodeCommand.getConnection()).isInstanceOf(MagnetConnection.class);
Assertions.assertThat(((MagnetConnection) setTargetNodeCommand.getConnection()).getMagnetIndex().getAsInt()).isEqualTo(MagnetConnection.MAGNET_RIGHT);
}
use of org.kie.workbench.common.stunner.core.command.Command in project kie-wb-common by kiegroup.
the class PasteSelectionSessionCommandTest method executeWithMultiSelection.
@Test
@SuppressWarnings("unchecked")
public void executeWithMultiSelection() {
pasteSelectionSessionCommand.bind(session);
pasteSelectionSessionCommand.setTestEdgeFoundInCanvas(true);
pasteSelectionSessionCommand.setTestEdgeFoundInClipboard(false);
// Mock the callback of CloneNodeCommand
ArgumentCaptor<Consumer> consumerNode = ArgumentCaptor.forClass(Consumer.class);
ArgumentCaptor<Consumer> consumerNode2 = ArgumentCaptor.forClass(Consumer.class);
when(cloneNodeCommand.getCandidate()).thenReturn(node);
when(cloneNodeCommand2.getCandidate()).thenReturn(node2);
when(canvasCommandFactory.cloneNode(eq(node), any(), any(), consumerNode.capture())).thenReturn(cloneNodeCommand);
when(canvasCommandFactory.cloneNode(eq(node2), any(), any(), consumerNode2.capture())).thenReturn(cloneNodeCommand2);
Map<Node, ArgumentCaptor<Consumer>> consumerMap = new HashMap() {
{
put(node, consumerNode);
put(node2, consumerNode2);
}
};
// Mock the callback of CloneConnectorCommand
ArgumentCaptor<Consumer> consumerEdge = ArgumentCaptor.forClass(Consumer.class);
when(canvasCommandFactory.cloneConnector(any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), consumerEdge.capture())).thenReturn(cloneConnectorCommand);
// apply callbacks mocks
when(sessionCommandManager.execute(eq(canvasHandler), any())).thenAnswer(param -> {
CompositeCommand argument = param.getArgument(1, CompositeCommand.class);
// callback to nodes
argument.getCommands().stream().filter(c -> c instanceof CloneNodeCommand).forEach(c -> {
CloneNodeCommand cloneNodeCommand = (CloneNodeCommand) c;
Node candidate = cloneNodeCommand.getCandidate();
consumerMap.get(candidate).getValue().accept(cloneMap.get(candidate));
});
// callback to connectors
argument.getCommands().stream().filter(c -> c instanceof CloneConnectorCommand).forEach(c -> consumerEdge.getValue().accept(cloneEdge));
return commandResult;
});
// Executing the command
clipboardControl.set(graphInstance.startNode, graphInstance.edge1, graphInstance.intermNode);
when(selectionControl.getSelectedItems()).thenReturn(Arrays.asList(graphInstance.startNode.getUUID(), graphInstance.edge1.getUUID(), graphInstance.intermNode.getUUID()));
CopySelectionSessionCommand.getInstance(sessionManager).bind(session);
pasteSelectionSessionCommand.execute(callback);
verify(canvasCommandFactory, times(1)).cloneNode(eq(graphInstance.startNode), eq(graphInstance.parentNode.getUUID()), eq(new Point2D(X, DEFAULT_PADDING + Y + NODE_SIZE)), any());
verify(canvasCommandFactory, times(1)).cloneConnector(eq(graphInstance.edge1), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), any());
// check command registry update after execution to allow a single undo/redo
verify(commandRegistry, times(2)).pop();
ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
verify(commandRegistry, times(1)).register(commandArgumentCaptor.capture());
assertTrue(commandArgumentCaptor.getValue() instanceof CompositeCommand);
assertEquals(2, ((CompositeCommand) commandArgumentCaptor.getValue()).size());
}
use of org.kie.workbench.common.stunner.core.command.Command in project kie-wb-common by kiegroup.
the class PasteSelectionSessionCommandTest method executeWithMultiSelectionWithEdgesClipboard.
@Test
@SuppressWarnings("unchecked")
public void executeWithMultiSelectionWithEdgesClipboard() {
final Connection sourceConnection = (Connection) ((ViewConnector) graphInstance.edge1.getContent()).getSourceConnection().orElse(null);
final Connection targetConnection = (Connection) ((ViewConnector) graphInstance.edge1.getContent()).getTargetConnection().orElse(null);
final EdgeClipboard clipboard = clipboardControl.buildNewEdgeClipboard(graphInstance.startNode.getUUID(), sourceConnection, graphInstance.intermNode.getUUID(), targetConnection);
clipboardControl.set(graphInstance.startNode, graphInstance.edge1, graphInstance.intermNode);
final Map<String, EdgeClipboard> map = new HashMap<>();
map.put(graphInstance.edge1.getUUID(), clipboard);
when(clipboardControl.getEdgeMap()).thenReturn(map);
assertEquals(1, clipboardControl.getEdgeMap().size());
pasteSelectionSessionCommand.bind(session);
pasteSelectionSessionCommand.setTestEdgeFoundInCanvas(false);
pasteSelectionSessionCommand.setTestEdgeFoundInClipboard(true);
assertEquals(false, pasteSelectionSessionCommand.isEdgeFoundInCanvas(graphInstance.edge1));
assertEquals(true, pasteSelectionSessionCommand.isEdgeFoundInClipboard(graphInstance.edge1));
// Mock the callback of CloneNodeCommand
ArgumentCaptor<Consumer> consumerNode = ArgumentCaptor.forClass(Consumer.class);
ArgumentCaptor<Consumer> consumerNode2 = ArgumentCaptor.forClass(Consumer.class);
when(cloneNodeCommand.getCandidate()).thenReturn(node);
when(cloneNodeCommand2.getCandidate()).thenReturn(node2);
when(canvasCommandFactory.cloneNode(eq(node), any(), any(), consumerNode.capture())).thenReturn(cloneNodeCommand);
when(canvasCommandFactory.cloneNode(eq(node2), any(), any(), consumerNode2.capture())).thenReturn(cloneNodeCommand2);
Map<Node, ArgumentCaptor<Consumer>> consumerMap = new HashMap() {
{
put(node, consumerNode);
put(node2, consumerNode2);
}
};
// Mock the callback of CloneConnectorCommand
ArgumentCaptor<Consumer> consumerEdge = ArgumentCaptor.forClass(Consumer.class);
when(canvasCommandFactory.cloneConnector(any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), consumerEdge.capture())).thenReturn(cloneConnectorCommand);
// apply callbacks mocks
when(sessionCommandManager.execute(eq(canvasHandler), any())).thenAnswer(param -> {
CompositeCommand argument = param.getArgument(1, CompositeCommand.class);
// callback to nodes
argument.getCommands().stream().filter(c -> c instanceof CloneNodeCommand).forEach(c -> {
CloneNodeCommand cloneNodeCommand = (CloneNodeCommand) c;
Node candidate = cloneNodeCommand.getCandidate();
consumerMap.get(candidate).getValue().accept(cloneMap.get(candidate));
});
// callback to connectors
argument.getCommands().stream().filter(c -> c instanceof CloneConnectorCommand).forEach(c -> consumerEdge.getValue().accept(cloneEdge));
return commandResult;
});
// Executing the command
clipboardControl.set(graphInstance.startNode, graphInstance.edge1, graphInstance.intermNode);
when(selectionControl.getSelectedItems()).thenReturn(Arrays.asList(graphInstance.startNode.getUUID(), graphInstance.edge1.getUUID(), graphInstance.intermNode.getUUID()));
CopySelectionSessionCommand.getInstance(sessionManager).bind(session);
pasteSelectionSessionCommand.execute(callback);
verify(canvasCommandFactory, times(1)).cloneNode(eq(graphInstance.startNode), eq(graphInstance.parentNode.getUUID()), eq(new Point2D(X, DEFAULT_PADDING + Y + NODE_SIZE)), any());
verify(canvasCommandFactory, times(1)).cloneConnector(eq(graphInstance.edge1), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), any());
// check command registry update after execution to allow a single undo/redo
verify(commandRegistry, times(2)).pop();
ArgumentCaptor<Command> commandArgumentCaptor = ArgumentCaptor.forClass(Command.class);
verify(commandRegistry, times(1)).register(commandArgumentCaptor.capture());
assertTrue(commandArgumentCaptor.getValue() instanceof CompositeCommand);
assertEquals(2, ((CompositeCommand) commandArgumentCaptor.getValue()).size());
}
use of org.kie.workbench.common.stunner.core.command.Command in project kie-wb-common by kiegroup.
the class NodeProxyTest method verifyCreateTargetNode.
@SuppressWarnings("all")
private void verifyCreateTargetNode(CanvasCommand<AbstractCanvasHandler> addConnectorCommand, CanvasCommand<AbstractCanvasHandler> addNodeCommand, CanvasCommand<AbstractCanvasHandler> setTargetNodeCommand) {
tested.init();
tested.start(1d, 2d);
verify(proxy, times(1)).start(eq(1d), eq(2d));
NodeShape targetNodeShape = view.getShapeBuilder().get();
assertEquals(targetShape, targetNodeShape);
ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
verify(proxy, times(1)).execute(commandCaptor.capture());
DeferredCompositeCommand command = (DeferredCompositeCommand) commandCaptor.getValue();
List commands = command.getCommands();
assertEquals(3, command.size());
DeferredCommand c0 = (DeferredCommand) commands.get(0);
assertEquals(addNodeCommand, c0.getCommand());
DeferredCommand c1 = (DeferredCommand) commands.get(1);
assertEquals(addConnectorCommand, c1.getCommand());
DeferredCommand c2 = (DeferredCommand) commands.get(2);
assertEquals(setTargetNodeCommand, c2.getCommand());
}
Aggregations