1
28
29 package demo.layout.withoutview;
30
31 import y.base.Edge;
32 import y.base.Node;
33 import y.base.NodeMap;
34 import y.base.ListCell;
35 import y.layout.BufferedLayouter;
36 import y.layout.CopiedLayoutGraph;
37 import y.layout.DefaultLayoutGraph;
38 import y.layout.LayoutGraph;
39 import y.layout.grid.PartitionGrid;
40 import y.layout.grid.RowDescriptor;
41 import y.layout.hierarchic.IncrementalHierarchicLayouter;
42 import y.layout.grid.ColumnDescriptor;
43 import y.util.D;
44 import y.util.DataProviders;
45
46 import java.awt.geom.Line2D;
47 import java.awt.Graphics2D;
48 import java.awt.Color;
49 import java.awt.Rectangle;
50 import java.awt.EventQueue;
51
52
65 public class PartitionGridLayoutWithoutAView
66 {
67
68 public static void main(String[] args) {
69 EventQueue.invokeLater(new Runnable() {
70 public void run() {
71 PartitionGridLayoutWithoutAView lwv = new PartitionGridLayoutWithoutAView();
72 lwv.doit();
73 }
74 });
75 }
76
77
80 public void doit()
81 {
82 DefaultLayoutGraph graph = new DefaultLayoutGraph();
83
84 Node v1 = graph.createNode();
86 graph.setSize(v1,30,30);
87 Node v2 = graph.createNode();
88 graph.setSize(v2,30,30);
89 Node v3 = graph.createNode();
90 graph.setSize(v3,30,30);
91 Node v4 = graph.createNode();
92 graph.setSize(v4,30,30);
93 Node v5 = graph.createNode();
94 graph.setSize(v5,30,30);
95 Node v6 = graph.createNode();
96 graph.setSize(v6,30,30);
97
98 Edge e1 = graph.createEdge(v1,v2);
100 Edge e2 = graph.createEdge(v1,v3);
101 Edge e3 = graph.createEdge(v2,v4);
102 Edge e4 = graph.createEdge(v4,v5);
103 Edge e5 = graph.createEdge(v3,v6);
104
105 final PartitionGrid grid = new PartitionGrid(3, 2);
107 for (ListCell cell = grid.getRows().firstCell(); cell != null; cell = cell.succ()) {
108 final RowDescriptor descriptor = (RowDescriptor) cell.getInfo();
109 descriptor.setTopInset(5);
110 descriptor.setBottomInset(5);
111 }
112
113
114 NodeMap cellMap = graph.createNodeMap();
116
117 cellMap.set(v1, grid.createCellId(0, 0));
120 cellMap.set(v2, grid.createCellId(1, 1));
121 cellMap.set(v3, grid.createCellId(2, 0));
122 cellMap.set(v4, grid.createCellId(0, 1));
123 cellMap.set(v5, grid.createCellId(2, 1));
124
125 graph.addDataProvider(PartitionGrid.PARTITION_CELL_DPKEY, cellMap);
127 graph.addDataProvider(PartitionGrid.PARTITION_GRID_DPKEY, DataProviders.createConstantDataProvider(grid));
128
129 IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
131
132 new BufferedLayouter(layouter).doLayout(graph);
134
135 LayoutPreviewPanel lpp1 = new LayoutPreviewPanel(new CopiedLayoutGraph(graph)) {
137 Line2D.Double line = new Line2D.Double();
138 protected void paint( final Graphics2D g, final LayoutGraph graph ) {
139 final Color oldColor = g.getColor();
140 g.setColor(Color.white);
141
142 final Rectangle bbx = graph.getBoundingBox();
143 line.y1 = Math.floor(bbx.getY()) - 10;
144 line.y2 = Math.ceil(bbx.getMaxY()) + 10;
145 for (ListCell cell = grid.getColumns().firstCell(); cell != null; cell = cell.succ()) {
146 final ColumnDescriptor descriptor = (ColumnDescriptor) cell.getInfo();
147 line.x1 = descriptor.getComputedPosition();
148 line.x2 = line.x1;
149 g.draw(line);
150 }
151 {
152 final ColumnDescriptor descriptor = grid.getColumn(grid.getColumns().size() - 1);
153 line.x1 += descriptor.getComputedWidth();
154 line.x2 = line.x1;
155 g.draw(line);
156 }
157
158 line.x1 = Math.floor(bbx.getX()) - 10;
159 line.x2 = Math.ceil(bbx.getMaxX()) + 10;
160 for (ListCell cell = grid.getRows().firstCell(); cell != null; cell = cell.succ()) {
161 final RowDescriptor descriptor = (RowDescriptor) cell.getInfo();
162 line.y1 = descriptor.getComputedPosition();
163 line.y2 = line.y1;
164 g.draw(line);
165 }
166 {
167 final RowDescriptor descriptor = grid.getRow(grid.getRows().size() - 1);
168 line.y1 += descriptor.getComputedHeight();
169 line.y2 = line.y1;
170 g.draw(line);
171 }
172
173 g.setColor(oldColor);
174 }
175 };
176 lpp1.createFrame("Partition Grid").setVisible(true);
177
178 D.bug("\n\nGRAPH LAID OUT HIERARCHICALLY IN GRID");
179 D.bug("v1 center position = " + graph.getCenter(v1));
180 D.bug("v2 center position = " + graph.getCenter(v2));
181 D.bug("v3 center position = " + graph.getCenter(v3));
182 D.bug("v4 center position = " + graph.getCenter(v4));
183 D.bug("v5 center position = " + graph.getCenter(v5));
184 D.bug("e1 path = " + graph.getPath(e1));
185 D.bug("e2 path = " + graph.getPath(e2));
186 D.bug("e3 path = " + graph.getPath(e3));
187 D.bug("e4 path = " + graph.getPath(e4));
188 D.bug("Column 0 index = " + grid.getColumn(0).getIndex());
189 D.bug("Column 0 position = " + grid.getColumn(0).getComputedPosition());
190 D.bug("Column 0 width = " + grid.getColumn(0).getComputedWidth());
191 D.bug("Column 1 index = " + grid.getColumn(1).getIndex());
192 D.bug("Column 1 position = " + grid.getColumn(1).getComputedPosition());
193 D.bug("Column 1 width = " + grid.getColumn(1).getComputedWidth());
194 D.bug("Row 0 index = " + grid.getRow(0).getIndex());
195 D.bug("Row 0 position = " + grid.getRow(0).getComputedPosition());
196 D.bug("Row 0 height = " + grid.getRow(0).getComputedHeight());
197 D.bug("Row 1 index = " + grid.getRow(1).getIndex());
198 D.bug("Row 1 position = " + grid.getRow(1).getComputedPosition());
199 D.bug("Row 1 height = " + grid.getRow(1).getComputedHeight());
200 D.bug("Row 2 index = " + grid.getRow(2).getIndex());
201 D.bug("Row 2 position = " + grid.getRow(2).getComputedPosition());
202 D.bug("Row 2 height = " + grid.getRow(2).getComputedHeight());
203 }
204 }
205