use of org.kie.workbench.common.stunner.core.graph.Element in project kie-wb-common by kiegroup.
the class MorphActionsToolboxFactory method getActions.
@Override
@SuppressWarnings("unchecked")
public Collection<ToolboxAction<AbstractCanvasHandler>> getActions(final AbstractCanvasHandler canvasHandler, final Element<?> e) {
final Set<ToolboxAction<AbstractCanvasHandler>> actions = new LinkedHashSet<>();
if (null != e.asNode()) {
final Element<? extends Definition<?>> element = (Element<? extends Definition<?>>) e;
final Object definition = element.getContent().getDefinition();
if (definitionUtils.hasMorphTargets(definition)) {
final String id = getDefinitionManager().adapters().forDefinition().getId(definition);
final MorphAdapter<Object> morphAdapter = getDefinitionManager().adapters().registry().getMorphAdapter(definition.getClass());
final Iterable<MorphDefinition> morphDefinitions = morphAdapter.getMorphDefinitions(definition);
if (null != morphDefinitions && morphDefinitions.iterator().hasNext()) {
final Map<String, MorphDefinition> definitionMap = new LinkedHashMap<>();
for (final MorphDefinition morphDefinition : morphDefinitions) {
final Iterable<String> morphTargets = morphAdapter.getTargets(definition, morphDefinition);
if (null != morphTargets && morphTargets.iterator().hasNext()) {
for (final String morphTarget : morphTargets) {
if (!id.equals(morphTarget)) {
definitionMap.put(morphTarget, morphDefinition);
}
}
}
}
// Create a morph toolbox action for each target morph candidate.
definitionMap.forEach((targetMorphId, morphDefinition) -> actions.add(morphNodeActions.get().setMorphDefinition(morphDefinition).setTargetDefinitionId(targetMorphId)));
}
}
}
return actions;
}
use of org.kie.workbench.common.stunner.core.graph.Element 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()));
}
}
use of org.kie.workbench.common.stunner.core.graph.Element 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();
}
use of org.kie.workbench.common.stunner.core.graph.Element 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();
}
}
use of org.kie.workbench.common.stunner.core.graph.Element in project kie-wb-common by kiegroup.
the class BaseCanvasHandlerTest method getElementAtWhenCanvasDoesNotHaveShapeAtPosition.
@Test
@SuppressWarnings("unchecked")
public void getElementAtWhenCanvasDoesNotHaveShapeAtPosition() {
final Optional<Shape> oShape = Optional.empty();
final Index index = mock(Index.class);
when(canvas.getShapeAt(anyDouble(), anyDouble())).thenReturn(oShape);
when(commandExecutionContext.getGraphIndex()).thenReturn(index);
final Optional<Element> oElement = tested.getElementAt(0.0, 0.0);
assertFalse(oElement.isPresent());
}
Aggregations