1
14
15 package demo.layout.withoutview;
16
17 import javax.swing.JFrame;
18
19 import y.geom.YRectangle;
20 import y.geom.YPoint;
21 import y.layout.*;
22 import y.layout.DefaultLayoutGraph;
23 import y.layout.EdgeLabelModel;
24 import y.layout.LabelLayoutTranslator;
25 import y.layout.LayoutGraph;
26 import y.layout.EdgeLabelLayout;
27 import y.layout.FreeEdgeLabelModel;
28 import y.layout.BufferedLayouter;
29 import y.layout.SliderEdgeLabelModel;
30 import y.layout.EdgeLabelLayoutImpl;
31 import y.layout.hierarchic.IncrementalHierarchicLayouter;
32 import y.layout.labeling.GreedyMISLabeling;
33 import y.util.D;
34 import y.base.Edge;
35 import y.base.EdgeMap;
36 import y.base.Node;
37 import y.layout.PortConstraint;
38 import y.layout.PortConstraintKeys;
39
40 import java.awt.EventQueue;
41
42
60 public class LayoutWithoutAView
61 {
62
63
66 public static void main(String[] args) {
67 EventQueue.invokeLater(new Runnable() {
68 public void run() {
69 LayoutWithoutAView lwv = new LayoutWithoutAView();
70 lwv.doit();
71 }
72 });
73 }
74
75
83 public void doit()
84 {
85 DefaultLayoutGraph graph = new DefaultLayoutGraph();
86
87 Node v1 = graph.createNode();
89 graph.setSize(v1,30,30);
90 Node v2 = graph.createNode();
91 graph.setSize(v2,30,30);
92 Node v3 = graph.createNode();
93 graph.setSize(v3,30,30);
94
95 Edge e1 = graph.createEdge(v1,v2);
96 Edge e2 = graph.createEdge(v2,v3);
97 Edge e3 = graph.createEdge(v1,v3);
98
99 EdgeMap spc = graph.createEdgeMap();
101 EdgeMap tpc = graph.createEdgeMap();
102 spc.set(e1, PortConstraint.create(PortConstraint.EAST));
104 tpc.set(e1, PortConstraint.create(PortConstraint.EAST, true));
106 graph.setTargetPointRel(e1, new YPoint(15, -15));
110
111 spc.set(e2, PortConstraint.create(PortConstraint.NORTH));
113 tpc.set(e2, PortConstraint.create(PortConstraint.NORTH));
114 graph.addDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY, spc);
116 graph.addDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY, tpc);
117
118
119 EdgeLabelLayoutImpl ell1 = new EdgeLabelLayoutImpl();
124 ell1.setBox(new YRectangle(0,0,80,20));
125 ell1.setEdgeLabelModel(new SliderEdgeLabelModel(SliderEdgeLabelModel.CENTER_SLIDER));
128
129 EdgeLabelLayoutImpl ell2 = new EdgeLabelLayoutImpl();
130 ell2.setBox(new YRectangle(0,0,80,20));
131 ell2.setEdgeLabelModel(new SliderEdgeLabelModel(SliderEdgeLabelModel.SIDE_SLIDER));
134
135 EdgeLabelLayout[] ells = new EdgeLabelLayout[]{ell1, ell2};
136 graph.setLabelLayout(e1, ells);
137
138 IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
139 layouter.setLabelLayouterEnabled(true);
140 layouter.setLabelLayouter(new GreedyMISLabeling());
141
142 new BufferedLayouter(layouter).doLayout(graph);
143
144 LayoutPreviewPanel lpp1 = new LayoutPreviewPanel(new CopiedLayoutGraph(graph));
146 lpp1.createFrame("Hierarchical with general edge labeling").setVisible(true);
147
148 D.bug("\n\nGRAPH LAID OUT USING GENERAL EDGE LABELING");
149 D.bug("v1 center position = " + graph.getCenter(v1));
150 D.bug("v2 center position = " + graph.getCenter(v2));
151 D.bug("v3 center position = " + graph.getCenter(v3));
152 D.bug("e1 path = " + graph.getPath(e1));
153 D.bug("e2 path = " + graph.getPath(e2));
154 D.bug("e3 path = " + graph.getPath(e3));
155 D.bug("ell1 upper left location = " + getEdgeLabelLocation(graph,e1,ell1));
156 D.bug("ell2 upper left location = " + getEdgeLabelLocation(graph,e1,ell2));
157
158 EdgeLabelModel freeModel = new FreeEdgeLabelModel();
159 ell1.setEdgeLabelModel(freeModel);
160 ell1.setModelParameter(freeModel.getDefaultParameter());
161 ell2.setEdgeLabelModel(freeModel);
162 ell2.setModelParameter(freeModel.getDefaultParameter());
163
164 layouter = new IncrementalHierarchicLayouter();
165 layouter.setIntegratedEdgeLabelingEnabled(true);
166
167 new BufferedLayouter(layouter).doLayout(graph);
168
169 LayoutPreviewPanel lpp2 = new LayoutPreviewPanel(graph);
171 lpp2.createFrame("Hierarchical with internal labeling").setVisible(true);
172
173 D.bug("\n\nGRAPH LAID OUT USING HIERARCHIC LAYOUTER WITH INTERNAL EDGE LABELING");
174 D.bug("v1 center position = " + graph.getCenter(v1));
175 D.bug("v2 center position = " + graph.getCenter(v2));
176 D.bug("v3 center position = " + graph.getCenter(v3));
177 D.bug("e1 path = " + graph.getPath(e1));
178 D.bug("e2 path = " + graph.getPath(e2));
179 D.bug("e3 path = " + graph.getPath(e3));
180 D.bug("ell1 upper left location = " + getEdgeLabelLocation(graph,e1,ell1));
181 D.bug("ell2 upper left location = " + getEdgeLabelLocation(graph,e1,ell2));
182 }
183
184
190 YPoint getEdgeLabelLocation(LayoutGraph graph, Edge e, EdgeLabelLayout ell)
191 {
192 YPoint ellp = ell.getLabelModel().getLabelPlacement(
193 ell.getBox(),
194 graph.getEdgeLayout(e),
195 graph.getNodeLayout(e.source()),
196 graph.getNodeLayout(e.target()),
197 ell.getModelParameter());
198 return ellp;
199 }
200 }
201