use of org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler in project kie-wb-common by kiegroup.
the class ContainmentAcceptorControlImpl method evaluate.
private boolean evaluate(final Element parent, final Node[] children, final Function<Command<AbstractCanvasHandler, CanvasViolation>, CommandResult<CanvasViolation>> executor, final boolean highlights) {
// Cannot evaluate with no candidates.
if (children == null || children.length == 0) {
return false;
}
// Do not accept multiple containment if children do not share same parent instance.
if (parent == null && children.length >= 2) {
return false;
}
// Generate the commands and perform the execution.
final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> builder = new CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation>().forward();
for (final Node node : children) {
builder.addCommand(canvasCommandFactory.updateChildNode((Node) parent, node));
}
if (builder.size() > 0) {
final Command<AbstractCanvasHandler, CanvasViolation> command = builder.size() == 1 ? builder.get(0) : builder.build();
final CommandResult<CanvasViolation> result = executor.apply(command);
final boolean success = isCommandSuccess(result);
if (highlights && !success) {
canvasHighlight.invalid(result.getViolations());
} else {
canvasHighlight.unhighLight();
}
return success;
}
return true;
}
use of org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler in project kie-wb-common by kiegroup.
the class LocationControlImpl method move.
@Override
@SuppressWarnings("unchecked")
public CommandResult<CanvasViolation> move(final Element[] elements, final Point2D[] locations) {
if (elements.length != locations.length) {
throw new IllegalArgumentException("The length for the elements to move " + "does not match the locations provided.");
}
Command<AbstractCanvasHandler, CanvasViolation> command;
if (elements.length == 1) {
command = createMoveCommand(elements[0], locations[0]);
} else {
final CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation> builder = new CompositeCommand.Builder<AbstractCanvasHandler, CanvasViolation>().forward();
int i = 0;
for (final Element element : elements) {
final CanvasCommand<AbstractCanvasHandler> c = createMoveCommand(element, locations[i]);
builder.addCommand(c);
i++;
}
command = builder.build();
}
CommandResult<CanvasViolation> result = getCommandManager().allow(canvasHandler, command);
if (!CommandUtils.isError(result)) {
result = getCommandManager().execute(canvasHandler, command);
if (!CommandUtils.isError(result)) {
List<String> uuids = Arrays.stream(elements).map(Element::getUUID).collect(Collectors.toList());
shapeLocationsChangedEvent.fire(new ShapeLocationsChangedEvent(uuids, canvasHandler));
}
}
return result;
}
use of org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler in project kie-wb-common by kiegroup.
the class SessionPreviewImplTest method checkCanvasHandler.
private void checkCanvasHandler(final boolean isQualifierUnsatisfied, final Consumer<AbstractCanvasHandler> assertion) {
when(customCanvasHandlerInstance.isUnsatisfied()).thenReturn(isQualifierUnsatisfied);
preview.open(session, callback);
final AbstractCanvasHandler handler = preview.getCanvasHandler();
assertion.accept(handler);
}
use of org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler in project kie-wb-common by kiegroup.
the class MoveColumnsCommand method newGraphCommand.
@Override
protected Command<GraphCommandExecutionContext, RuleViolation> newGraphCommand(final AbstractCanvasHandler context) {
return new AbstractGraphCommand() {
@Override
protected CommandResult<RuleViolation> check(final GraphCommandExecutionContext context) {
return isColumnInValidSection() ? GraphCommandResultBuilder.SUCCESS : GraphCommandResultBuilder.FAILED;
}
private boolean isColumnInValidSection() {
final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
return section == DecisionTableSection.INPUT_CLAUSES || section == DecisionTableSection.OUTPUT_CLAUSES;
}
@Override
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
return moveClauses(index);
}
@Override
public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
return moveClauses(oldIndex);
}
private CommandResult<RuleViolation> moveClauses(final int index) {
final DecisionTableSection section = DecisionTableUIModelMapperHelper.getSection(dtable, index);
if (section == DecisionTableSection.INPUT_CLAUSES) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, index);
final int relativeOldIndex = DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, oldIndex);
final List<Integer> inputClauseIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).map(i -> DecisionTableUIModelMapperHelper.getInputEntryIndex(dtable, i)).collect(Collectors.toList());
moveClauses(relativeIndex, relativeOldIndex, dtable.getInput(), inputClauseIndexesToMove);
final List<List<UnaryTests>> decisionRulesInputEntries = dtable.getRule().stream().map(DecisionRule::getInputEntry).collect(Collectors.toList());
updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesInputEntries, inputClauseIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else if (section == DecisionTableSection.OUTPUT_CLAUSES) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, index);
final int relativeOldIndex = DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, oldIndex);
final List<Integer> outputClauseIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).map(i -> DecisionTableUIModelMapperHelper.getOutputEntryIndex(dtable, i)).collect(Collectors.toList());
moveClauses(relativeIndex, relativeOldIndex, dtable.getOutput(), outputClauseIndexesToMove);
final List<List<LiteralExpression>> decisionRulesOutputEntries = dtable.getRule().stream().map(DecisionRule::getOutputEntry).collect(Collectors.toList());
updateDecisionRules(relativeIndex, relativeOldIndex, decisionRulesOutputEntries, outputClauseIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else {
return GraphCommandResultBuilder.FAILED;
}
}
private <T> void moveClauses(final int relativeIndex, final int relativeOldIndex, final List<T> clauses, final List<Integer> clauseIndexesToMove) {
final List<T> clausesToMove = clauseIndexesToMove.stream().map(clauses::get).collect(Collectors.toList());
clauses.removeAll(clausesToMove);
if (relativeIndex < relativeOldIndex) {
clauses.addAll(relativeIndex, clausesToMove);
} else if (relativeIndex > relativeOldIndex) {
clauses.addAll(relativeIndex - clausesToMove.size() + 1, clausesToMove);
}
}
private <T> void updateDecisionRules(final int relativeIndex, final int relativeOldIndex, final List<List<T>> clauses, final List<Integer> clauseIndexesToMove) {
clauses.forEach(row -> moveClauses(relativeIndex, relativeOldIndex, row, clauseIndexesToMove));
}
};
}
use of org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler in project kie-wb-common by kiegroup.
the class SetKindCommand method newCanvasCommand.
@Override
protected Command<AbstractCanvasHandler, CanvasViolation> newCanvasCommand(final AbstractCanvasHandler handler) {
return new AbstractCanvasCommand() {
@Override
public CommandResult<CanvasViolation> execute(final AbstractCanvasHandler handler) {
final GridData gridData = cellTuple.getGridWidget().getModel();
gridData.setCellValue(cellTuple.getRowIndex(), cellTuple.getColumnIndex(), cellTuple.getValue());
canvasOperation.execute();
return CanvasCommandResultBuilder.SUCCESS;
}
@Override
public CommandResult<CanvasViolation> undo(final AbstractCanvasHandler handler) {
if (oldCellValue.isPresent()) {
cellTuple.getGridWidget().getModel().setCellValue(cellTuple.getRowIndex(), cellTuple.getColumnIndex(), oldCellValue.get());
} else {
cellTuple.getGridWidget().getModel().deleteCell(cellTuple.getRowIndex(), cellTuple.getColumnIndex());
}
canvasOperation.execute();
return CanvasCommandResultBuilder.SUCCESS;
}
};
}
Aggregations