Search in sources :

Example 31 with Point2D

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

the class AbstractNodeBuilder method afterNodeBuild.

@SuppressWarnings("unchecked")
protected void afterNodeBuild(final BuilderContext context, final T node) {
    // Outgoing connections.
    if (outgoingResourceIds != null && !outgoingResourceIds.isEmpty()) {
        for (String outgoingNodeId : outgoingResourceIds) {
            GraphObjectBuilder<?, ?> outgoingBuilder = getBuilder(context, outgoingNodeId);
            if (outgoingBuilder == null) {
                throw new RuntimeException("No outgoing edge builder for " + outgoingNodeId);
            }
            final List<Command<GraphCommandExecutionContext, RuleViolation>> commands = new LinkedList<>();
            // If outgoing element it's a node means that it's docked.
            if (outgoingBuilder instanceof AbstractNodeBuilder) {
                // Command - Create the docked node.
                Node docked = (Node) outgoingBuilder.build(context);
                commands.add(context.getCommandFactory().addDockedNode(node, docked));
                // Obtain docked position and use those for the docked node.
                final List<Double[]> dockers = ((AbstractNodeBuilder) outgoingBuilder).dockers;
                if (!dockers.isEmpty()) {
                    // TODO: Use not only first docker coordinates?
                    Double[] dCoords = dockers.get(0);
                    double x = dCoords[0];
                    double y = dCoords[1];
                    commands.add(context.getCommandFactory().updatePosition(docked, new Point2D(x, y)));
                }
            } else {
                // Create the outgoing edge.
                AbstractEdgeBuilder edgeBuilder = (AbstractEdgeBuilder) outgoingBuilder;
                Edge edge = (Edge) edgeBuilder.build(context);
                if (edge != null) {
                    // Command - Execute the graph command to set the node as the edge connection's source..
                    Double[] sourceDocker = null;
                    final List<Double[]> dockers = ((AbstractEdgeBuilder) outgoingBuilder).dockers;
                    if (dockers != null && dockers.size() > 1) {
                        sourceDocker = dockers.get(0);
                    }
                    Connection sourceConnection = null;
                    if (null != sourceDocker) {
                        sourceConnection = MagnetConnection.Builder.at(sourceDocker[0], sourceDocker[1]).setAuto(edgeBuilder.isSourceAutoConnection());
                    }
                    commands.add(context.getCommandFactory().setSourceNode(node, edge, sourceConnection));
                }
            }
            if (!commands.isEmpty()) {
                for (Command<GraphCommandExecutionContext, RuleViolation> command : commands) {
                    doExecuteCommand(context, command);
                }
            }
        }
    }
    // Children connections.
    if (childNodeIds != null && !childNodeIds.isEmpty()) {
        for (String childNodeId : childNodeIds) {
            GraphObjectBuilder<?, ?> childNodeBuilder = getBuilder(context, childNodeId);
            if (childNodeBuilder == null) {
                throw new RuntimeException("No child node builder for " + childNodeId);
            }
            Command<GraphCommandExecutionContext, RuleViolation> command = null;
            if (childNodeBuilder instanceof NodeObjectBuilder) {
                // Command - Create the child node and the parent-child relationship.
                Node childNode = (Node) childNodeBuilder.build(context);
                command = context.getCommandFactory().addChildNode(node, childNode);
            }
            if (null != command) {
                doExecuteCommand(context, command);
            }
        }
    }
}
Also used : Node(org.kie.workbench.common.stunner.core.graph.Node) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) LinkedList(java.util.LinkedList) Command(org.kie.workbench.common.stunner.core.command.Command) AddNodeCommand(org.kie.workbench.common.stunner.core.graph.command.impl.AddNodeCommand) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 32 with Point2D

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

the class GraphBuilder method translate.

/**
 * Move node into a new coordinate system with origin in newOrigin.
 * <p>
 * E.g., assume origin is currently (0,0), and consider node at (10,11).
 * If we move node into a new coordinate system where the origin is in (3, 4)
 * then the new coordinates for node are: (10-3, 11-4) = (7,7)
 */
private void translate(Node<? extends View, ?> node, Bounds.Bound newOrigin) {
    logger.debug("Translating {} into constraints {}", node.getContent().getBounds(), newOrigin);
    Bounds childBounds = node.getContent().getBounds();
    double constrainedX = childBounds.getUpperLeft().getX() - newOrigin.getX();
    double constrainedY = childBounds.getUpperLeft().getY() - newOrigin.getY();
    Point2D coords = Point2D.create(constrainedX, constrainedY);
    updatePosition(node, coords);
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds)

Example 33 with Point2D

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

the class BPMNGraphFactoryImplTest method testBuild.

