Skip to content

Instantly share code, notes, and snippets.

@satyagraha
Created November 7, 2014 23:00
Show Gist options
  • Save satyagraha/9904fdbba5b0547b756b to your computer and use it in GitHub Desktop.
Save satyagraha/9904fdbba5b0547b756b to your computer and use it in GitHub Desktop.
AllDirectionsHierarchicalLayout
package org.mydemo.jgraphx;
import java.util.Collection;
import java.util.List;
import javax.swing.SwingConstants;
import com.mxgraph.layout.hierarchical.mxHierarchicalLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.model.mxGraphModel;
import com.mxgraph.view.mxGraph;
public class AllDirectionsHierarchicalLayout extends mxHierarchicalLayout {
public AllDirectionsHierarchicalLayout(mxGraph aGraph, int aOrientation) {
super(aGraph, aOrientation);
}
public AllDirectionsHierarchicalLayout(mxGraph aGraph) {
super(aGraph);
}
@Override
public void execute(Object aParent) {
graph.getModel().beginUpdate();
super.execute(aParent);
fixLayout();
graph.getModel().endUpdate();
}
@Override
public void execute(Object aParent, List<Object> aRoots) {
graph.getModel().beginUpdate();
super.execute(aParent, aRoots);
fixLayout();
graph.getModel().endUpdate();
}
private void fixLayout() {
// Don't need fix for these cases
if ((orientation == SwingConstants.NORTH) || (orientation == SwingConstants.WEST))
return;
if (graph.isAllowNegativeCoordinates())
return;
graph.getModel().beginUpdate();
Collection<Object> theCells = ((mxGraphModel) graph.getModel()).getCells().values();
double xMin = 0;
double yMin = 0;
for (Object theCellObj : theCells) {
mxCell theCell = (mxCell) theCellObj;
if (theCell.getGeometry() == null)
continue;
xMin = Math.min(xMin, theCell.getGeometry().getX());
yMin = Math.min(yMin, theCell.getGeometry().getY());
}
for (Object theCellObj : theCells) {
mxCell theCell = (mxCell) theCellObj;
mxGeometry geometry = theCell.getGeometry();
// Not affected by layout so don't need to translate
if (geometry == null || geometry.getX() > 0 && geometry.getY() > 0)
continue;
graph.translateCell(theCell, -xMin, -yMin);
}
graph.getModel().endUpdate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment