1
28 package demo.layout.withoutview;
29
30 import y.base.DataProvider;
31 import y.base.Node;
32 import y.base.NodeMap;
33 import y.layout.BufferedLayouter;
34 import y.layout.DefaultLayoutGraph;
35 import y.layout.LayoutGraph;
36 import y.layout.LayoutOrientation;
37 import y.layout.NodeLayout;
38 import y.layout.PortCalculator;
39 import y.layout.grouping.GroupingKeys;
40 import y.layout.hierarchic.IncrementalHierarchicLayouter;
41 import y.util.Maps;
42
43 import java.awt.Color;
44 import java.awt.EventQueue;
45 import java.awt.Graphics2D;
46 import java.awt.font.TextLayout;
47 import java.awt.geom.Ellipse2D;
48 import java.awt.geom.GeneralPath;
49 import java.awt.geom.Rectangle2D;
50
51
59 public class IntersectionCalculatorDemo {
60 public static void main( String[] args ) {
61 EventQueue.invokeLater(new Runnable() {
62 public void run() {
63 (new IntersectionCalculatorDemo()).doit();
64 }
65 });
66 }
67
68
72 private void doit() {
73 LayoutGraph graph = createSampleGraph();
74
75 layoutGraph(graph);
76
77 displayGraph(graph);
78 }
79
80
84 private void layoutGraph( final LayoutGraph graph ) {
85 final IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
89 layouter.setLayoutOrientation(LayoutOrientation.LEFT_TO_RIGHT);
90
91 layouter.prependStage(new PortCalculator());
97
98 (new BufferedLayouter(layouter)).doLayout(graph);
99 }
100
101
106 private void displayGraph( final LayoutGraph graph ) {
107 (new MyLayoutPreviewPanel(graph)).createFrame("").setVisible(true);
108 }
109
110
116 private LayoutGraph createSampleGraph() {
117 final DefaultLayoutGraph graph = new DefaultLayoutGraph();
118
119 Node v0 = graph.createNode();
125 graph.setSize(v0, 60, 30);
126 Node v1 = graph.createNode();
127 graph.setSize(v1, 30, 60);
128 Node v2 = graph.createNode();
129 graph.setSize(v2, 90, 90);
130 Node v3 = graph.createNode();
131 graph.setSize(v3, 30, 30);
132 Node v4 = graph.createNode();
133 graph.setSize(v4, 30, 30);
134 Node v5 = graph.createNode();
135 graph.setSize(v5, 60, 40);
136 Node v6 = graph.createNode();
137 graph.setSize(v6, 30, 30);
138
139 graph.createEdge(v0, v1);
141 graph.createEdge(v0, v2);
142 graph.createEdge(v1, v3);
143 graph.createEdge(v3, v4);
144 graph.createEdge(v1, v5);
145 graph.createEdge(v2, v5);
146 graph.createEdge(v6, v2);
147
148
149 final NodeMap shapes = Maps.createHashedNodeMap();
153 shapes.setInt(v0, IntersectionCalculators.SHAPE_DIAMOND);
154 shapes.setInt(v1, IntersectionCalculators.SHAPE_ELLIPSE);
155 shapes.setInt(v2, IntersectionCalculators.SHAPE_DIAMOND);
156 shapes.setInt(v5, IntersectionCalculators.SHAPE_ELLIPSE);
157 graph.addDataProvider(IntersectionCalculators.SHAPE_DPKEY, shapes);
158
159 IntersectionCalculators.addIntersectionCalculator(graph, shapes, true);
162 IntersectionCalculators.addIntersectionCalculator(graph, shapes, false);
165 return graph;
166 }
167
168
169
170
174 public static class MyLayoutPreviewPanel extends LayoutPreviewPanel {
175 final Ellipse2D.Double ellipse;
176 final GeneralPath diamond;
177
178 public MyLayoutPreviewPanel( final LayoutGraph graph ) {
179 super(graph);
180 ellipse = new Ellipse2D.Double();
181 diamond = new GeneralPath();
182 }
183
184 protected void paint(
185 final Graphics2D g,
186 final LayoutGraph graph,
187 final Node node
188 ) {
189 final NodeLayout nl = graph.getNodeLayout(node);
190 final DataProvider dp = graph.getDataProvider(GroupingKeys.GROUP_DPKEY);
191 if (dp != null && dp.getBool(node)) {
192 rectangle.setFrame(nl.getX(), nl.getY(), nl.getWidth(), nl.getHeight());
193 g.draw(rectangle);
194 } else {
195 switch (getShape(graph, node)) {
196 case IntersectionCalculators.SHAPE_ELLIPSE:
197 ellipse.setFrame(nl.getX(), nl.getY(), nl.getWidth(), nl.getHeight());
198 g.fill(ellipse);
199 break;
200 case IntersectionCalculators.SHAPE_DIAMOND:
201 setDiamondFrame(diamond, nl.getX(), nl.getY(), nl.getWidth(), nl.getHeight());
202 g.fill(diamond);
203 break;
204 default:
205 rectangle.setFrame(nl.getX(), nl.getY(), nl.getWidth(), nl.getHeight());
206 g.fill(rectangle);
207 break;
208 }
209 }
210
211 paintNodeLabel(g, graph, node, Integer.toString(node.index()));
212 }
213
214 protected void paintNodeLabel(
215 final Graphics2D g,
216 final LayoutGraph graph,
217 final Node node,
218 final String text
219 ) {
220 final Color oldColor = g.getColor();
221 g.setColor(Color.white);
222
223 final TextLayout tl = new TextLayout(text, g.getFont(), g.getFontRenderContext());
224 final Rectangle2D box = tl.getBounds();
225 final NodeLayout nl = graph.getNodeLayout(node);
226 final double tx = nl.getX() + (nl.getWidth() - box.getWidth())*0.5;
227 final double ty = nl.getY() + box.getHeight() + (nl.getHeight() - box.getHeight())*0.5;
228 tl.draw(g, (float) (tx), (float) (ty));
229
230 g.setColor(oldColor);
231 }
232
233 private static void setDiamondFrame(
234 final GeneralPath path,
235 final double x, final double y,
236 final double width, final double height
237 ) {
238 final double w2 = width * 0.5;
239 final double h2 = height * 0.5;
240 final double x2 = x + w2;
241 final double y2 = y + h2;
242
243 path.reset();
244 path.moveTo((float) x2, (float) y);
245 path.lineTo((float) (x + width), (float) y2);
246 path.lineTo((float) x2, (float) (y + height));
247 path.lineTo((float) x, (float) y2);
248 path.closePath();
249 }
250
251 private static int getShape( final LayoutGraph graph, final Node node ) {
252 final DataProvider dp = graph.getDataProvider(IntersectionCalculators.SHAPE_DPKEY);
253 if (dp != null) {
254 return dp.getInt(node);
255 }
256 return IntersectionCalculators.SHAPE_RECTANGLE;
257 }
258 }
259 }
260