KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > views > EdgeRouter


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