use of org.kie.workbench.common.stunner.core.command.CommandResult in project kie-wb-common by kiegroup.
the class AbstractCanvasHandlerViewerTest method init.
@SuppressWarnings("unchecked")
public void init() throws Exception {
this.canvasHandlerDiagram = null;
when(canvas.getView()).thenReturn(canvasView);
when(canvasView.asWidget()).thenReturn(canvasViewWidget);
when(canvas.getLienzoPanel()).thenReturn(lienzoPanel);
when(canvasHandler.getCanvas()).thenReturn(canvas);
when(canvasHandler.getAbstractCanvas()).thenReturn(canvas);
// The different viewer/editors tested reply on the canvas handler to obtain
// the diagram instance, so applying mocked answers for CanvasHandler #open,
// #clear and #destroy to handle the right diagram instance.
doAnswer(invocationOnMock -> {
final Diagram d = (Diagram) invocationOnMock.getArguments()[0];
final ParameterizedCommand<CommandResult<?>> c = (ParameterizedCommand) invocationOnMock.getArguments()[1];
AbstractCanvasHandlerViewerTest.this.canvasHandlerDiagram = d;
when(canvasHandler.getDiagram()).thenReturn(canvasHandlerDiagram);
c.execute(CanvasCommandResultBuilder.SUCCESS);
return null;
}).when(canvasHandler).draw(any(Diagram.class), any(ParameterizedCommand.class));
doAnswer(invocationOnMock -> {
AbstractCanvasHandlerViewerTest.this.canvasHandlerDiagram = null;
when(canvasHandler.getDiagram()).thenReturn(canvasHandlerDiagram);
return null;
}).when(canvasHandler).clear();
doAnswer(invocationOnMock -> {
AbstractCanvasHandlerViewerTest.this.canvasHandlerDiagram = null;
when(canvasHandler.getDiagram()).thenReturn(canvasHandlerDiagram);
return null;
}).when(canvasHandler).destroy();
when(diagram.getGraph()).thenReturn(graph);
when(graph.getContent()).thenReturn(graphContent);
when(graphContent.getBounds()).thenReturn(GRAPH_BOUNDS);
}
use of org.kie.workbench.common.stunner.core.command.CommandResult 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.command.CommandResult 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 RelationSection section = RelationUIModelMapperHelper.getSection(relation, index);
return section == RelationSection.INFORMATION_ITEM;
}
@Override
public CommandResult<RuleViolation> execute(final GraphCommandExecutionContext context) {
return moveInformationItems(index);
}
@Override
public CommandResult<RuleViolation> undo(final GraphCommandExecutionContext context) {
return moveInformationItems(oldIndex);
}
private CommandResult<RuleViolation> moveInformationItems(final int index) {
final RelationSection section = RelationUIModelMapperHelper.getSection(relation, index);
if (section == RelationSection.INFORMATION_ITEM) {
final int oldIndex = uiModel.getColumns().indexOf(columns.get(0));
final int relativeIndex = RelationUIModelMapperHelper.getInformationItemIndex(relation, index);
final int relativeOldIndex = RelationUIModelMapperHelper.getInformationItemIndex(relation, oldIndex);
final java.util.List<Integer> informationItemIndexesToMove = columns.stream().map(c -> uiModel.getColumns().indexOf(c)).map(i -> RelationUIModelMapperHelper.getInformationItemIndex(relation, i)).collect(Collectors.toList());
moveInformationItems(relativeIndex, relativeOldIndex, relation.getColumn(), informationItemIndexesToMove);
updateRowsData(relativeIndex, relativeOldIndex, relation.getRow(), informationItemIndexesToMove);
return GraphCommandResultBuilder.SUCCESS;
} else {
return GraphCommandResultBuilder.FAILED;
}
}
private <T> void moveInformationItems(final int relativeIndex, final int relativeOldIndex, final java.util.List<T> informationItems, final java.util.List<Integer> informationItemIndexesToMove) {
final java.util.List<T> informationItemsToMove = informationItemIndexesToMove.stream().map(informationItems::get).collect(Collectors.toList());
informationItems.removeAll(informationItemsToMove);
if (relativeIndex < relativeOldIndex) {
informationItems.addAll(relativeIndex, informationItemsToMove);
} else if (relativeIndex > relativeOldIndex) {
informationItems.addAll(relativeIndex - informationItemsToMove.size() + 1, informationItemsToMove);
}
}
private void updateRowsData(final int relativeIndex, final int relativeOldIndex, final java.util.List<List> rows, final java.util.List<Integer> informationItemIndexesToMove) {
rows.forEach(row -> moveInformationItems(relativeIndex, relativeOldIndex, row.getExpression(), informationItemIndexesToMove));
}
};
}
Aggregations