Search in sources :

Example 41 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class AbstractElementBuilderControl method getChildCoordinates.

public Point2D getChildCoordinates(final Node<View<?>, Edge> parent, final double _x, final double _y) {
    if (null != parent) {
        final Point2D parentCoords = GraphUtils.getPosition(parent.getContent());
        final double x = _x - parentCoords.getX();
        final double y = _y - parentCoords.getY();
        return new Point2D(x, y);
    }
    return new Point2D(_x, _y);
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D)

Example 42 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class AbstractElementBuilderControl method getElementCommands.

@SuppressWarnings("unchecked")
public void getElementCommands(final Element element, final Node<View<?>, Edge> parent, final double x, final double y, final CommandsCallback commandsCallback) {
    Command<AbstractCanvasHandler, CanvasViolation> command = null;
    if (element instanceof Node) {
        if (null != parent) {
            command = canvasCommandFactory.addChildNode(parent, (Node) element, getShapeSetId());
        } else {
            command = canvasCommandFactory.addNode((Node) element, getShapeSetId());
        }
    } else if (element instanceof Edge && null != parent) {
        command = canvasCommandFactory.addConnector(parent, (Edge) element, MagnetConnection.Builder.forElement(parent), getShapeSetId());
    } else {
        throw new RuntimeException("Unrecognized element type for " + element);
    }
    // Execute both add element and move commands in batch, so undo will be done in batch as well.
    Command<AbstractCanvasHandler, CanvasViolation> moveCanvasElementCommand = canvasCommandFactory.updatePosition((Node<View<?>, Edge>) element, new Point2D(x, y));
    final List<Command<AbstractCanvasHandler, CanvasViolation>> commandList = new LinkedList<Command<AbstractCanvasHandler, CanvasViolation>>();
    commandList.add(command);
    commandList.add(moveCanvasElementCommand);
    commandsCallback.onComplete(element.getUUID(), commandList);
}
Also used : CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) AbstractCanvasHandler(org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Node(org.kie.workbench.common.stunner.core.graph.Node) Edge(org.kie.workbench.common.stunner.core.graph.Edge) View(org.kie.workbench.common.stunner.core.graph.content.view.View) LinkedList(java.util.LinkedList)

Example 43 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class NodeBuilderControlImpl method build.

@Override
@SuppressWarnings("unchecked")
public void build(final NodeBuildRequest request, final BuildCallback buildCallback) {
    final double x = request.getX();
    final double y = request.getY();
    final Node<? extends View<?>, Edge> node = request.getNode();
    final Edge<? extends ViewConnector<?>, Node> inEdge = request.getInEdge();
    final Connection sourceConnection = request.getSourceConnection();
    final Connection targetConnection = request.getTargetConnection();
    if (null != node) {
        final Object nodeDef = node.getContent().getDefinition();
        final String nodeId = clientDefinitionManager.adapters().forDefinition().getId(nodeDef);
        final ElementBuilderControlImpl ebc = getElementBuilderControl();
        final Node<View<?>, Edge> parent = ebc.getParent(x, y);
        final Point2D childCoordinates = ebc.getChildCoordinates(parent, x, y);
        final String ssid = canvasHandler.getDiagram().getMetadata().getShapeSetId();
        ebc.getElementCommands(node, parent, childCoordinates.getX(), childCoordinates.getY(), new AbstractElementBuilderControl.CommandsCallback() {

            @Override
            public void onComplete(final String uuid, final List<Command<AbstractCanvasHandler, CanvasViolation>> commands) {
                final CompositeCommand.Builder commandBuilder = new CompositeCommand.Builder().addCommands(commands);
                if (inEdge != null) {
                    final Object edgeDef = inEdge.getContent().getDefinition();
                    final String edgeId = clientDefinitionManager.adapters().forDefinition().getId(edgeDef);
                    // The commands to batch for the edge that connects both nodes.
                    commandBuilder.addCommand(commandFactory.addConnector(inEdge.getSourceNode(), inEdge, sourceConnection, ssid));
                    commandBuilder.addCommand(commandFactory.setTargetNode(node, inEdge, targetConnection));
                }
                final CommandResult<CanvasViolation> results = elementBuilderControl.getCommandManager().execute(canvasHandler, commandBuilder.build());
                if (!CommandUtils.isError(results)) {
                    updateConnectorShape(inEdge, node, sourceConnection, targetConnection);
                }
                buildCallback.onSuccess(uuid);
            }

            @Override
            public void onError(final ClientRuntimeError error) {
                buildCallback.onError(error);
            }
        });
    }
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) View(org.kie.workbench.common.stunner.core.graph.content.view.View) CommandResult(org.kie.workbench.common.stunner.core.command.CommandResult) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) ClientRuntimeError(org.kie.workbench.common.stunner.core.client.service.ClientRuntimeError) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 44 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class CanvasLayoutUtils method getNext.

