|
Re: [ZEST] Center view on a selected node [message #901813 is a reply to message #894131] |
Tue, 14 August 2012 16:18 |
Fabian Günter Messages: 2 Registered: July 2012 |
Junior Member |
|
|
For anyone that has the same problem: You can't do this in the graph itself, you have to write your own LayoutAlgorithm (or extend an existing one) and pass it to the graph via setLayoutAlgorithm(...).
The algorithm and the code in the graph that worked for me is this (the delta is the x- and y-size by which all nodes have to be moved, that is the distance from the center of the graph area to the node that will be centered):
Graph:
final MyGraphLayoutAlgorithm myGraphLayoutAlgorithm = new MyGraphLayoutAlgorithm(
LayoutStyles.NO_LAYOUT_NODE_RESIZING);
setLayoutAlgorithm(myGraphLayoutAlgorithm, true);
addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Object object = getSelection().get(0);
// We can only center on nodes, not connections
if (object instanceof MyNode) {
MyGraphNode myGraphNode = (MyGraphNode) object;
Point point = myGraphNode.getLocation();
Dimension nodeSize = myGraphNode.getSize();
Point delta = new Point();
delta.setX((getSize().x / 2) - (point.x + nodeSize.width / 2));
delta.setY((getSize().y / 2) - (point.y + nodeSize.height / 2));
myGraphLayoutAlgorithm.setDelta(delta.getSWTPoint());
applyLayout();
}
}
});
LayoutAlgorithm:
public class MyGraphLayoutAlgorithm extends SpringLayoutAlgorithm {
/**
* The delta to relocate the nodes.
*/
private Point delta;
/**
* Call super constructor.
*
* @param style
* The style of the layout.
*/
public MyGraphLayoutAlgorithm(int style) {
super(LayoutStyles.NO_LAYOUT_NODE_RESIZING);
}
@Override
protected void preLayoutAlgorithm(InternalNode[] entitiesToLayout, InternalRelationship[] relationshipsToConsider,
double x, double y, double width, double height) {
super.preLayoutAlgorithm(entitiesToLayout, relationshipsToConsider, x, y, width, height);
}
@Override
protected void applyLayoutInternal(InternalNode[] entitiesToLayout, InternalRelationship[] relationshipsToConsider,
double x, double y, double width, double height) {
if (delta == null) {
// First time only
super.applyLayoutInternal(entitiesToLayout, relationshipsToConsider, x, y, width, height);
} else {
for (InternalNode node : entitiesToLayout) {
// node.setInternalLocation(point.x, point.y);
// node.setLocationInLayout(point.x, point.y);
node.setLocation(node.getInternalX() + delta.x, node.getInternalY() + delta.y);
}
}
}
/**
* Set the delta.
*
* @param delta
* The delta to be set.
*/
public void setDelta(Point delta) {
this.delta = delta;
}
}
[Updated on: Tue, 14 August 2012 16:19] Report message to a moderator
|
|
|
Powered by
FUDForum. Page generated in 0.03233 seconds