@Test
@SuppressWarnings("unchecked")
public void testBuild() {
    final Node diagramNode = mock(Node.class);
    final Node startEventNode = mock(Node.class);
    when(factoryManager.newElement(anyString(), eq(BPMNDiagramImpl.class))).thenReturn(diagramNode);
    when(factoryManager.newElement(anyString(), eq(StartNoneEvent.class))).thenReturn(startEventNode);
    final Graph<DefinitionSet, Node> graph = tested.build("uuid1", "defSetId");
    assertNotNull(graph);
    assertEquals("uuid1", graph.getUUID());
    assertEquals(1, graph.getLabels().size());
    assertTrue(graph.getLabels().contains("defSetId"));
    final ArgumentCaptor<Command> commandCaptor = ArgumentCaptor.forClass(Command.class);
    verify(graphCommandFactory, times(1)).addNode(eq(diagramNode));
    verify(graphCommandFactory, times(1)).addChildNode(eq(diagramNode), eq(startEventNode), eq(new Point2D(BPMNGraphFactoryImpl.START_X, BPMNGraphFactoryImpl.START_Y)));
    verify(graphCommandManager, times(1)).execute(any(GraphCommandExecutionContext.class), commandCaptor.capture());
    final Command command = commandCaptor.getValue();
    assertTrue(command instanceof CompositeCommand);
    final CompositeCommand compositeCommand = (CompositeCommand) command;
    assertEquals(2, compositeCommand.size());
}
Also used : Command(org.kie.workbench.common.stunner.core.command.Command) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Node(org.kie.workbench.common.stunner.core.graph.Node) GraphCommandExecutionContext(org.kie.workbench.common.stunner.core.graph.command.GraphCommandExecutionContext) BPMNDiagramImpl(org.kie.workbench.common.stunner.bpmn.definition.BPMNDiagramImpl) DefinitionSet(org.kie.workbench.common.stunner.core.graph.content.definition.DefinitionSet) StartNoneEvent(org.kie.workbench.common.stunner.bpmn.definition.StartNoneEvent) CompositeCommand(org.kie.workbench.common.stunner.core.command.impl.CompositeCommand) Test(org.junit.Test)

Example 34 with Point2D

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

the class SequenceFlowPropertyWriter method setWaypoints.

private void setWaypoints(ViewConnector<? extends BPMNViewDefinition> connector) {
    BPMNEdge bpmnEdge = di.createBPMNEdge();
    bpmnEdge.setBpmnElement(sequenceFlow);
    Point2D sourcePt = connector.getSourceConnection().get().getLocation();
    Point2D targetPt = connector.getTargetConnection().get().getLocation();
    org.eclipse.dd.dc.Point sourcePoint = dc.createPoint();
    sourcePoint.setX(source.getShape().getBounds().getX() + (float) sourcePt.getX());
    sourcePoint.setY(source.getShape().getBounds().getY() + (float) sourcePt.getY());
    org.eclipse.dd.dc.Point targetPoint = dc.createPoint();
    targetPoint.setX(target.getShape().getBounds().getX() + (float) targetPt.getX());
    targetPoint.setY(target.getShape().getBounds().getY() + (float) targetPt.getY());
    bpmnEdge.getWaypoint().add(sourcePoint);
    bpmnEdge.getWaypoint().add(targetPoint);
    this.bpmnEdge = bpmnEdge;
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) BPMNEdge(org.eclipse.bpmn2.di.BPMNEdge) Factories.dc(org.kie.workbench.common.stunner.bpmn.backend.converters.fromstunner.Factories.dc)

Example 35 with Point2D

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

the class CanvasLayoutUtilsTest method getNextFromRoot.

// TODO (AlessioP & Roger):
@Test
@Ignore
@SuppressWarnings("unchecked")
public void getNextFromRoot() {
    Node node1 = mock(Node.class);
    Bounds boundsNode1 = new BoundsImpl(new BoundImpl(100d, 100d), new BoundImpl(300d, 200d));
    View viewNode1 = mock(View.class);
    when(node1.getContent()).thenReturn(viewNode1);
    when(viewNode1.getBounds()).thenReturn(boundsNode1);
    Node node2 = mock(Node.class);
    Bounds boundsNode2 = new BoundsImpl(new BoundImpl(100d, 100d), new BoundImpl(300d, 200d));
    View viewNode2 = mock(View.class);
    when(node2.getContent()).thenReturn(viewNode2);
    when(viewNode2.getBounds()).thenReturn(boundsNode2);
    Node nodeRoot = mock(Node.class);
    double rootWidth = 40d;
    double rootHeight = 40d;
    Bounds rootBounds = new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(rootWidth, rootHeight));
    View rootView = mock(View.class);
    when(nodeRoot.getContent()).thenReturn(rootView);
    when(rootView.getBounds()).thenReturn(rootBounds);
    List<Node> nodes = new ArrayList<>();
    List<Edge> edges = new ArrayList<>();
    Edge edge = mock(Edge.class);
    edges.add(edge);
    when(nodeRoot.getOutEdges()).thenReturn(edges);
    when(edge.getTargetNode()).thenReturn(node1);
    when(nodeRoot.getContent()).thenReturn(rootView);
    nodes.add(nodeRoot);
    when(graph.nodes()).thenReturn(nodes);
    when(node2.getInEdges()).thenReturn(edges);
    when(nodeRoot.asNode()).thenReturn(nodeRoot);
    Point2D next = canvasLayoutUtils.getNext(canvasHandler, nodeRoot, node2);
    assertTrue(next.getX() > rootWidth);
    assertTrue(next.getY() > NEW_NODE_HEIGHT);
}
Also used : Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) Node(org.kie.workbench.common.stunner.core.graph.Node) Bounds(org.kie.workbench.common.stunner.core.graph.content.Bounds) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) ArrayList(java.util.ArrayList) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) View(org.kie.workbench.common.stunner.core.graph.content.view.View) Edge(org.kie.workbench.common.stunner.core.graph.Edge) Ignore(org.junit.Ignore) Test(org.junit.Test)

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