public Point2D getNext(final CanvasHandler canvasHandler, final Node<View<?>, Edge> root, final double rootNodeWidth, final double rootNodeHeight, final double newNodeWidth, final double newNodeHeight, Point2D offset, Point2D rootNodeCoordinates) {
    checkNotNull("canvasHandler", canvasHandler);
    checkNotNull("root", root);
    final int canvasHeight = canvasHandler.getCanvas().getHeight();
    final int canvasWidth = canvasHandler.getCanvas().getWidth();
    Point2D newPositionUL = getNextPositionWithOffset(rootNodeCoordinates, offset);
    graphBoundsIndexer.build(canvasHandler.getDiagram().getGraph());
    Element parentNode = GraphUtils.getParent(root.asNode());
    boolean checkParent = false;
    if (parentNode != null) {
        if (!(isCanvasRoot(canvasHandler.getDiagram(), parentNode.getUUID()))) {
            checkParent = true;
        }
    }
    Node targetNodeContainer = graphBoundsIndexer.getAt(newPositionUL.getX(), newPositionUL.getY(), newNodeWidth, newNodeHeight, parentNode);
    boolean canContain = false;
    if (targetNodeContainer != null) {
        canContain = canContain(canvasHandler, targetNodeContainer, root);
    }
    if ((!canContain) || isOutOfCanvas(newPositionUL, newNodeHeight, canvasHeight) || checkParent) {
        if (checkParent) {
            newPositionUL = getNextPositionFromParent(rootNodeCoordinates, offset, parentNode, rootNodeHeight, rootNodeWidth, newNodeWidth);
        }
        while (((!isCanvasPositionAvailable(graphBoundsIndexer, newPositionUL, newNodeWidth, newNodeHeight, parentNode)) && !canContain) && (newPositionUL.getY() < canvasHeight) && (newPositionUL.getX() < canvasWidth) || isOutOfCanvas(newPositionUL, newNodeHeight, canvasHeight)) {
            parentNode = GraphUtils.getParent(root.asNode());
            checkParent = false;
            if (parentNode != null) {
                if (!(isCanvasRoot(canvasHandler.getDiagram(), parentNode.getUUID()))) {
                    checkParent = true;
                }
            }
            double[] nodeAtPositionSize;
            nodeAtPositionSize = getNodeSizeAt(graphBoundsIndexer, newPositionUL, newNodeWidth, newNodeHeight, parentNode);
            if (checkParent) {
                final Node targetNodeNewPos = graphBoundsIndexer.getAt(newPositionUL.getX(), newPositionUL.getY(), newNodeWidth, newNodeHeight, parentNode);
                if (parentNode != targetNodeNewPos) {
                    offset.setY(offset.getY() + PADDING_Y);
                }
            } else {
                if (nodeAtPositionSize == null) {
                    nodeAtPositionSize = new double[] { 0.0d };
                    offset.setY(offset.getY() + PADDING_Y);
                } else {
                    offset.setY(offset.getY() + nodeAtPositionSize[1] + PADDING_Y);
                }
            }
            newPositionUL = getNextPositionWithOffset(rootNodeCoordinates, offset);
            if (isOutOfCanvas(newPositionUL, newNodeHeight, canvasHeight)) {
                rootNodeCoordinates.setY(0);
                offset.setY(PADDING_Y);
                offset.setX(offset.getX() + nodeAtPositionSize[0] + PADDING_X);
                newPositionUL = getNextPositionWithOffset(rootNodeCoordinates, offset);
            }
            if (checkParent) {
                newPositionUL = getNextPositionFromParent(rootNodeCoordinates, offset, parentNode, rootNodeHeight, rootNodeWidth, newNodeWidth);
            }
            targetNodeContainer = graphBoundsIndexer.getAt(newPositionUL.getX(), newPositionUL.getY(), newNodeWidth, newNodeHeight, parentNode);
            canContain = targetNodeContainer == null || canContain(canvasHandler, targetNodeContainer, root);
        }
    } else {
        if (checkParent) {
            newPositionUL = getNextPositionFromParent(rootNodeCoordinates, offset, parentNode, rootNodeHeight, rootNodeWidth, newNodeWidth);
        } else {
            newPositionUL = getNextPositionWithOffset(rootNodeCoordinates, offset);
        }
    }
    return newPositionUL;
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Element(org.kie.workbench.common.stunner.core.graph.Element) Node(org.kie.workbench.common.stunner.core.graph.Node)

Example 45 with Point2D

use of org.kie.workbench.common.stunner.core.graph.content.view.Point2D in project kie-wb-common by kiegroup.

the class AbstractCanvasTooltip method show.

@Override
public void show(final T content, final Point2D location) {
    final Point2D computedLocation = getComputedLocation(location);
    showAt(content, computedLocation);
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D)

Aggregations

Point2D (org.kie.workbench.common.stunner.core.graph.content.view.Point2D)55 Test (org.junit.Test)25 Edge (org.kie.workbench.common.stunner.core.graph.Edge)18 View (org.kie.workbench.common.stunner.core.graph.content.view.View)17 Node (org.kie.workbench.common.stunner.core.graph.Node)15 Command (org.kie.workbench.common.stunner.core.command.Command)10 BoundImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl)10 BoundsImpl (org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl)10 Before (org.junit.Before)9 Bounds (org.kie.workbench.common.stunner.core.graph.content.Bounds)7 AbstractCanvasHandler (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvasHandler)6 CompositeCommand (org.kie.workbench.common.stunner.core.command.impl.CompositeCommand)6 TestingGraphMockHandler (org.kie.workbench.common.stunner.core.TestingGraphMockHandler)5 UpdateElementPositionCommand (org.kie.workbench.common.stunner.core.client.canvas.command.UpdateElementPositionCommand)5 Ignore (org.junit.Ignore)4 Element (org.kie.workbench.common.stunner.core.graph.Element)4 AbstractCanvas (org.kie.workbench.common.stunner.core.client.canvas.AbstractCanvas)3 CanvasCommand (org.kie.workbench.common.stunner.core.client.command.CanvasCommand)3 ShapeView (org.kie.workbench.common.stunner.core.client.shape.view.ShapeView)3 CommandResult (org.kie.workbench.common.stunner.core.command.CommandResult)3