Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Place connection anchors on a circle, but such that connections are on a line with the node's center
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 23:51 Go to next message
Oskar van Rest is currently offline Oskar van RestFriend
Messages: 13
Registered: February 2012
Junior Member
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 11:02 Go to previous messageGo to next message
Sebastiaan Blommers is currently offline Sebastiaan BlommersFriend
Messages: 3
Registered: April 2015
Junior Member
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);
	}
}

Re: Place connection anchors on a circle, but such that connections are on a line with the node's ce [message #1691530 is a reply to message #1691449] Tue, 07 April 2015 22:05 Go to previous messageGo to next message
Oskar van Rest is currently offline Oskar van RestFriend
Messages: 13
Registered: February 2012
Junior Member
Great to hear it was useful Smile
Re: Place connection anchors on a circle, but such that connections are on a line with the node's ce [message #1691574 is a reply to message #1691530] Wed, 08 April 2015 09:13 Go to previous message
Sebastiaan Blommers is currently offline Sebastiaan BlommersFriend
Messages: 3
Registered: April 2015
Junior Member
One tiny addition, when zooming the center was not correctly translated, when using a fixed width and/or height for nodes. I just translate size before calculating the center and now it is fine Very Happy

Dimension size = getOwner().getSize();
getOwner().translateToAbsolute(size);
Point center = new Point(box.x + (size.width/2), box.y + (size.height/2));
Previous Topic:GMF Code Generation customization
Next Topic:Generating PDFs from forms
Goto Forum:
  


Current Time: Tue Jun 27 15:52:10 GMT 2017

Powered by FUDForum. Page generated in 0.03712 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software