Search in sources :

Example 36 with AbstractCanvasHandler

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;
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Node(org.kie.workbench.common.stunner.core.graph.Node) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)

Example 37 with AbstractCanvasHandler

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;
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) Element(org.kie.workbench.common.stunner.core.graph.Element) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) ShapeLocationsChangedEvent(org.kie.workbench.common.stunner.core.client.canvas.event.ShapeLocationsChangedEvent) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)

Example 38 with AbstractCanvasHandler

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);
}
Also used : AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)

Example 39 with AbstractCanvasHandler

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));
        }
    };
}
Also used : DMNGridData(org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridData) DecisionTable(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionTable) DecisionTableUIModelMapperHelper(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) DecisionRule(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule) LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) VetoExecutionCommand(org.kie.workbench.common.dmn.client.commands.VetoExecutionCommand) CanvasCommandResultBuilder(org.kie.workbench.common.stunner.core.client.command.CanvasCommandResultBuilder) GridColumn(org.uberfire.ext.wires.core.grids.client.model.GridColumn) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) AbstractCanvasCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AbstractCanvasCommand) Collectors(java.util.stream.Collectors) AbstractCanvasGraphCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AbstractCanvasGraphCommand) ArrayList(java.util.ArrayList) List(java.util.List) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) UnaryTests(org.kie.workbench.common.dmn.api.definition.v1_1.UnaryTests) Command(org.kie.workbench.common.stunner.core.command.Command) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) GraphCommandResultBuilder(org.kie.workbench.common.stunner.core.graph.command.GraphCommandResultBuilder) VetoUndoCommand(org.kie.workbench.common.dmn.client.commands.VetoUndoCommand) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) DecisionTableSection(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper.DecisionTableSection) DecisionTableSection(org.kie.workbench.common.dmn.client.editors.expressions.types.dtable.DecisionTableUIModelMapperHelper.DecisionTableSection) LiteralExpression(org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) DecisionRule(org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule) AbstractGraphCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AbstractGraphCommand) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) ArrayList(java.util.ArrayList) List(java.util.List)

Example 40 with AbstractCanvasHandler

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;
        }
    };
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) GridData(org.uberfire.ext.wires.core.grids.client.model.GridData) AbstractCanvasCommand(org.kie.workbench.common.stunner.core.client.canvas.command.AbstractCanvasCommand)

Aggregations

AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)60 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)37 Test (org.junit.Test)30 DMNGridRow (org.kie.workbench.common.dmn.client.widgets.grid.model.DMNGridRow)15 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)10 Node (org.kie.workbench.common.stunner.core.graph.Node)10 Command (org.kie.workbench.common.stunner.core.command.Command)9 GraphCommandExecutionContext (org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext)9 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)9 List (org.kie.workbench.common.dmn.api.definition.v1_1.List)8 Edge (org.kie.workbench.common.stunner.core.graph.Edge)8 Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)7 DecisionRule (org.kie.workbench.common.dmn.api.definition.v1_1.DecisionRule)6 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)6 View (org.kie.workbench.common.stunner.core.graph.content.view.View)6 BaseGridRow (org.uberfire.ext.wires.core.grids.client.model.impl.BaseGridRow)6 Collectors (java.util.stream.Collectors)5 LiteralExpression (org.kie.workbench.common.dmn.api.definition.v1_1.LiteralExpression)5 CanvasSelectionEvent (org.kie.workbench.common.stunner.core.client.canvas.event.selection.CanvasSelectionEvent)5 CanvasCommandFactory (org.kie.workbench.common.stunner.core.client.command.CanvasCommandFactory)5