1
28 package demo.layout.withoutview;
29
30 import java.util.*;
31
32 import y.base.Node;
33 import y.base.Edge;
34 import y.base.EdgeCursor;
35 import y.base.EdgeList;
36
37 import y.layout.EdgeLayout;
38 import y.layout.LayoutGraph;
39 import y.layout.CanonicMultiStageLayouter;
40
41 import y.geom.YPoint;
42
43
57 public class DiagonalLayouter extends CanonicMultiStageLayouter
58 {
59 double minimalNodeDistance = 40;
60
61
64 public DiagonalLayouter()
65 {
66 setParallelEdgeLayouterEnabled(false);
68 }
69
70
73 public void setMinimalNodeDistance(double d)
74 {
75 minimalNodeDistance = d;
76 }
77
78
81 public double getMinimalNodeDistance()
82 {
83 return minimalNodeDistance;
84 }
85
86
90 protected boolean canLayoutCore(LayoutGraph graph)
91 {
92 return true;
93 }
94
95
98 protected void doLayoutCore(LayoutGraph graph)
99 {
100
101 Node[] nodes = graph.getNodeArray();
103 double offset = 0.0;
104 for(int i = 0; i < nodes.length; i++)
105 {
106 Node v = nodes[i];
107 graph.setLocation(v,offset,offset);
108 offset += minimalNodeDistance + Math.max(graph.getWidth(v),graph.getHeight(v));
109 }
110
111 Comparator outComp = new Comparator() {
114 public int compare(Object a, Object b) {
115 Node va = ((Edge)a).target();
116 Node vb = ((Edge)b).target();
117 if(va != vb)
118 return va.index() - vb.index();
119 else
120 return ((Edge)a).index() - ((Edge)b).index();
121 }
122 };
123
124 Comparator inComp = new Comparator() {
127 public int compare(Object a, Object b) {
128 Node va = ((Edge)a).source();
129 Node vb = ((Edge)b).source();
130 if(va != vb)
131 return va.index() - vb.index();
132 else
133 return ((Edge)b).index() - ((Edge)a).index();
134 }
135 };
136
137 for(EdgeCursor ec = graph.edges(); ec.ok(); ec.next())
139 {
140 EdgeLayout el = graph.getLayout(ec.edge());
141 el.clearPoints();
142 el.addPoint(0,0);
143 }
144
145 for(int i = 0; i < nodes.length; i++)
147 {
148 Node v = nodes[i];
149
150
151 EdgeList rightSide = new EdgeList();
152 EdgeList leftSide = new EdgeList();
153
154 v.sortOutEdges(outComp);
156 for(EdgeCursor ec = v.outEdges(); ec.ok(); ec.next())
157 {
158 Edge e = ec.edge();
159 Node w = e.target();
160
161 if(w.index() < v.index())
162 rightSide.addLast(e);
163 else
164 leftSide.addLast(e);
165 }
166
167 if(!rightSide.isEmpty())
168 {
169 double space = graph.getWidth(v)/rightSide.size();
170 double xcoord = graph.getX(v) + graph.getWidth(v) - space/2.0;
171 for(EdgeCursor ec = rightSide.edges(); ec.ok(); ec.next())
172 {
173 Edge e = ec.edge();
174 EdgeLayout el = graph.getLayout(e);
175 YPoint p = el.getPoint(0);
176 el.setPoint(0, xcoord, p.getY());
177 graph.setSourcePointAbs(e, new YPoint(xcoord, graph.getCenterY(v)));
178 xcoord -= space;
179 }
180 }
181
182 if(!leftSide.isEmpty())
183 {
184 double space = graph.getWidth(v)/leftSide.size();
185 double xcoord = graph.getX(v) + graph.getWidth(v) - space/2.0;
186 for(EdgeCursor ec = leftSide.edges(); ec.ok(); ec.next())
187 {
188 Edge e = ec.edge();
189 EdgeLayout el = graph.getLayout(e);
190 YPoint p = el.getPoint(0);
191 el.setPoint(0, xcoord, p.getY());
192 graph.setSourcePointAbs(e, new YPoint(xcoord,graph.getCenterY(v)));
193 xcoord -= space;
194 }
195 }
196
197 rightSide.clear();
199 leftSide.clear();
200 v.sortInEdges(inComp);
201 for(EdgeCursor ec = v.inEdges(); ec.ok(); ec.next())
202 {
203 Edge e = ec.edge();
204 Node w = e.source();
205
206 if(w.index() < v.index())
207 leftSide.addLast(e);
208 else
209 rightSide.addLast(e);
210 }
211
212 if(!rightSide.isEmpty())
213 {
214 double space = graph.getHeight(v)/rightSide.size();
215 double ycoord = graph.getY(v) + graph.getHeight(v) - space/2.0;
216 for(EdgeCursor ec = rightSide.edges(); ec.ok(); ec.next())
217 {
218 Edge e = ec.edge();
219 EdgeLayout el = graph.getLayout(e);
220 YPoint p = el.getPoint(0);
221 el.setPoint(0, p.getX(), ycoord);
222 graph.setTargetPointAbs(e, new YPoint(graph.getCenterX(v), ycoord));
223 ycoord -= space;
224 }
225 }
226
227 if(!leftSide.isEmpty())
228 {
229 double space = graph.getHeight(v)/leftSide.size();
230 double ycoord = graph.getY(v) + graph.getHeight(v) - space/2.0;
231 for(EdgeCursor ec = leftSide.edges(); ec.ok(); ec.next())
232 {
233 Edge e = ec.edge();
234 EdgeLayout el = graph.getLayout(e);
235 YPoint p = el.getPoint(0);
236 el.setPoint(0, p.getX(), ycoord);
237 graph.setTargetPointAbs(e, new YPoint(graph.getCenterX(v), ycoord));
238 ycoord -= space;
239 }
240 }
241 }
242 }
243 }
244