Search in sources :

Example 6 with WiresConnector

use of com.ait.lienzo.client.core.shape.wires.WiresConnector in project lienzo-core by ahome-it.

the class ShapeControlUtils method checkForAndApplyLineSplice.

public static boolean checkForAndApplyLineSplice(final WiresManager wiresManager, final WiresShape shape) {
    if (!wiresManager.isSpliceEnabled() || (shape.getMagnets() == null)) {
        // cannot connect to a shape with no magnets.
        return true;
    }
    boolean accept = true;
    for (final WiresConnector c : wiresManager.getConnectorList()) {
        final Point2DArray linePoints = ((OrthogonalPolyLine) c.getLine()).getComputedPoint2DArray();
        final MultiPath path = shape.getPath();
        Point2DArray intersectPoints = null;
        final Point2D absLoc = path.getComputedLocation();
        intersectPoints = getIntersections(linePoints, path, intersectPoints, absLoc);
        if (((c.getHeadConnection().getMagnet() != null) && (c.getHeadConnection().getMagnet().getMagnets().getWiresShape() == shape)) || ((c.getTailConnection().getMagnet() != null) && (c.getTailConnection().getMagnet().getMagnets().getWiresShape() == shape))) {
            // don't split yourself
            return accept;
        }
        if (intersectPoints != null) {
            final WiresConnection headCon = c.getHeadConnection();
            final WiresConnection tailCon = c.getTailConnection();
            if (intersectPoints.size() == 1) {
                // one arrow end is enclosed in the shape, we can only splice/connect if that connection is not already connected.
                final BoundingBox bbox = shape.getContainer().getComputedBoundingPoints().getBoundingBox();
                if (bbox.contains(headCon.getPoint()) && (headCon.getMagnet() != null)) {
                    return accept;
                } else if (bbox.contains(tailCon.getPoint()) && (headCon.getMagnet() != null)) {
                    return accept;
                } else {
                    throw new RuntimeException("Defensive programming: should not be possible if there is a single intersection.");
                }
            }
            c.getWiresConnectorHandler().getControl().hideControlPoints();
            final Point2DArray oldPoints = c.getLine().getPoint2DArray();
            int firstSegmentIndex = Integer.MAX_VALUE;
            int lastSegmentIndex = 0;
            for (final Point2D p : intersectPoints) {
                final double x = p.getX() + absLoc.getX();
                final double y = p.getY() + absLoc.getY();
                // get first and last segment, this can happen if shape straddles multiple segments of the line
                final int pointIndex = WiresConnectorControlImpl.getIndexForSelectedSegment(c, (int) x, (int) y, oldPoints);
                if (pointIndex < firstSegmentIndex) {
                    firstSegmentIndex = pointIndex;
                }
                if (pointIndex > lastSegmentIndex) {
                    lastSegmentIndex = pointIndex;
                }
            }
            WiresConnector c2 = null;
            // record these, as they may need restoring later.
            double tailXOffset = 0;
            double tailYOffset = 0;
            boolean tailAutoConnection = false;
            Point2D tailPoint = null;
            WiresMagnet tailMagnet = null;
            if (tailCon != null) {
                tailXOffset = tailCon.getXOffset();
                tailYOffset = tailCon.getYOffset();
                tailAutoConnection = tailCon.isAutoConnection();
                tailMagnet = tailCon.getMagnet();
                tailPoint = tailCon.getPoint();
            }
            if (firstSegmentIndex > 0) {
                final Point2DArray newPoints1 = new Point2DArray();
                final Point2DArray newPoints2 = new Point2DArray();
                newPoints1.push(oldPoints.get(0));
                for (int i = 1; i < firstSegmentIndex; i++) {
                    newPoints1.push(oldPoints.get(i));
                }
                final WiresMagnet cmagnet = shape.getMagnets().getMagnet(1);
                // check if isAllowed
                WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(headCon, true, shape, cmagnet, false);
                accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(tailCon, false, shape, cmagnet, false);
                if (!accept) {
                    return accept;
                }
                if (intersectPoints.size() > 1) {
                    final Point2D startPoint = new Point2D(cmagnet.getControl().getX(), cmagnet.getControl().getY());
                    newPoints2.push(startPoint);
                    // will skip any segments between first and last. this happens if a shape straddles multiple segments.
                    for (int i = lastSegmentIndex; i < oldPoints.size(); i++) {
                        newPoints2.push(oldPoints.get(i));
                    }
                    final AbstractDirectionalMultiPointShape<?> line = c.getLine().copy();
                    line.setPoint2DArray(newPoints2);
                    c2 = new WiresConnector(line, c.getHeadDecorator().copy(), c.getTailDecorator().copy());
                    wiresManager.register(c2);
                    final WiresConnection headCon2 = c2.getHeadConnection();
                    headCon2.setAutoConnection(true);
                    // reset, if not already 0
                    headCon2.setXOffset(0);
                    headCon2.setYOffset(0);
                    final WiresConnection tailCon2 = c2.getTailConnection();
                    // preserve tail auto connection
                    tailCon2.setAutoConnection(tailCon.isAutoConnection());
                    tailCon2.setMagnet(tailCon.getMagnet());
                    // reset, if not already 0
                    tailCon2.setXOffset(tailCon.getXOffset());
                    tailCon2.setYOffset(tailCon.getYOffset());
                    tailCon2.setPoint(tailCon.getPoint());
                    accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(headCon2, true, shape, cmagnet, true);
                    if (!accept) {
                        // we already checked isAllowed before mutation, so this in theory should not be needed. Adding for future proofing and completeness - in
                        // case a future version doesn't require identical behavioural logic for allowed and accept.
                        tailCon2.setMagnet(null);
                        wiresManager.deregister(c2);
                        return accept;
                    }
                }
                // this is done after the potential newPoitns2, as it reads values from the original connector.
                final Point2D endPoint = new Point2D(cmagnet.getControl().getX(), cmagnet.getControl().getY());
                newPoints1.push(endPoint);
                tailCon.setAutoConnection(true);
                // reset, if not already 0
                tailCon.setXOffset(0);
                tailCon.setYOffset(0);
                tailCon.setPoint(endPoint);
                c.getLine().setPoint2DArray(newPoints1);
                accept = accept && WiresConnectionControlImpl.allowedMagnetAndUpdateAutoConnections(tailCon, false, shape, cmagnet, true);
                if (!accept) {
                    // case a future version doesn't require identical behavioural logic for allowed and accept.
                    if (c2 != null) {
                        c2.getTailConnection().setMagnet(null);
                        c2.getHeadConnection().setMagnet(null);
                        wiresManager.deregister(c2);
                    }
                    tailCon.setAutoConnection(tailAutoConnection);
                    // reset, if not already 0
                    tailCon.setXOffset(tailXOffset);
                    tailCon.setYOffset(tailYOffset);
                    tailCon.setMagnet(tailMagnet);
                    tailCon.setPoint(tailPoint);
                    return accept;
                }
            }
        }
    }
    return accept;
}
Also used : MultiPath(com.ait.lienzo.client.core.shape.MultiPath) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector) WiresConnection(com.ait.lienzo.client.core.shape.wires.WiresConnection) Point2DArray(com.ait.lienzo.client.core.types.Point2DArray) Point2D(com.ait.lienzo.client.core.types.Point2D) OrthogonalPolyLine(com.ait.lienzo.client.core.shape.OrthogonalPolyLine) BoundingBox(com.ait.lienzo.client.core.types.BoundingBox) WiresMagnet(com.ait.lienzo.client.core.shape.wires.WiresMagnet)

