The problem is that the layered-digraph autolayout is operating on the centers of the nodes, not the centers of the icons, as you would prefer for your particular application.
The following code modifies Flower.java, of the Flower example.
It customizes the JGoNetworkNode’s idea of what the center of the node is.
[code] static class CustomNetwork extends JGoNetwork {
public CustomNetwork(JGoObjectSimpleCollection coll) {
super(coll);
}
public JGoNetworkNode createNetworkNode() {
return new CustomNetworkNode();
}
}
static class CustomNetworkNode extends JGoNetworkNode {
public void setJGoObject(JGoObject obj) {
super.setJGoObject(obj);
if (obj instanceof SimpleNode) {
SimpleNode sn = (SimpleNode)obj;
Point center = sn.getIcon().getSpotLocation(JGoObject.Center);
setCenter(center);
}
}
public void commitPosition() {
SimpleNode sn = (SimpleNode)getJGoObject();
int x = getCenter().x; // - sn.getWidth()/2; // + sn.getIcon().getWidth()/2;
int y = getCenter().y; // - sn.getHeight()/2; // + sn.getIcon().getHeight()/2;
sn.getIcon().setSpotLocation(JGoObject.Center, x, y);
}
}
void layerAction() {
ProcessDocument doc = getCurrentView().getDoc();
doc.startTransaction();
JGoLayeredDigraphAutoLayout l = new JGoLayeredDigraphAutoLayout(doc, new CustomNetwork(doc),
30, 30, JGoLayeredDigraphAutoLayout.LD_DIRECTION_RIGHT,
JGoLayeredDigraphAutoLayout.LD_CYCLEREMOVE_DFS,
JGoLayeredDigraphAutoLayout.LD_LAYERING_OPTIMALLINKLENGTH,
JGoLayeredDigraphAutoLayout.LD_INITIALIZE_DFSOUT, 4,
JGoLayeredDigraphAutoLayout.LD_AGGRESSIVE_FALSE);
l.performLayout();
// minimize the size of the document
Rectangle b = doc.computeBounds();
doc.setDocumentTopLeft(b.getLocation());
doc.setDocumentSize(b.getSize());
// scroll the view to show the top-left corner of the laid-out diagram
getCurrentView().setIncludingNegativeCoords(true); // could do this when initializing the JGoView
getCurrentView().setViewPosition(doc.getDocumentTopLeft());
// or show a particular area of the diagram by calling JGoView.scrollRectToVisible
doc.endTransaction("LayeredDigraph Layout");
}[/code]
So I believe this handles the straight-line cases, like your #1. But it won’t help when there are multiple paths, each with different height nodes.
For #2, I don’t know if that can be improved as long as the node have different heights and you still want to vertically center the icons rather than the whole nodes.
For #3, the above code sizes the document and scrolls the view to show the top left corner of the document.
For #4, no there isn’t such a property. I suggest doing the normal layout and then moving subsections of the graph programmatically.
For 1, I use JGoNode and not JGoNetworkNode. Is there a way to center a JGoNode?
As you can see from the documentation, JGoNetworkNode does not extend JGoNode. JGoNetworkNode is just a data structure used by JGoLayout and JGoNetwork to represent JGoNodes internally before and during the layout. JGoLayout operates on a JGoNetwork, not directly on a JGoDocument.