1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  
15  package demo.layout;
16  
17  import y.base.Edge;
18  import y.base.EdgeMap;
19  import y.base.Node;
20  import y.base.NodeMap;
21  
22  import y.layout.BufferedLayouter;
23  import y.layout.DefaultLayoutGraph;
24  import y.layout.PortConstraintKeys;
25  import y.layout.grouping.Grouping;
26  import y.layout.hierarchic.HierarchicGroupLayouter;
27  
28  import y.util.D;
29  
30  /**
31   * This class shows how to use layout and grouping algorithms without using classes 
32   * that are only present in the yFiles Viewer Distribution. Therefore this demo
33   * only outputs the calculated coordinates of the graph layout to the console
34   * and displays it inside a simple preview panel.
35   * <br>
36   * In this demo HierarchicGroupLayouter is used to layout a small graph. 
37   */
38  public class GroupingLayoutWithoutAView
39  {
40    
41    /**
42     * Launcher
43     */
44    public static void main(String[] args)
45    {
46      GroupingLayoutWithoutAView lwv = new GroupingLayoutWithoutAView();
47      lwv.doit();
48    }
49  
50    /**
51     * Creates a small graph and applies a hierarchic group layout to it.
52     * <p>
53     * The output of the calculated coordinates will be displayed in the
54     * console.
55     */ 
56    public void doit()
57    {
58      DefaultLayoutGraph graph = new DefaultLayoutGraph();
59      
60      //construct graph. assign sizes to nodes
61      Node v1 = graph.createNode();
62      graph.setSize(v1,30,30);
63      Node v2 = graph.createNode();
64      graph.setSize(v2,30,30);
65      Node v3 = graph.createNode();
66      graph.setSize(v3,30,30);
67      Node v4 = graph.createNode();
68      graph.setSize(v4,30,30);
69      
70      Node groupNode = graph.createNode();
71      graph.setSize(groupNode, 100,100);
72      
73      Edge e1 = graph.createEdge(v1,v2);
74      Edge e2 = graph.createEdge(v4, groupNode);
75      Edge e3 = graph.createEdge(v1,v3);
76      Edge e4 = graph.createEdge(v1, v1);
77      Edge e5 = graph.createEdge(v2, groupNode);
78      Edge e6 = graph.createEdge(groupNode, v2);
79   
80      //optionally setup some edge groups
81      EdgeMap spg = graph.createEdgeMap();
82      EdgeMap tpg = graph.createEdgeMap();
83      
84      graph.addDataProvider(PortConstraintKeys.SOURCE_GROUPID_KEY, spg);
85      graph.addDataProvider(PortConstraintKeys.TARGET_GROUPID_KEY, tpg);
86      
87      spg.set(e1, "SGroup1");
88      spg.set(e3, "SGroup1");
89      tpg.set(e1, "TGroup1");
90      tpg.set(e3, "TGroup1");
91  
92      //optionally setup the node grouping
93      NodeMap nodeId = graph.createNodeMap();
94      NodeMap parentNodeId = graph.createNodeMap();
95      NodeMap groupKey = graph.createNodeMap();
96      
97      graph.addDataProvider(Grouping.NODE_ID_DPKEY, nodeId);
98      graph.addDataProvider(Grouping.PARENT_NODE_ID_DPKEY, parentNodeId);
99      graph.addDataProvider(Grouping.GROUP_DPKEY, groupKey);
100     
101     //mark a node as a group node
102     groupKey.setBool(groupNode, true);
103     
104     // add ids for each node
105     nodeId.set(v1, "v1");
106     nodeId.set(v2, "v2");
107     nodeId.set(v3, "v3");
108     nodeId.set(v4, "v4");
109     nodeId.set(groupNode, "groupNode");
110     
111     // set the parent for each grouped node
112     parentNodeId.set(v2, "groupNode");
113     parentNodeId.set(v3, "groupNode");
114     
115     HierarchicGroupLayouter layouter = new HierarchicGroupLayouter();
116     
117     layouter.setMinimalLayerDistance(0.0d);
118     layouter.setMinimalEdgeDistance(10.0d);
119   
120     new BufferedLayouter(layouter).doLayout(graph);
121     
122     //display result
123     LayoutPreviewPanel lpp = new LayoutPreviewPanel(graph);
124     lpp.createFrame("Hierarchical Group Layout").setVisible(true);
125 
126     D.bug("\n\nGRAPH LAID OUT USING HIERACHIC GROUP LAYOUT");
127     D.bug("v1 center position = " + graph.getCenter(v1));
128     D.bug("v2 center position = " + graph.getCenter(v2));
129     D.bug("v3 center position = " + graph.getCenter(v3));
130     D.bug("v4 center position = " + graph.getCenter(v4));
131     D.bug("group center position = " + graph.getCenter(groupNode));
132     D.bug("group size = " + graph.getSize(groupNode));
133     D.bug("e1 path = " + graph.getPath(e1));
134     D.bug("e2 path = " + graph.getPath(e2));
135     D.bug("e3 path = " + graph.getPath(e3));
136     D.bug("e4 path = " + graph.getPath(e4));
137     D.bug("e5 path = " + graph.getPath(e5));
138     D.bug("e6 path = " + graph.getPath(e4));
139   }
140 }
141