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.EdgeMap;
33  import y.base.Node;
34  import y.base.NodeMap;
35  
36  import y.layout.BufferedLayouter;
37  import y.layout.DefaultLayoutGraph;
38  import y.layout.PortConstraintKeys;
39  import y.layout.grouping.Grouping;
40  import y.layout.hierarchic.HierarchicGroupLayouter;
41  
42  import y.util.D;
43  
44  import java.awt.EventQueue;
45  
46  /**
47   * This class shows how to use layout and grouping algorithms without using classes 
48   * that are only present in the yFiles Viewer Distribution. Therefore this demo
49   * only outputs the calculated coordinates of the graph layout to the console
50   * and displays it inside a simple preview panel.
51   * <br>
52   * In this demo HierarchicGroupLayouter is used to layout a small graph. 
53   *
54   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/layout_advanced_features" target="_blank">Section Advanced Layout Concepts</a> in the yFiles for Java Developer's Guide
55   */
56  public class GroupingLayoutWithoutAView
57  {
58    
59    /**
60     * Launcher
61     */
62    public static void main(String[] args) {
63      EventQueue.invokeLater(new Runnable() {
64        public void run() {
65          GroupingLayoutWithoutAView lwv = new GroupingLayoutWithoutAView();
66          lwv.doit();
67        }
68      });
69    }
70  
71    /**
72     * Creates a small graph and applies a hierarchic group layout to it.
73     * <p>
74     * The output of the calculated coordinates will be displayed in the
75     * console.
76     */ 
77    public void doit()
78    {
79      DefaultLayoutGraph graph = new DefaultLayoutGraph();
80      
81      //construct graph. assign sizes to nodes
82      Node v1 = graph.createNode();
83      graph.setSize(v1,30,30);
84      Node v2 = graph.createNode();
85      graph.setSize(v2,30,30);
86      Node v3 = graph.createNode();
87      graph.setSize(v3,30,30);
88      Node v4 = graph.createNode();
89      graph.setSize(v4,30,30);
90      
91      Node groupNode = graph.createNode();
92      graph.setSize(groupNode, 100,100);
93      
94      Edge e1 = graph.createEdge(v1,v2);
95      Edge e2 = graph.createEdge(v4, groupNode);
96      Edge e3 = graph.createEdge(v1,v3);
97      Edge e4 = graph.createEdge(v1, v1);
98      Edge e5 = graph.createEdge(v2, groupNode);
99      Edge e6 = graph.createEdge(groupNode, v2);
100  
101     //optionally setup some edge groups
102     EdgeMap spg = graph.createEdgeMap();
103     EdgeMap tpg = graph.createEdgeMap();
104     
105     graph.addDataProvider(PortConstraintKeys.SOURCE_GROUPID_KEY, spg);
106     graph.addDataProvider(PortConstraintKeys.TARGET_GROUPID_KEY, tpg);
107     
108     spg.set(e1, "SGroup1");
109     spg.set(e3, "SGroup1");
110     tpg.set(e1, "TGroup1");
111     tpg.set(e3, "TGroup1");
112 
113     //optionally setup the node grouping
114     NodeMap nodeId = graph.createNodeMap();
115     NodeMap parentNodeId = graph.createNodeMap();
116     NodeMap groupKey = graph.createNodeMap();
117     
118     graph.addDataProvider(Grouping.NODE_ID_DPKEY, nodeId);
119     graph.addDataProvider(Grouping.PARENT_NODE_ID_DPKEY, parentNodeId);
120     graph.addDataProvider(Grouping.GROUP_DPKEY, groupKey);
121     
122     //mark a node as a group node
123     groupKey.setBool(groupNode, true);
124     
125     // add ids for each node
126     nodeId.set(v1, "v1");
127     nodeId.set(v2, "v2");
128     nodeId.set(v3, "v3");
129     nodeId.set(v4, "v4");
130     nodeId.set(groupNode, "groupNode");
131     
132     // set the parent for each grouped node
133     parentNodeId.set(v2, "groupNode");
134     parentNodeId.set(v3, "groupNode");
135     
136     HierarchicGroupLayouter layouter = new HierarchicGroupLayouter();
137     
138     layouter.setMinimalLayerDistance(0.0d);
139     layouter.setMinimalEdgeDistance(10.0d);
140   
141     new BufferedLayouter(layouter).doLayout(graph);
142     
143     //display result
144     LayoutPreviewPanel lpp = new LayoutPreviewPanel(graph);
145     lpp.createFrame("Hierarchical Group Layout").setVisible(true);
146 
147     D.bug("\n\nGRAPH LAID OUT USING HIERACHIC GROUP LAYOUT");
148     D.bug("v1 center position = " + graph.getCenter(v1));
149     D.bug("v2 center position = " + graph.getCenter(v2));
150     D.bug("v3 center position = " + graph.getCenter(v3));
151     D.bug("v4 center position = " + graph.getCenter(v4));
152     D.bug("group center position = " + graph.getCenter(groupNode));
153     D.bug("group size = " + graph.getSize(groupNode));
154     D.bug("e1 path = " + graph.getPath(e1));
155     D.bug("e2 path = " + graph.getPath(e2));
156     D.bug("e3 path = " + graph.getPath(e3));
157     D.bug("e4 path = " + graph.getPath(e4));
158     D.bug("e5 path = " + graph.getPath(e5));
159     D.bug("e6 path = " + graph.getPath(e4));
160   }
161 }
162