01: package com.opensymphony.workflow.designer.views;
02:
03: import java.awt.Point;
04: import java.awt.geom.Point2D;
05: import java.awt.geom.Rectangle2D;
06:
07: import org.jgraph.graph.Edge;
08: import org.jgraph.graph.EdgeView;
09: import org.jgraph.graph.PortView;
10:
11: import com.opensymphony.workflow.designer.WorkflowPort;
12:
13: /**
14: * User: Hani Suleiman
15: * Date: Oct 16, 2003
16: * Time: 1:45:47 PM
17: */
18: public class EdgeRouter implements Edge.Routing {
19: public void route(EdgeView edge, java.util.List points) {
20: PortView sourceView = (PortView) edge.getSource();
21: PortView targetView = (PortView) edge.getTarget();
22: if (sourceView == null || targetView == null)
23: return;
24:
25: WorkflowPort sourcePort = (WorkflowPort) sourceView.getCell();
26: WorkflowPort targetPort = (WorkflowPort) targetView.getCell();
27: Point2D from = sourceView.getLocation(null);
28: Point2D to = targetView.getLocation(null);
29: //check if this is a dup route
30: // Collection duplicates = new HashSet();
31: // if(sourcePort!=targetPort)
32: // {
33: // Iterator iter = sourcePort.edges();
34: // while(iter.hasNext())
35: // {
36: // Edge e = (Edge)iter.next();
37: // if(e.getTarget()==sourcePort && e.getSource()==targetPort && e!=edge.getCell())
38: // {
39: // duplicates.add(e);
40: // System.out.println("detected duplicate line for edge " + e);
41: // }
42: // }
43: // }
44: if (from != null && to != null) {
45: Point[] routed;
46: // Handle self references
47: if (sourcePort == targetPort) {
48: Rectangle2D bounds = sourceView.getParentView()
49: .getBounds();
50: int size = 35;
51: routed = new Point[4];
52: routed[0] = new Point((int) (bounds.getX() + bounds
53: .getWidth()), (int) (bounds.getY() + bounds
54: .getHeight()));
55: routed[1] = new Point((int) (bounds.getX() + bounds
56: .getWidth()), (int) (bounds.getY()
57: + bounds.getHeight() + size));
58: routed[2] = new Point(
59: (int) (bounds.getX() + size + bounds.getWidth()),
60: (int) (bounds.getY() + bounds.getHeight() + size));
61: routed[3] = new Point(
62: (int) (bounds.getX() + size + bounds.getWidth()),
63: (int) (bounds.getY() + bounds.getHeight()));
64: } else {
65: double dx = Math.abs(from.getX() - to.getX());
66: double dy = Math.abs(from.getY() - to.getY());
67: double x2 = from.getX()
68: + ((to.getX() - from.getX()) / 2);
69: double y2 = from.getY()
70: + ((to.getY() - from.getY()) / 2);
71: routed = new Point[2];
72: if (dx > dy) {
73: routed[0] = new Point((int) x2, (int) from.getY());
74: routed[1] = new Point((int) x2, (int) to.getY());
75: } else {
76: routed[0] = new Point((int) from.getX(), (int) y2);
77: routed[1] = new Point((int) to.getX(), (int) y2);
78: }
79: }
80: // Set/Add Points
81: for (int i = 0; i < routed.length; i++)
82: if (points.size() > i + 2)
83: points.set(i + 1, routed[i]);
84: else
85: points.add(i + 1, routed[i]);
86: // Remove spare points
87: while (points.size() > routed.length + 2) {
88: points.remove(points.size() - 2);
89: }
90: }
91: }
92:
93: }
|