@Override protected List<?> createSelectionHandles() { List<BendpointHandle> list = new ArrayList<BendpointHandle>(); ConnectionEditPart connEP = (ConnectionEditPart) getHost(); PointList points = getConnection().getPoints(); List<?> bendPoints = (List<?>) getConnection().getRoutingConstraint(); int bendPointIndex = 0; Point currBendPoint = null; if (bendPoints == null) bendPoints = NULL_CONSTRAINT; else if (!bendPoints.isEmpty()) currBendPoint = ((Bendpoint) bendPoints.get(0)).getLocation(); for (int i = 0; i < points.size() - 1; i++) { // Put a create handle on the middle of every segment list.add(new SnappingBendpointCreationHandle(connEP, bendPointIndex, i)); // If the current user bendpoint matches a bend location, show a // move handle if (i < points.size() - 1 && bendPointIndex < bendPoints.size() && currentBendPointMatches(currBendPoint, points.getPoint(i + 1))) { list.add(new SnappingBendpointMoveHandle(connEP, bendPointIndex, i + 1)); // Go to the next user bendpoint bendPointIndex++; if (bendPointIndex < bendPoints.size()) currBendPoint = ((Bendpoint) bendPoints.get(bendPointIndex)).getLocation(); } } return list; }
/** * straightenPoints This is a simpler version of the. * * @see updateIfNotRectilinear that simply ensures that the lines are horizontal or vertical * without any intelligence in terms of shortest distance around a rectangle. * @param newLine PointList to check for rectilinear qualities and change if necessary. * @param tolerance int tolerance value by which points will be straightened in HiMetrics */ protected static void straightenPoints(final PointList newLine, final int tolerance) { for (int i = 0; i < newLine.size() - 1; i++) { final Point ptCurrent = newLine.getPoint(i); final Point ptNext = newLine.getPoint(i + 1); final int xDelta = Math.abs(ptNext.x - ptCurrent.x); final int yDelta = Math.abs(ptNext.y - ptCurrent.y); if (xDelta < yDelta) { if (xDelta > tolerance) { return; } if (i == newLine.size() - 2) { // The last point is more important than the other (not // change the end of the line) ptCurrent.x = ptNext.x; } else { ptNext.x = ptCurrent.x; } } else { if (yDelta > tolerance) { return; } if (i == newLine.size() - 2) { // The last point is more important than the other (not // change the end of the line) ptCurrent.y = ptNext.y; } else { ptNext.y = ptCurrent.y; } } newLine.setPoint(ptNext, i + 1); } }
protected List<BezierPoint> getBezierPoints(PointList points, double zoom) { List<BezierPoint> ret = new ArrayList<BezierPoint>(points.size()); for (int i = 0; i < points.size(); i++) { int bezierDistance = (int) (getGeneralBezierDistance() * zoom); Point point = points.getPoint(i); ret.add(new BezierPoint(point.x, point.y, bezierDistance, bezierDistance)); } return ret; }
private boolean trunkVertexEqual(final Connection connMaster, final Connection connSlave) { final PointList cmPts = connMaster.getPoints(); final PointList csPts = connSlave.getPoints(); if (cmPts.size() > 2 && csPts.size() > 2) { return cmPts.getPoint(2).equals(csPts.getPoint(2)); } return false; }
@Override public boolean test() throws Exception { boolean location = false; PointList points = ((PolylineConnectionEx) connectionEditPart.getFigure()).getPoints(); if (checkFirstBendpoint) { location = !initialLocation.equals(points.getFirstPoint()); } else if (checkLastBendpoint) { location = !initialLocation.equals(points.getLastPoint()); } else if (bendpointPosition >= 0 || bendpointPosition < points.size()) { location = !initialLocation.equals(points.getPoint(bendpointPosition)); } return location; }
@Override public void route(Connection conn) { if (isDirty) { ignoreInvalidate = true; processStaleConnections(); isDirty = false; List<?> updated = algorithm.solve(); Connection current; for (int i = 0; i < updated.size(); i++) { Path path = (Path) updated.get(i); current = (Connection) path.data; current.revalidate(); PointList points = path.getPoints().getCopy(); Point ref1; Point ref2; Point start; Point end; ref1 = new PrecisionPoint(points.getPoint(1)); ref2 = new PrecisionPoint(points.getPoint(points.size() - 2)); current.translateToAbsolute(ref1); current.translateToAbsolute(ref2); start = current.getSourceAnchor().getLocation(ref1).getCopy(); end = current.getTargetAnchor().getLocation(ref2).getCopy(); current.translateToRelative(start); current.translateToRelative(end); points.setPoint(start, 0); points.setPoint(end, points.size() - 1); current.setPoints(points); } ignoreInvalidate = false; } }
/** * UpdateConstraint Updates the constraint value for the connection based on the tree vertex. * * @param conn Connection whose constraint is to be updated. */ protected void updateConstraint(final Connection conn) { boolean update = conn != null && conn.getSourceAnchor() != null && conn.getTargetAnchor() != null; update = update && conn.getSourceAnchor().getOwner() != null && conn.getTargetAnchor().getOwner() != null; if (update) { if (isUpdatingPeers()) { return; } List bendpoints = (List) conn.getRoutingConstraint(); if (bendpoints == null) { bendpoints = new ArrayList(conn.getPoints().size()); } final Point sourceRefPoint = conn.getSourceAnchor().getReferencePoint(); conn.translateToRelative(sourceRefPoint); final Point targetRefPoint = conn.getTargetAnchor().getReferencePoint(); conn.translateToRelative(targetRefPoint); final Point ptTrunk = getTrunkLocation(conn); final Point ptSource = getBranchRouter().getSourceLocation(conn, ptTrunk); bendpoints.clear(); final PointList pts = getBranchRouter().recreateBranch(conn, ptSource, ptTrunk); for (int i = 0; i < pts.size(); i++) { final Bendpoint bp = new AbsoluteBendpoint(pts.getPoint(i)); bendpoints.add(bp); } setUpdatingPeers(true); try { setConstraint(conn, bendpoints); conn.invalidate(); conn.validate(); } finally { setUpdatingPeers(false); } } }
private Point getAbsoluteEdgeExtremity( ConnectionNodeEditPart editPart, boolean isStart, Float[] preferPosition) { if (editPart == null) { return null; } PointList points = editPart.getConnectionFigure().getPoints().getCopy(); if (points.size() == 2 && new Point(0, 0).equals(points.getFirstPoint()) && new Point(100, 100).equals(points.getLastPoint())) { // not display yet. if (preferPosition != null) { if (isStart && preferPosition[0] != null) { return new Point(0, preferPosition[0].intValue()); } else if (!isStart && preferPosition[1] != null) { return new Point(0, preferPosition[1].intValue()); } } return SequenceUtil.getAbsoluteEdgeExtremity(editPart, isStart, false); } return SequenceUtil.getAbsoluteEdgeExtremity(editPart, isStart, true); }
/** * Utility method to determine if the given set of points conforms to the constraints of being a * connection tree-branch. 1. Points size must be 4. 2. Source point resides with-in boundary of * source shape based on orientation 3. Target point resides with-in boundary of target shape * based on orientation * * @param conn the <code>Connection</code> to test * @param points the <code>PointList</code> to test constraints against * @return <code>boolean</code> <code>true</code> if points represent valid tree branch, <code> * false</code> otherwise. */ public boolean isTreeBranch(final Connection conn, final PointList points) { boolean result = false; if (points.size() == 4) { // just check if ends are with-in the owner bounding box final Rectangle targetBounds = getTargetAnchorRelativeBounds(conn); final Rectangle sourceBounds = getSourceAnchorRelativeBounds(conn); if (isTopDown(conn)) { result = (points.getPoint(0).x > sourceBounds.x && points.getPoint(0).x < sourceBounds.x + sourceBounds.width) && (points.getPoint(3).x > targetBounds.x && points.getPoint(3).x < targetBounds.x + targetBounds.width); } else { result = (points.getPoint(0).y > sourceBounds.y && points.getPoint(0).y < sourceBounds.y + sourceBounds.height) && (points.getPoint(3).y > targetBounds.y && points.getPoint(3).y < targetBounds.y + targetBounds.height); } } return result; }
private PointList getTranslated(PointList source, Point offset) { PointList target = new PointList(source.size()); target.addAll(source); target.translate(offset); return target; }
@Override public void execute() { // configure the request EditPart source = request.getSourceEditPart(); EditPart target = request.getTargetEditPart(); PointList list = feedback.getPoints(); DiagramEditPart diagramEditPart = null; if (source.getParent() instanceof DiagramEditPart) { diagramEditPart = (DiagramEditPart) source.getParent(); } else if (source instanceof DiagramEditPart) { diagramEditPart = (DiagramEditPart) source; } else { if (source.getParent().getParent() instanceof DiagramEditPart) { diagramEditPart = (DiagramEditPart) source.getParent().getParent(); } } if (diagramEditPart == null) { return; } if (!(source instanceof DiagramEditPart || target instanceof DiagramEditPart)) { // one must be whitespace, we make it the target request.setTargetEditPart(diagramEditPart); // additionally we must cut the drawn connection // in half to share room with the element that will // be auto-created PointList newList = new PointList(); newList.addPoint(list.getFirstPoint()); if (list.size() == 4) { // rectilinear routing will have a three points newList.addPoint(list.getPoint(1)); } newList.addPoint(list.getMidpoint()); feedback.setPoints(newList); } // create the active tool's element first CreateConnectionCommand command = new CreateConnectionCommand(request, feedback); command.execute(); Connector_c newConnector = command.result; if (newConnector == null) { return; } // we need to now refresh the diagram to get the new edit part source.refresh(); source.getParent().refresh(); GraphicalEditPart newPart = (GraphicalEditPart) source.getViewer().getEditPartRegistry().get(newConnector); // transfer the feedback connection points to the newly created // edit part ((Connection) newPart.getFigure()).setPoints(feedback.getPoints().getCopy()); // now adjust the request for the new element to be created for each // terminal specification that requires auto-creation createElementForTerminalSpec( autoStartSpec, ((NonRootModelElement) diagramEditPart.getModel()).getModelRoot(), list, newPart, target, diagramEditPart); createElementForTerminalSpec( autoEndSpec, ((NonRootModelElement) diagramEditPart.getModel()).getModelRoot(), list, newPart, target, diagramEditPart); }
private void createElementForTerminalSpec( TerminalSpecification_c autoCreationSpec, ModelRoot modelRoot, PointList line, EditPart newPart, EditPart targetPart, final DiagramEditPart diagramEditPart) { ElementSpecification_c specification = ElementSpecification_c.getOneGD_ESOnR209(autoCreationSpec); if (specification != null && request instanceof GraphicsConnectionCreateRequest) { // deactivate the current tool ModelTool_c activeTool = ModelTool_c.ModelToolInstance( modelRoot, new ClassQueryInterface_c() { @Override public boolean evaluate(Object candidate) { return ((ModelTool_c) candidate).getActive(); } }); activeTool.setActive(false); // find the new tool and activate it ModelTool_c newTool = ModelTool_c.getOneCT_MTLOnR103( specification, new ClassQueryInterface_c() { @Override public boolean evaluate(Object candidate) { ModelTool_c tool = (ModelTool_c) candidate; return Model_c.getOneGD_MDOnR100(tool) == diagramEditPart.getModel(); } }); newTool.setActive(true); // create the new element if (specification.getSymboltype().equals("connector")) { // $NON-NLS-1$ // creating a connector // we want to create the new connector from the // midpoint of the drawn line to the end of the // drawn line PointList newLine = new PointList(); newLine.addPoint(line.getLastPoint()); if (line.size() == 4) { // rectilinear routing will have a three points newLine.addPoint(line.getPoint(2)); } newLine.addPoint(line.getMidpoint()); feedback.setPoints(newLine); // we also need to adjust the request's source and // target edit part, we swap the original target as the // source as if the new connection was drawn from // the original destination element request.setTargetEditPart(newPart); request.setSourceEditPart(targetPart); // need to update the location in the request for // those connectors that end on whitespace request.setLocation(line.getMidpoint()); // we need to configure the proper tool id in the // request GraphicsConnectionCreateRequest gRequest = (GraphicsConnectionCreateRequest) request; gRequest.setToolId(newTool.getTool_id()); CreateConnectionCommand command = new CreateConnectionCommand(request, feedback); command.disableCropping(); command.execute(); // reset the active tool states newTool.setActive(false); activeTool.setActive(true); } else if (specification.getSymboltype().equals("shape")) { // $NON-NLS-1$ // TODO: support shape auto creation } } // otherwise this is not an auto create specification }