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