1   /****************************************************************************
2    * This demo file is part of yFiles for Java 2.14.
3    * Copyright (c) 2000-2017 by yWorks GmbH, Vor dem Kreuzberg 28,
4    * 72070 Tuebingen, Germany. All rights reserved.
5    * 
6    * yFiles demo files exhibit yFiles for Java functionalities. Any redistribution
7    * of demo files in source code or binary form, with or without
8    * modification, is not permitted.
9    * 
10   * Owners of a valid software license for a yFiles for Java version that this
11   * demo is shipped with are allowed to use the demo source code as basis
12   * for their own yFiles for Java powered applications. Use of such programs is
13   * governed by the rights and conditions as set out in the yFiles for Java
14   * license agreement.
15   * 
16   * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY EXPRESS OR IMPLIED
17   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19   * NO EVENT SHALL yWorks BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21   * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   *
27   ***************************************************************************/
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  /**
53   * This demo shows how to use the partition grid feature of IncrementalHierarchicLayouter
54   * without using classes that are only present in the yFiles Viewer Distribution.
55   * In this demo, nodes will be assigned to certain regions of the diagram,
56   * the so-called cells. The diagram will be arranged using hierarchical layout
57   * style, while nodes remain within the bounds of their cells.
58   * <br>
59   * This demo displays the calculated coordinates in a simple graph viewer.
60   * Additionally it outputs the calculated coordinates of the graph layout to
61   * the console.
62   *
63   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/incremental_hierarchical_layouter" target="_blank">Section Hierarchical Layout Style</a> in the yFiles for Java Developer's Guide
64   */
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    /**
78     * Creates a small graph and applies a swim lane layout to it.
79     */
80    public void doit()
81    {
82      DefaultLayoutGraph graph = new DefaultLayoutGraph();
83  
84      //construct graph. assign sizes to nodes
85      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      // create some edges...
99      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     // create a 3x2 grid
106     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     // create a map to store the node to lane mapping
115     NodeMap cellMap = graph.createNodeMap();
116 
117     // assign nodes to lanes
118     // lanes correspond to cells, as the grid has only one row
119     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     // register the information
126     graph.addDataProvider(PartitionGrid.PARTITION_CELL_DPKEY, cellMap);
127     graph.addDataProvider(PartitionGrid.PARTITION_GRID_DPKEY, DataProviders.createConstantDataProvider(grid));
128 
129     // create the layout algorithm
130     IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
131 
132     // start the layout
133     new BufferedLayouter(layouter).doLayout(graph);
134 
135     //display result
136     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