Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » FixedConnectionAnchors from logic example
FixedConnectionAnchors from logic example [message #226896] Tue, 21 April 2009 20:22
Frantisek Kocun is currently offline Frantisek KocunFriend
Messages: 16
Registered: July 2009
Junior Member
Hi, I want to use FixedConnectionAnchor from logic example. I have a base
class with ports (anchors from which my edit parts derive).

Questions:

1.) I need to know when ConnectionAnchor is occupied. I want only one
connection to one anchor (it is not in emf domain model). How I know that
some connection has been deleted or anchor removed from one element to
another?

2.) I have a problem that when I create a node it is bigger than screen.
When I leave out the code form constructor, which is creating anchors,
figure has right dimension. I can't figure out what is happening.

Thank for help in advance!


Here is the code:

public abstract class NodeEditPart extends ShapeNodeEditPart {

public NodeEditPart(View view) {
super(view);
FixedConnectionAnchor c = new FixedConnectionAnchor(getFigure());
c.offsetH = 20;
c.offsetV = 20;
addTargetAnchor(c);

c = new FixedConnectionAnchor(getFigure());
c.offsetH = 40;
c.offsetV = 40;
addTargetAnchor(c);
}



private List<ConnectionAnchor> targetAnchors = new
ArrayList<ConnectionAnchor>();

private ConnectionAnchor lastTargetAnchor;

public ConnectionAnchor connectionAnchorAt(Point p) {
ConnectionAnchor closest = null;
long min = Long.MAX_VALUE;

for(ConnectionAnchor c : targetAnchors){
Point p2 = c.getLocation(null);
long d = p.getDistance2(p2);
if (d < min) {
min = d;
closest = c;
}
}
return closest;
}

protected void addTargetAnchor(ConnectionAnchor c){
targetAnchors.add(c);
}

@Override
public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart
connEditPart) {
if(lastTargetAnchor == null){
lastTargetAnchor = targetAnchors.get(0);
}
return lastTargetAnchor;
}

@Override
public ConnectionAnchor getTargetConnectionAnchor(Request request) {
CreateConnectionRequest r = (CreateConnectionRequest) request;
ConnectionAnchor c = connectionAnchorAt(r.getLocation());

this.lastTargetAnchor = c;
return c;
}

}


FixedConnectionAnchor is copied from logic example.


public class FixedConnectionAnchor
extends AbstractConnectionAnchor
{

public boolean leftToRight = true;
public int offsetH;
public int offsetV;
public boolean topDown = true;

public FixedConnectionAnchor(IFigure owner) {
super(owner);
}

/**
* @see org.eclipse.draw2d.AbstractConnectionAnchor#ancestorMoved(IF igure)
*/
public void ancestorMoved(IFigure figure) {
if (figure instanceof ScalableFigure)
return;
super.ancestorMoved(figure);
}

public Point getLocation(Point reference) {
Rectangle r = getOwner().getBounds();
int x,y;
if (topDown)
y = r.y + offsetV;
else
y = r.bottom() - 1 - offsetV;

if (leftToRight)
x = r.x + offsetH;
else
x = r.right() - 1 - offsetH;

Point p = new PrecisionPoint(x,y);
getOwner().translateToAbsolute(p);
return p;
}

public Point getReferencePoint(){
return getLocation(null);
}

/**
* @param offsetH
* The offsetH to set.
*/
public void setOffsetH(int offsetH) {
this.offsetH = offsetH;
fireAnchorMoved();
}

/**
* @param offsetV
* The offsetV to set.
*/
public void setOffsetV(int offsetV) {
this.offsetV = offsetV;
fireAnchorMoved();
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if (o instanceof FixedConnectionAnchor) {
FixedConnectionAnchor fa = (FixedConnectionAnchor)o;

if (fa.leftToRight == this.leftToRight &&
fa.topDown == this.topDown &&
fa.offsetH == this.offsetH &&
fa.offsetV == this.offsetV &&
fa.getOwner() == this.getOwner()) {
return true;
}
}

return false;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return ((this.leftToRight ? 31 : 0) + (this.topDown ? 37 : 0) +
this.offsetH * 43 + this.offsetV * 47) ^
this.getOwner().hashCode();
}

}
Previous Topic:show non containment reference inside the node
Next Topic:How can I initializing diagrams with elements
Goto Forum:
  


Current Time: Tue Nov 21 01:07:22 GMT 2017

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

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