* Edge for which routing is demanding
*/
public List routeEdge(GraphLayoutCache cache, EdgeView edge) {
List newPoints = new ArrayList();
CellView nodeFrom = edge.getSource();
CellView nodeTo = edge.getTarget();
// Check presence of source/target nodes
if (null == nodeFrom) {
nodeFrom = edge.getSourceParentView();
}
if (null == nodeTo) {
nodeTo = edge.getTargetParentView();
}
if ((null == nodeFrom) || (null == nodeTo)) {
// System.out.println("EdgeView has no source or target view : "
// + edge.toString());
return null;
}
if (nodeFrom == nodeTo) {
// System.out.println("nodeFrom and NodeTo are the same cell view");
return null;
}
List points = edge.getPoints();
Object startPort = points.get(0);
Object endPort = points.get(points.size() - 1);
newPoints.add(startPort);
// Promote edges up to first visible connected parents
// if (graph == null) {
// System.out
// .println("graph variable not correctly set, must be set to obtain parallel routing");
// }
// Check presence of parallel edges
Object[] edges = getParallelEdges(cache, edge, nodeFrom, nodeTo);
if (edges == null) {
return null;
}
// For one edge, no intermediate point
if (edges.length >= 2) {
// System.out.println("EdgeView indicates " + edges.length
// + " parallel edges");
// Looking for position of edge
int position = 0;
// System.out.println();
// System.out.println("edges.length = " + edges.length);
for (int i = 0; i < edges.length; i++) {
// System.out
// .println("edge value = "
// + String.valueOf(((DefaultGraphCell) edges[i])
// .getUserObject()));
// System.out
// .println("compared edge value = "
// + String.valueOf(((DefaultGraphCell) edge.getCell())
// .getUserObject()));
Object e = edges[i];
if (e == edge.getCell()) {
position = i + 1;
}
}
// System.out.println("position = " + position);
// Looking for position of source/target nodes (edge=>port=>vertex)
Point2D from;
Point2D perimeterPoint = edge.getTarget() != null ? edge
.getPoint(edge.getPointCount() - 1) : AbstractCellView
.getCenterPoint(nodeTo);
if (perimeterPoint == null) {
perimeterPoint = AbstractCellView.getCenterPoint(nodeTo);
}
if (edge.getSource() == null || edge.getSource().getParentView() == null) {
// System.out.println(edge+"-source promoted");
from = nodeFrom.getPerimeterPoint(edge,
AbstractCellView.getCenterPoint(nodeFrom),
perimeterPoint);
} else {
from = edge.getSource().getParentView().getPerimeterPoint(edge,
AbstractCellView.getCenterPoint(edge.getSource().getParentView()),
(edge.getTarget() != null && edge.getTarget().getParentView() != null) ?
AbstractCellView.getCenterPoint(edge.getTarget().getParentView()) :
AbstractCellView.getCenterPoint(nodeTo));
}
Point2D to;
if (edge.getTarget() == null || edge.getTarget().getParentView() == null) { // INV: nodeTo != null
// System.out.println(edge+"-target promoted");
to = nodeTo.getPerimeterPoint(edge, AbstractCellView.getCenterPoint(nodeTo), from);
} else {
to = edge.getTarget().getParentView().getPerimeterPoint
(edge, AbstractCellView.getCenterPoint(edge.getTarget().getParentView()), from);
}
// System.out.println("from Point = " + String.valueOf(from));
// System.out.println("to Point = " + String.valueOf(to));
if (from != null && to != null) {
double dy = from.getY() - to.getY();
double dx = from.getX() - to.getX();
if (dy == 0 && dx == 0) {
return null;
}
double theta = 0;
if (dy == 0) {
theta = Math.PI / 2.0;
} else if (dx == 0) {
theta = 0;
} else {
double m = dy / dx;
theta = Math.atan(-1 / m);
}
// Calc of radius
double length = Math.sqrt(dx * dx + dy * dy);
// System.out.println("length = " + length);
double rx = dx / length;
double ry = dy / length;
// Memorize size of source/target nodes
double sizeFrom = Math.max(nodeFrom.getBounds().getWidth(),
nodeFrom.getBounds().getHeight()) / 2.;
double sizeTo = Math.max(nodeTo.getBounds().getWidth(), nodeTo
.getBounds().getHeight()) / 2.;
// Calc position of central point
double edgeMiddleDeparture = (Math.sqrt(dx * dx + dy * dy)
- sizeFrom - sizeTo)