Place connection anchors on a circle, but such that connections are on a line with the node's center [message #1379970] |
Sat, 24 May 2014 19:51  |
Eclipse User |
|
|
|
This post is not a question, but instead provides some useful code that I needed for something and thought I might as well share.
On http://gmfsamples.tuxfamily.org/wiki/doku.php?id=gmf_tutorial6 is already described how to put connection anchors on a circle. However, if you want them to be on a circle and such that if you would extend the connection, it would go through the center of the node, then first modify method createNodePlate() in your EditPart like this:
protected YourFigure createNodePlate() {
return new CircleFigureWithCenterAnchor(40, 40);
}
Create new class CircleFigureWithCenterAnchor.java:
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.gmf.runtime.gef.ui.internal.figures.CircleFigure;
@SuppressWarnings("restriction")
public class CircleFigureWithCenterAnchor extends CircleFigure {
public CircleFigureWithCenterAnchor(int width, int height) {
super(width, height);
}
@Override
protected ConnectionAnchor createAnchor(PrecisionPoint p) {
if (p == null)
return createDefaultAnchor();
return new SlidableCenteredOvalAnchor(this, p);
}
@Override
protected ConnectionAnchor createDefaultAnchor() {
return new SlidableCenteredOvalAnchor(this);
}
}
Create new class SlidableCenteredOvalAnchor.java
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.figures.IOvalAnchorableFigure;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.LineSeg;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.SlidableAnchor;
public class SlidableCenteredOvalAnchor
extends SlidableAnchor {
private final NodeFigure f;
public SlidableCenteredOvalAnchor(NodeFigure f) {
super(f);
this.f = f;
}
public SlidableCenteredOvalAnchor(NodeFigure f, PrecisionPoint p) {
super(f,p);
this.f = f;
}
@Override
protected PointList getIntersectionPoints(Point ownReference, Point foreignReference) {
Rectangle ellipseBox = new PrecisionRectangle(((IOvalAnchorableFigure)getOwner()).getOvalBounds());
getOwner().translateToAbsolute(ellipseBox);
Point center = new Point(ellipseBox.x + (f.getSize().width/2), ellipseBox.y + (f.getSize().height/2));
return new LineSeg(center, foreignReference).getLineIntersectionsWithEllipse(ellipseBox);
}
}
|
|
|
Re: Place connection anchors on a circle, but such that connections are on a line with the node's ce [message #1691449 is a reply to message #1379970] |
Tue, 07 April 2015 07:02   |
Eclipse User |
|
|
|
DUDE ! AWESOME.
I use it for Rectangle now too, makes modeling quite easy now (no strange anchors)
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.gmf.runtime.gef.ui.figures.DefaultSizeNodeFigure;
public class RectangleFigureWithCenterAnchor extends DefaultSizeNodeFigure {
public RectangleFigureWithCenterAnchor(int width, int height) {
super(width, height);
}
@Override
protected ConnectionAnchor createAnchor(PrecisionPoint p) {
if (p == null)
return createDefaultAnchor();
return new SlidableCenteredRectangleAnchor(this, p);
}
@Override
protected ConnectionAnchor createDefaultAnchor() {
return new SlidableCenteredRectangleAnchor(this);
}
}
AND
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.PrecisionPoint;
import org.eclipse.draw2d.geometry.PrecisionRectangle;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.LineSeg;
import org.eclipse.gmf.runtime.draw2d.ui.geometry.PointListUtilities;
import org.eclipse.gmf.runtime.gef.ui.figures.NodeFigure;
import org.eclipse.gmf.runtime.gef.ui.figures.SlidableAnchor;
public class SlidableCenteredRectangleAnchor
extends SlidableAnchor {
private final NodeFigure f;
public SlidableCenteredRectangleAnchor(NodeFigure f) {
super(f);
this.f = f;
}
public SlidableCenteredRectangleAnchor(NodeFigure f, PrecisionPoint p) {
super(f,p);
this.f = f;
}
@Override
protected PointList getIntersectionPoints(Point ownReference, Point foreignReference) {
Rectangle box = new PrecisionRectangle(getOwner().getBounds());
getOwner().translateToAbsolute(box);
Point center = new Point(box.x + (f.getSize().width/2), box.y + (f.getSize().height/2));
PointList points = PointListUtilities.createPointsFromRect(box);
return new LineSeg(center, foreignReference).getLineIntersectionsWithLineSegs(points);
}
}
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.03525 seconds