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