Example 7 with WiresConnector

use of com.ait.lienzo.client.core.shape.wires.WiresConnector in project lienzo-core by ahome-it.

the class WiresCompositeControlImpl method onMove.

@Override
public boolean onMove(final double dx, final double dy) {
    if (isOutOfBounds(dx, dy)) {
        return true;
    }
    delta = new Point2D(dx, dy);
    // Delegate location deltas to shape controls and obtain current locations for each one.
    final Collection<WiresShape> shapes = selectedShapes;
    if (!shapes.isEmpty()) {
        final WiresManager wiresManager = shapes.iterator().next().getWiresManager();
        final Point2D[] locs = new Point2D[shapes.size()];
        int i = 0;
        for (final WiresShape shape : shapes) {
            shape.getControl().onMove(dx, dy);
            locs[i++] = getCandidateShapeLocationRelativeToInitialParent(shape);
        }
        // Check if new locations are allowed.
        final WiresShape[] shapesArray = toArray(shapes);
        final boolean locationAllowed = wiresManager.getLocationAcceptor().allow(shapesArray, locs);
        // Do the updates.
        if (locationAllowed) {
            i = 0;
            for (final WiresShape shape : shapes) {
                shape.getControl().getParentPickerControl().setShapeLocation(locs[i++]);
                postUpdateShape(shape);
            }
        }
    }
    final Collection<WiresConnector> connectors = selectedConnectors;
    if (!connectors.isEmpty()) {
        // Update connectors and connections.
        for (final WiresConnector connector : connectors) {
            final WiresConnector.WiresConnectorHandler handler = connector.getWiresConnectorHandler();
            handler.getControl().move(dx, dy, true, true);
            WiresConnector.updateHeadTailForRefreshedConnector(connector);
        }
    }
    ShapeControlUtils.updateSpecialConnections(m_connectorsWithSpecialConnections, false);
    return false;
}
Also used : WiresShape(com.ait.lienzo.client.core.shape.wires.WiresShape) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector) Point2D(com.ait.lienzo.client.core.types.Point2D) WiresManager(com.ait.lienzo.client.core.shape.wires.WiresManager)

