Search in sources :

Example 1 with MagnetConnection

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

the class ShapeUtils method updateEdgeConnection.

@SuppressWarnings("unchecked")
public static void updateEdgeConnection(final AbstractCanvasHandler context, final Connection connection, final Node<? extends View<?>, Edge> node) {
    if (null != node && null == connection.getLocation() && connection instanceof MagnetConnection) {
        final MagnetConnection magnetConnection = (MagnetConnection) connection;
        final OptionalInt magnetIndex = magnetConnection.getMagnetIndex();
        if (magnetIndex.orElse(-1) == MagnetConnection.MAGNET_CENTER) {
            final Shape<ShapeView<?>> nodeShape = context.getCanvas().getShape(node.getUUID());
            final BoundingBox boundingBox = nodeShape.getShapeView().getBoundingBox();
            magnetConnection.setLocation(new Point2D(boundingBox.getWidth() / 2, boundingBox.getHeight() / 2));
        }
    }
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) Point2D(org.kie.workbench.common.stunner.core.graph.content.view.Point2D) BoundingBox(org.kie.workbench.common.stunner.core.client.shape.view.BoundingBox) OptionalInt(java.util.OptionalInt) ShapeView(org.kie.workbench.common.stunner.core.client.shape.view.ShapeView)

Example 2 with MagnetConnection

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

the class SetCanvasConnectionCommandTest method testExecuteAndUpdateMagnetLocations.

@Test
@SuppressWarnings("unchecked")
public void testExecuteAndUpdateMagnetLocations() {
    final MagnetConnection sourceConnection = new MagnetConnection.Builder().magnet(0).build();
    final MagnetConnection targetConnection = new MagnetConnection.Builder().magnet(0).build();
    when(edgeContent.getSourceConnection()).thenReturn(Optional.of(sourceConnection));
    when(edgeContent.getTargetConnection()).thenReturn(Optional.of(targetConnection));
    final CommandResult<CanvasViolation> result = tested.execute(canvasHandler);
    assertNotEquals(CommandResult.Type.ERROR, result.getType());
    verify(edgeShape, times(1)).applyConnections(eq(edge), eq(sourceShapeView), eq(targetShapeView), any(MutationContext.class));
    verify(canvasHandler, times(1)).notifyCanvasElementUpdated(eq(source));
    verify(canvasHandler, times(1)).notifyCanvasElementUpdated(eq(target));
    assertEquals(5d, sourceConnection.getLocation().getX(), 0d);
    assertEquals(5d, sourceConnection.getLocation().getY(), 0d);
    assertEquals(10d, targetConnection.getLocation().getX(), 0d);
    assertEquals(10d, targetConnection.getLocation().getY(), 0d);
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) CanvasViolation(org.kie.workbench.common.stunner.core.client.command.CanvasViolation) MutationContext(org.kie.workbench.common.stunner.core.client.shape.MutationContext) Test(org.junit.Test)

Example 3 with MagnetConnection

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

the class SetConnectionTargetNodeCommandTest method testExecuteOnlyConnectionsHasBeenChanged.

@Test
@SuppressWarnings("unchecked")
public void testExecuteOnlyConnectionsHasBeenChanged() {
    when(edge.getTargetNode()).thenReturn(node);
    MagnetConnection connection = MagnetConnection.Builder.at(MAGNETX, MAGNETY);
    tested = new SetConnectionTargetNodeCommand(node, edge, connection);
    CommandResult<RuleViolation> result = tested.execute(graphCommandExecutionContext);
    assertEquals(CommandResult.Type.INFO, result.getType());
    verify(ruleManager, never()).evaluate(eq(ruleSet), any(RuleEvaluationContext.class));
    verify(edgeContent, times(1)).setTargetConnection(eq(connection));
    verify(edgeContent, never()).setSourceConnection(any(Connection.class));
    assertEquals(targetMagnet.get(), tested.lastConnection);
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) Connection(org.kie.workbench.common.stunner.core.graph.content.view.Connection) TestingGraphUtils.verifyConnection(org.kie.workbench.common.stunner.core.TestingGraphUtils.verifyConnection) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) RuleEvaluationContext(org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext) RuleViolation(org.kie.workbench.common.stunner.core.rule.RuleViolation) Test(org.junit.Test)

Example 4 with MagnetConnection

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

the class ConnectorDragProxyImpl method show.

