1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.9. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2011 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
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  /**
39   * This demo shows how to use the partition grid feature of IncrementalHierarchicLayouter
40   * without using classes that are only present in the yFiles Viewer Distribution.
41   * In this demo, nodes will be assigned to certain regions of the diagram,
42   * the so-called cells. The diagram will be arranged using hierarchical layout
43   * style, while nodes remain within the bounds of their cells.
44   * <br>
45   * This demo displays the calculated coordinates in a simple graph viewer.
46   * Additionally it outputs the calculated coordinates of the graph layout to
47   * the console.
48   *
49   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/incremental_hierarchical_layouter.html">Section Hierarchical Layout Style</a> in the yFiles for Java Developer's Guide
50   */
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    /**
64     * Creates a small graph and applies a swim lane layout to it.
65     */
66    public void doit()
67    {
68      DefaultLayoutGraph graph = new DefaultLayoutGraph();
69  
70      //construct graph. assign sizes to nodes
71      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      // create some edges...
83      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      // create a 3x2 grid
89      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      // create a map to store the node to lane mapping
98      NodeMap cellMap = graph.createNodeMap();
99  
100     // assign nodes to lanes
101     // lanes correspond to cells, as the grid has only one row
102     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     // register the information
109     graph.addDataProvider(PartitionGrid.PARTITION_CELL_DPKEY, cellMap);
110     graph.addDataProvider(PartitionGrid.PARTITION_GRID_DPKEY, DataProviders.createConstantDataProvider(grid));
111 
112     // create the layout algorithm
113     IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
114 
115     // start the layout
116     new BufferedLayouter(layouter).doLayout(graph);
117 
118     //display result
119     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 }