Example 8 with WiresConnector

use of com.ait.lienzo.client.core.shape.wires.WiresConnector in project lienzo-core by ahome-it.

the class WiresCompositeControlImpl method reset.

@Override
public void reset() {
    for (final WiresShape shape : selectedShapes) {
        shape.getControl().reset();
        enableDocking(shape.getControl());
    }
    for (final WiresConnector connector : selectedConnectors) {
        final WiresConnector.WiresConnectorHandler handler = connector.getWiresConnectorHandler();
        handler.getControl().reset();
        WiresConnector.updateHeadTailForRefreshedConnector(connector);
    }
    clearState();
}
Also used : WiresShape(com.ait.lienzo.client.core.shape.wires.WiresShape) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector)

Example 9 with WiresConnector

use of com.ait.lienzo.client.core.shape.wires.WiresConnector in project lienzo-core by ahome-it.

the class WiresCompositeControlImpl method execute.

@Override
public void execute() {
    for (final WiresShape shape : selectedShapes) {
        shape.getControl().getContainmentControl().execute();
        postUpdateShape(shape);
    }
    for (final WiresConnector connector : selectedConnectors) {
        connector.getWiresConnectorHandler();
        WiresConnector.updateHeadTailForRefreshedConnector(connector);
    }
    ShapeControlUtils.updateSpecialConnections(m_connectorsWithSpecialConnections, true);
    clear();
}
Also used : WiresShape(com.ait.lienzo.client.core.shape.wires.WiresShape) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector)

Example 10 with WiresConnector

use of com.ait.lienzo.client.core.shape.wires.WiresConnector in project lienzo-core by ahome-it.

the class WiresConnectionControlImpl method allowedMagnetAndUpdateAutoConnections.

public static boolean allowedMagnetAndUpdateAutoConnections(final WiresConnection connection, final boolean isHead, final WiresShape shape, final WiresMagnet currentMagnet, final boolean applyAccept) {
    final WiresConnector connector = connection.getConnector();
    // shape can be null, to see if a connection can be unconnected to a magnet
    boolean accept;
    WiresShape headS;
    WiresShape tailS;
    if (isHead) {
        accept = connector.getConnectionAcceptor().headConnectionAllowed(connection, shape);
        headS = shape;
        tailS = (connector.getTailConnection().getMagnet() != null) ? connector.getTailConnection().getMagnet().getMagnets().getWiresShape() : null;
    } else {
        accept = connector.getConnectionAcceptor().tailConnectionAllowed(connection, shape);
        headS = (connector.getHeadConnection().getMagnet() != null) ? connector.getHeadConnection().getMagnet().getMagnets().getWiresShape() : null;
        tailS = shape;
    }
    if (applyAccept && accept) {
        accept = accept && acceptMagnetAndUpdateAutoConnection(connection, isHead, headS, tailS, currentMagnet);
        if (!accept) {
            throw new RuntimeException("This should never happen, acceptors should not fail if the alled passed. Added for defensive programming checking");
        }
    }
    return accept;
}
Also used : WiresShape(com.ait.lienzo.client.core.shape.wires.WiresShape) WiresConnector(com.ait.lienzo.client.core.shape.wires.WiresConnector)

Aggregations

WiresConnector (com.ait.lienzo.client.core.shape.wires.WiresConnector)11 WiresShape (com.ait.lienzo.client.core.shape.wires.WiresShape)9 Point2D (com.ait.lienzo.client.core.types.Point2D)4 MultiPath (com.ait.lienzo.client.core.shape.MultiPath)1 OrthogonalPolyLine (com.ait.lienzo.client.core.shape.OrthogonalPolyLine)1 WiresConnection (com.ait.lienzo.client.core.shape.wires.WiresConnection)1 WiresMagnet (com.ait.lienzo.client.core.shape.wires.WiresMagnet)1 WiresManager (com.ait.lienzo.client.core.shape.wires.WiresManager)1 WiresConnectorControl (com.ait.lienzo.client.core.shape.wires.handlers.WiresConnectorControl)1 BoundingBox (com.ait.lienzo.client.core.types.BoundingBox)1 Point2DArray (com.ait.lienzo.client.core.types.Point2DArray)1 HashMap (java.util.HashMap)1 WiresConnectorView (org.kie.workbench.common.stunner.client.lienzo.shape.view.wires.WiresConnectorView)1 DragProxyCallback (org.kie.workbench.common.stunner.core.client.components.drag.DragProxyCallback)1 AbstractDragProxy (org.kie.workbench.common.stunner.lienzo.primitive.AbstractDragProxy)1 WiresConnectorDragProxy (org.kie.workbench.common.stunner.lienzo.primitive.WiresConnectorDragProxy)1 WiresShapeDragProxy (org.kie.workbench.common.stunner.lienzo.primitive.WiresShapeDragProxy)1