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));
}
}
}
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);
}
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);
}
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;
}
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));
}
Aggregations