1
14
15 package demo.layout.withoutview;
16
17 import y.base.Edge;
18 import y.base.Node;
19 import y.base.NodeMap;
20 import y.base.ListCell;
21 import y.layout.BufferedLayouter;
22 import y.layout.CopiedLayoutGraph;
23 import y.layout.DefaultLayoutGraph;
24 import y.layout.LayoutGraph;
25 import y.layout.hierarchic.IncrementalHierarchicLayouter;
26 import y.layout.hierarchic.incremental.PartitionGrid;
27 import y.layout.hierarchic.incremental.ColumnDescriptor;
28 import y.layout.hierarchic.incremental.RowDescriptor;
29 import y.util.D;
30 import y.util.DataProviders;
31
32 import java.awt.geom.Line2D;
33 import java.awt.Graphics2D;
34 import java.awt.Color;
35 import java.awt.Rectangle;
36 import java.awt.EventQueue;
37
38
51 public class PartitionGridLayoutWithoutAView
52 {
53
54 public static void main(String[] args) {
55 EventQueue.invokeLater(new Runnable() {
56 public void run() {
57 PartitionGridLayoutWithoutAView lwv = new PartitionGridLayoutWithoutAView();
58 lwv.doit();
59 }
60 });
61 }
62
63
66 public void doit()
67 {
68 DefaultLayoutGraph graph = new DefaultLayoutGraph();
69
70 Node v1 = graph.createNode();
72 graph.setSize(v1,30,30);
73 Node v2 = graph.createNode();
74 graph.setSize(v2,30,30);
75 Node v3 = graph.createNode();
76 graph.setSize(v3,30,30);
77 Node v4 = graph.createNode();
78 graph.setSize(v4,30,30);
79 Node v5 = graph.createNode();
80 graph.setSize(v5,30,30);
81
82 Edge e1 = graph.createEdge(v1,v2);
84 Edge e2 = graph.createEdge(v1,v3);
85 Edge e3 = graph.createEdge(v2,v4);
86 Edge e4 = graph.createEdge(v4,v5);
87
88 final PartitionGrid grid = new PartitionGrid(3, 2);
90 for (ListCell cell = grid.getRows().firstCell(); cell != null; cell = cell.succ()) {
91 final RowDescriptor descriptor = (RowDescriptor) cell.getInfo();
92 descriptor.setTopInset(5);
93 descriptor.setBottomInset(5);
94 }
95
96
97 NodeMap cellMap = graph.createNodeMap();
99
100 cellMap.set(v1, grid.createCellId(0, 0));
103 cellMap.set(v2, grid.createCellId(1, 1));
104 cellMap.set(v3, grid.createCellId(1, 0));
105 cellMap.set(v4, grid.createCellId(0, 1));
106 cellMap.set(v5, grid.createCellId(2, 1));
107
108 graph.addDataProvider(PartitionGrid.PARTITION_CELL_DPKEY, cellMap);
110 graph.addDataProvider(PartitionGrid.PARTITION_GRID_DPKEY, DataProviders.createConstantDataProvider(grid));
111
112 IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
114
115 new BufferedLayouter(layouter).doLayout(graph);
117
118 LayoutPreviewPanel lpp1 = new LayoutPreviewPanel(new CopiedLayoutGraph(graph)) {
120 Line2D.Double line = new Line2D.Double();
121 protected void paint( final Graphics2D g, final LayoutGraph graph ) {
122 final Color oldColor = g.getColor();
123 g.setColor(Color.white);
124
125 final Rectangle bbx = graph.getBoundingBox();
126 line.y1 = Math.floor(bbx.getY()) - 10;
127 line.y2 = Math.ceil(bbx.getMaxY()) + 10;
128 for (ListCell cell = grid.getColumns().firstCell(); cell != null; cell = cell.succ()) {
129 final ColumnDescriptor descriptor = (ColumnDescriptor) cell.getInfo();
130 line.x1 = descriptor.getComputedPosition();
131 line.x2 = line.x1;
132 g.draw(line);
133 }
134 {
135 final ColumnDescriptor descriptor = grid.getColumn(grid.getColumns().size() - 1);
136 line.x1 += descriptor.getComputedWidth();
137 line.x2 = line.x1;
138 g.draw(line);
139 }
140
141 line.x1 = Math.floor(bbx.getX()) - 10;
142 line.x2 = Math.ceil(bbx.getMaxX()) + 10;
143 for (ListCell cell = grid.getRows().firstCell(); cell != null; cell = cell.succ()) {
144 final RowDescriptor descriptor = (RowDescriptor) cell.getInfo();
145 line.y1 = descriptor.getComputedPosition();
146 line.y2 = line.y1;
147 g.draw(line);
148 }
149 {
150 final RowDescriptor descriptor = grid.getRow(grid.getRows().size() - 1);
151 line.y1 += descriptor.getComputedHeight();
152 line.y2 = line.y1;
153 g.draw(line);
154 }
155
156 g.setColor(oldColor);
157 }
158 };
159 lpp1.createFrame("Partition Grid").setVisible(true);
160
161 D.bug("\n\nGRAPH LAID OUT HIERARCHICALLY IN GRID");
162 D.bug("v1 center position = " + graph.getCenter(v1));
163 D.bug("v2 center position = " + graph.getCenter(v2));
164 D.bug("v3 center position = " + graph.getCenter(v3));
165 D.bug("v4 center position = " + graph.getCenter(v4));
166 D.bug("v5 center position = " + graph.getCenter(v5));
167 D.bug("e1 path = " + graph.getPath(e1));
168 D.bug("e2 path = " + graph.getPath(e2));
169 D.bug("e3 path = " + graph.getPath(e3));
170 D.bug("e4 path = " + graph.getPath(e4));
171 D.bug("Column 0 index = " + grid.getColumn(0).getIndex());
172 D.bug("Column 0 position = " + grid.getColumn(0).getComputedPosition());
173 D.bug("Column 0 width = " + grid.getColumn(0).getComputedWidth());
174 D.bug("Column 1 index = " + grid.getColumn(1).getIndex());
175 D.bug("Column 1 position = " + grid.getColumn(1).getComputedPosition());
176 D.bug("Column 1 width = " + grid.getColumn(1).getComputedWidth());
177 D.bug("Row 0 index = " + grid.getRow(0).getIndex());
178 D.bug("Row 0 position = " + grid.getRow(0).getComputedPosition());
179 D.bug("Row 0 height = " + grid.getRow(0).getComputedHeight());
180 D.bug("Row 1 index = " + grid.getRow(1).getIndex());
181 D.bug("Row 1 position = " + grid.getRow(1).getComputedPosition());
182 D.bug("Row 1 height = " + grid.getRow(1).getComputedHeight());
183 D.bug("Row 2 index = " + grid.getRow(2).getIndex());
184 D.bug("Row 2 position = " + grid.getRow(2).getComputedPosition());
185 D.bug("Row 2 height = " + grid.getRow(2).getComputedHeight());
186 }
187 }