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  package demo.layout.withoutview;
15  
16  import y.base.DataProvider;
17  import y.base.EdgeMap;
18  import y.base.Node;
19  import y.base.NodeMap;
20  import y.layout.BufferedLayouter;
21  import y.layout.DefaultLayoutGraph;
22  import y.layout.LayoutGraph;
23  import y.layout.LayoutMultiplexer;
24  import y.layout.LayoutOrientation;
25  import y.layout.PortConstraint;
26  import y.layout.PortConstraintKeys;
27  import y.layout.grouping.GroupingKeys;
28  import y.layout.grouping.RecursiveGroupLayouter;
29  import y.layout.hierarchic.IncrementalHierarchicLayouter;
30  
31  import java.awt.EventQueue;
32  
33  /**
34   * This class shows how to layout the contents of group nodes
35   * each with different layout style. In this example,
36   * the graph induced by the grouped nodes labeled 0 to 5 will be laid out
37   * by HierarchicLayouter using BOTTOM_TO_TOP orientation,
38   * while the remaining nodes will be laid out by
39   * HierarchicLayouter using LEFT_TO_RIGHT orientation.
40   *
41   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/layout_stages.html">Section Layout Stages</a> in the yFiles for Java Developer's Guide
42   */
43  public class RecursiveGroupLayouterDemo
44  {
45    public static void main(String[] args) {
46      EventQueue.invokeLater(new Runnable() {
47        public void run() {
48          LayoutGraph graph = new DefaultLayoutGraph();
49  
50          //create graph structure
51          Node[] v = new Node[10];
52          for (int i = 0; i < v.length; i++) {
53            v[i] = graph.createNode();
54            graph.setSize(v[i], 30, 30);
55          }
56          int[][] e = {{0, 1}, {0, 2}, {0, 3}, {4, 0}, {5, 0}, {0, 7}, {6, 0}, {6, 8}, {8, 7}};
57          for (int i = 0; i < e.length; i++) {
58            Node s = v[e[i][0]];
59            Node t = v[e[i][1]];
60            graph.createEdge(s, t);
61          }
62  
63          //set up fixed port constraints for edges that connect at v[0]
64          EdgeMap spcMap = graph.createEdgeMap();
65          EdgeMap tpcMap = graph.createEdgeMap();
66          graph.addDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY, spcMap);
67          graph.addDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY, tpcMap);
68          spcMap.set(v[0].getEdgeTo(v[7]), PortConstraint.create(PortConstraint.EAST, false));
69          tpcMap.set(v[6].getEdgeTo(v[0]), PortConstraint.create(PortConstraint.WEST, false));
70  
71          //set up edge group information (optional)
72          EdgeMap sgMap = graph.createEdgeMap();
73          EdgeMap tgMap = graph.createEdgeMap();
74          graph.addDataProvider(PortConstraintKeys.SOURCE_GROUPID_KEY, sgMap);
75          graph.addDataProvider(PortConstraintKeys.TARGET_GROUPID_KEY, tgMap);
76          sgMap.set(v[0].getEdgeTo(v[1]), "G1");
77          sgMap.set(v[0].getEdgeTo(v[2]), "G1");
78          sgMap.set(v[0].getEdgeTo(v[3]), "G1");
79  
80          tgMap.set(v[0].getEdgeFrom(v[4]), "G2");
81          tgMap.set(v[0].getEdgeFrom(v[5]), "G2");
82  
83          //set up grouping information
84          NodeMap groupMap = graph.createNodeMap();
85          NodeMap pidMap = graph.createNodeMap();
86          NodeMap idMap = graph.createNodeMap();
87          graph.addDataProvider(GroupingKeys.GROUP_DPKEY, groupMap);
88          graph.addDataProvider(GroupingKeys.NODE_ID_DPKEY, idMap);
89          graph.addDataProvider(GroupingKeys.PARENT_NODE_ID_DPKEY, pidMap);
90          groupMap.setBool(v[9], true);
91          for (int i = 0; i < 6; i++) {
92            pidMap.set(v[i], v[9]);
93          }
94          for (int i = 0; i < v.length; i++) {
95            idMap.set(v[i], v[i]);
96          }
97  
98          //configure layout algorithm
99          IncrementalHierarchicLayouter innerHL = new IncrementalHierarchicLayouter();
100         innerHL.setLayoutOrientation(LayoutOrientation.BOTTOM_TO_TOP);
101         IncrementalHierarchicLayouter outerHL = new IncrementalHierarchicLayouter();
102         outerHL.setLayoutOrientation(LayoutOrientation.LEFT_TO_RIGHT);
103         LayoutMultiplexer lm = new LayoutMultiplexer();
104         NodeMap layoutMap = graph.createNodeMap();
105         graph.addDataProvider(LayoutMultiplexer.LAYOUTER_DPKEY, layoutMap);
106         for (int i = 0; i < 6; i++) {
107           layoutMap.set(v[i], innerHL);
108         }
109         for (int i = 7; i < v.length; i++) {
110           layoutMap.set(v[i], outerHL);
111         }
112 
113         //launch layout algorithm
114         RecursiveGroupLayouter rgl = new RecursiveGroupLayouter(lm);
115         new BufferedLayouter(rgl).doLayout(graph);
116 
117         //remove group node
118         graph.removeNode(v[9]);
119 
120         //  display result
121         LayoutPreviewPanel lpp = new LayoutPreviewPanel(graph);
122         lpp.createFrame("RecursiveGroupLayouterDemo").setVisible(true);
123 
124         //remove all registered DataProviders, NodeMap and EdgeMaps
125         Object[] key = graph.getDataProviderKeys();
126         for (int i = 0; i < key.length; i++) {
127           DataProvider dp = graph.getDataProvider(key[i]);
128           graph.removeDataProvider(key[i]);
129           if (dp instanceof NodeMap) {
130             graph.disposeNodeMap((NodeMap) dp);
131           } else if (dp instanceof EdgeMap) {
132             graph.disposeEdgeMap((EdgeMap) dp);
133           }
134         }
135       }
136     });
137 
138   }
139 
140 }
141