@Override
@SuppressWarnings("unchecked")
public DragProxy<AbstractCanvasHandler, Item, DragProxyCallback> show(final Item item, final int x, final int y, final DragProxyCallback callback) {
    // Source connector's shape - Obtain the shape for the source node.
    final Node<View<?>, Edge> sourceNode = item.getSourceNode();
    final Shape<?> sourceNodeShape = getCanvas().getShape(sourceNode.getUUID());
    // Target connector's shape - Create a temporary shape view, that will act as the connector's target node.
    final WiresShapeView transientShapeView = new WiresShapeView<>(new MultiPath().rect(0, 0, 1, 1).setFillAlpha(0).setStrokeAlpha(0));
    getWiresManager().getMagnetManager().createMagnets(transientShapeView);
    // Create the transient connector's shape and view.
    final Edge<View<?>, Node> edge = item.getEdge();
    final Shape<?> edgeShape = ((ShapeFactory<Object, ?>) item.getShapeFactory()).newShape(edge.getContent().getDefinition());
    final EdgeShape connectorShape = (EdgeShape) edgeShape;
    this.connectorShapeView = (WiresConnectorView<?>) edgeShape.getShapeView();
    // Register and update shape's view as for edge bean's state.
    getWiresManager().register(connectorShapeView);
    connectorShape.applyProperties(edge, MutationContext.STATIC);
    // Apply connector's connections for both source and target shapes.
    // Using center connector strategy, so magnet index 0.
    final MagnetConnection centerConnection = new MagnetConnection.Builder().atX(0).atY(0).magnet(0).build();
    connectorShapeView.connect(sourceNodeShape.getShapeView(), centerConnection, transientShapeView, centerConnection);
    // Optimize the index and show the drag proxy for the temporary shape view.
    graphBoundsIndexer.build(canvasHandler.getDiagram().getGraph());
    shapeViewDragProxyFactory.show(transientShapeView, x, y, new DragProxyCallback() {

        @Override
        public void onStart(final int x, final int y) {
            callback.onStart(x, y);
            // As using center magnet, update the connection.
            connectorShapeView.updateForCenterConnection();
        }

        @Override
        public void onMove(final int x, final int y) {
            callback.onMove(x, y);
            // As using center magnet, update the connection.
            connectorShapeView.updateForCenterConnection();
        }

        @Override
        public void onComplete(final int x, final int y) {
            callback.onComplete(x, y);
            deregisterTransientConnector();
            getCanvas().draw();
        }
    });
    return this;
}
Also used : MultiPath(com.ait.lienzo.client.core.shape.MultiPath) EdgeShape(org.kie.workbench.common.stunner.core.client.shape.EdgeShape) Node(org.kie.workbench.common.stunner.core.graph.Node) ShapeFactory(org.kie.workbench.common.stunner.core.client.shape.factory.ShapeFactory) WiresShapeView(org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresShapeView) View(org.kie.workbench.common.stunner.core.graph.content.view.View) WiresConnectorView(org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresConnectorView) MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) WiresShapeView(org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresShapeView) DragProxyCallback(org.kie.workbench.common.stunner.core.client.components.drag.DragProxyCallback) Edge(org.kie.workbench.common.stunner.core.graph.Edge)

Example 5 with MagnetConnection

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

the class ConnectionAcceptorControlImplTest method testCreateConnections.

@Test
public void testCreateConnections() {
    // New default connection for a graph element.
    Element element = mock(Element.class);
    View<?> content = mock(View.class);
    BoundsImpl bounds = new BoundsImpl(new BoundImpl(0d, 0d), new BoundImpl(10d, 20d));
    when(element.getContent()).thenReturn(content);
    when(content.getBounds()).thenReturn(bounds);
    MagnetConnection c1 = ConnectionAcceptorControlImpl.createConnection(element);
    assertEquals(5, c1.getLocation().getX(), 0);
    assertEquals(10, c1.getLocation().getY(), 0);
    assertEquals(MagnetConnection.MAGNET_CENTER, c1.getMagnetIndex().getAsInt());
    assertFalse(c1.isAuto());
    // New default connection for wires.
    WiresConnection wiresConnection = mock(WiresConnection.class);
    when(wiresConnection.isAutoConnection()).thenReturn(true);
    WiresMagnet wiresMagnet = mock(WiresMagnet.class);
    when(wiresMagnet.getX()).thenReturn(122d);
    when(wiresMagnet.getY()).thenReturn(543d);
    when(wiresMagnet.getIndex()).thenReturn(7);
    MagnetConnection c2 = ConnectionAcceptorControlImpl.createConnection(wiresConnection, wiresMagnet);
    assertEquals(122, c2.getLocation().getX(), 0);
    assertEquals(543, c2.getLocation().getY(), 0);
    assertEquals(7, c2.getMagnetIndex().getAsInt());
    assertTrue(c2.isAuto());
    // Connections (view magnets) can be nullified.
    assertNull(ConnectionAcceptorControlImpl.createConnection(null));
    assertNull(ConnectionAcceptorControlImpl.createConnection(wiresConnection, null));
    assertNull(ConnectionAcceptorControlImpl.createConnection(null, null));
}
Also used : MagnetConnection(org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) Element(org.kie.workbench.common.stunner.core.graph.Element) BoundImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundImpl) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet) BoundsImpl(org.kie.workbench.common.stunner.core.graph.content.view.BoundsImpl) Test(org.junit.Test)

Aggregations

MagnetConnection (org.kie.workbench.common.stunner.core.graph.content.view.MagnetConnection)8 Test (org.junit.Test)6 WiresConnection (com.ait.lienzo.client.core.shape.wires.WiresConnection)3 WiresMagnet (com.ait.lienzo.client.core.shape.wires.WiresMagnet)3 MagnetManager (com.ait.lienzo.client.core.shape.wires.MagnetManager)2 Point2D (com.ait.lienzo.client.core.types.Point2D)2 TestingGraphUtils.verifyConnection (org.kie.workbench.common.stunner.core.TestingGraphUtils.verifyConnection)2 Connection (org.kie.workbench.common.stunner.core.graph.content.view.Connection)2 RuleEvaluationContext (org.kie.workbench.common.stunner.core.rule.RuleEvaluationContext)2 RuleViolation (org.kie.workbench.common.stunner.core.rule.RuleViolation)2 IPrimitive (com.ait.lienzo.client.core.shape.IPrimitive)1 MultiPath (com.ait.lienzo.client.core.shape.MultiPath)1 Shape (com.ait.lienzo.client.core.shape.Shape)1 ControlHandleList (com.ait.lienzo.client.core.shape.wires.ControlHandleList)1 IControlHandleList (com.ait.lienzo.client.core.shape.wires.IControlHandleList)1 OptionalInt (java.util.OptionalInt)1 WiresConnectorView (org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresConnectorView)1 WiresShapeView (org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresShapeView)1 CanvasViolation (org.kie.workbench.common.stunner.core.client.command.CanvasViolation)1 DragProxyCallback (org.kie.workbench.common.stunner.core.client.components.drag.DragProxyCallback)1