package vg.userInterface.jgraphx;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.evelopers.unimod.glayout.fast.MultiLayerLayouter;
import com.evelopers.unimod.glayout.fast.extensions.VertexExt;
import com.evelopers.unimod.glayout.graph.SimpleEdge;
import com.evelopers.unimod.glayout.graph.SimpleGraph;
import com.evelopers.unimod.glayout.graph.SimpleVertex;
import com.mxgraph.layout.mxGraphLayout;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.util.mxPoint;
import com.mxgraph.view.mxGraph;
public class OrthogonalLayout extends mxGraphLayout {
private double separation = 40.0;
private double margin = 40.0;
public OrthogonalLayout(mxGraph graph) {
super(graph);
}
@Override
public void execute(Object parent) {
SimpleGraph sg = new SimpleGraph();
HashMap <mxCell, SimpleVertex> vertexes = new HashMap<mxCell, SimpleVertex>();
for (Object obj : graph.getChildVertices(parent)) {
if (obj instanceof mxCell) {
mxCell cell = (mxCell) obj;
SimpleVertex sv = new SimpleVertex();
int width = (int) Math.round(cell.getGeometry().getWidth());
int height = (int) Math.round(cell.getGeometry().getHeight());
VertexExt ve = new VertexExt(cell.getGeometry().getPoint(), width, height);
sv.setVertexext(ve);
sv.setProperty("id", cell.getId());
sg.addVertex(sv);
vertexes.put(cell, sv);
}
}
for (Object obj : graph.getChildEdges(parent)) {
if (obj instanceof mxCell) {
mxCell cell = (mxCell) obj;
SimpleEdge se = new SimpleEdge(vertexes.get(cell.getSource()), vertexes.get(cell.getTarget()));
se.setProperty("id", cell.getId());
sg.addEdge(se);
}
}
MultiLayerLayouter.layout(sg);
mxCell someCell = ((mxCell)vertexes.keySet().toArray()[0]);
double xscale = someCell.getGeometry().getWidth() / (vertexes.get(someCell).getVertexext().getWidth() - .5);
double yscale = someCell.getGeometry().getHeight() / (vertexes.get(someCell).getVertexext().getHeight() - .5);
for (Object obj : sg.getVertices()) {
if (obj instanceof SimpleVertex) {
SimpleVertex sv = (SimpleVertex) obj;
for (mxCell vertex: vertexes.keySet()) {
if (vertex.getId().equals(sv.getProperty("id"))) {
VertexExt ve = sv.getVertexext();
mxGeometry vertexGeometry = vertex.getGeometry();
super.setVertexLocation(vertex, ve.getLeftTop().getX()*xscale, ve.getLeftTop().getY()*yscale);
}
}
}
}
for (Object obj : sg.getEdges()) {
if (obj instanceof SimpleEdge) {
SimpleEdge se = (SimpleEdge) obj;
for (Object e : graph.getChildEdges(parent)) {
if (e instanceof mxCell) {
mxCell edge = (mxCell) e;
if (edge.getId().equals(se.getProperty("id"))) {
List<mxPoint> jgraphpoints = new ArrayList<mxPoint>();
jgraphpoints.add(new mxPoint(edge.getSource().getGeometry().getCenterX(),
edge.getTarget().getGeometry().getCenterY()));
super.setEdgePoints(edge, jgraphpoints);
super.setOrthogonalEdge(edge, true);
}
}
}
}
}
}
}