[ZEST] Center view on a selected node [message #894131] |
Fri, 06 July 2012 16:29  |
Eclipse User |
|
|
|
Hi,
I've got the following problem: when a node is clicked (selected), the whole view/graph/eclipse area should be centered on the node so that you would be able to click through a whole graph from beginning to end. I already tried Graph.setLocation(...) which works but doesn't change the canvas of the whole graph so that I have a graph with a white background which ends somewhere and out of that there's only gray. Node.setLocation(...) seems to do nothing at all.
Is there any possibility of achieving what I want? Do I maybe have to write my own layout?
I've appended my current code, if I left something unclear I'd be glad to provide further information.
Thanks in advance for your help.
Code:
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 DecisionModelGraphNode) {
DecisionModelGraphNode decisionModelGraphNode = (DecisionModelGraphNode) object;
Point point = decisionModelGraphNode.getLocation();
Dimension nodeSize = decisionModelGraphNode.getSize();
setLocation((getSize().x / 2) - (nodeSize.width / 2) - point.getSWTPoint().x, (getSize().y / 2)
- (nodeSize.height / 2) - point.getSWTPoint().y);
// TODO: Doesn't work as well -_- (a subsequent call to redraw() or layout()
// does nothing)
decisionModelGraphNode.setLocation(
(getSize().x / 2) - (nodeSize.width / 2) - point.getSWTPoint().x, (getSize().y / 2)
- (nodeSize.height / 2) - point.getSWTPoint().y);
}
}
});
|
|
|
Re: [ZEST] Center view on a selected node [message #901813 is a reply to message #894131] |
Tue, 14 August 2012 12:18  |
Eclipse User |
|
|
|
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 12:19] by Moderator
|
|
|
Powered by
FUDForum. Page generated in 0.04156 seconds