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 java.util.HashMap;
18  import java.awt.EventQueue;
19  
20  import y.base.DataMap;
21  import y.geom.YPoint;
22  import y.layout.*;
23  import y.layout.DefaultLayoutGraph;
24  import y.layout.BufferedLayouter;
25  import y.util.D;
26  import y.base.Edge;
27  import y.base.EdgeMap;
28  import y.base.Node;
29  import y.layout.PortConstraint;
30  import y.layout.PortConstraintKeys;
31  import y.layout.hierarchic.IncrementalHierarchicLayouter;
32  import y.layout.hierarchic.incremental.IncrementalHintsFactory;
33  import y.util.Maps;
34  
35  /**
36   * This demo shows how to use the incremental hierarchical layout algorithm
37   * without using classes that are only present in the yFiles Viewer Distribution. 
38   * In this demo, first a graph will be laid out from scratch. Then new graph elements
39   * will be added to the graph structure. Finally, an updated layout for the
40   * grown graph structure will be calculated. The updated layout
41   * will still look similar to the original layout. This feature
42   * is called incremental layout.
43   * <br>
44   * This demo displays the calculated coordinates before and
45   * after the incremental layout step in a simple graph viewer.
46   * Additionally it outputs the calculated coordinates of the graph layout to
47   * the console.
48   *
49   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/incremental_hierarchical_layouter.html">Section Hierarchical Layout Style</a> in the yFiles for Java Developer's Guide
50   */
51  public class IncrementalLayoutWithoutAView
52  {
53    
54    /**
55     * Launcher
56     */
57    public static void main(String[] args) {
58      EventQueue.invokeLater(new Runnable() {
59        public void run() {
60          IncrementalLayoutWithoutAView lwv = new IncrementalLayoutWithoutAView();
61          lwv.doit();
62        }
63      });
64    }
65    
66    /**
67     * Uses IncrementalHierarchicLayouter to perform an incremental layout of a graph.
68     */
69    public void doit()
70    {
71      DefaultLayoutGraph graph = new DefaultLayoutGraph();
72      
73      //construct graph. assign sizes to nodes
74      Node v1 = graph.createNode();
75      graph.setSize(v1,30,30);
76      Node v2 = graph.createNode();
77      graph.setSize(v2,30,30);
78      Node v3 = graph.createNode();
79      graph.setSize(v3,30,30);
80      
81      Edge e1 = graph.createEdge(v1,v2);
82      Edge e2 = graph.createEdge(v1,v3);
83   
84      //optionally setup some port constraints for HierarchicLayouter
85      EdgeMap spc = graph.createEdgeMap();
86      EdgeMap tpc = graph.createEdgeMap();
87      //e1 shall leave and enter the node on the right side
88      spc.set(e1, PortConstraint.create(PortConstraint.EAST));
89      //additionally set a strong port constraint on the target side. 
90      tpc.set(e1, PortConstraint.create(PortConstraint.EAST, true));
91      //ports with strong port constraints will not be reset by the 
92      //layouter.  So we specify the target port right now to connect 
93      //to the upper left corner of the node 
94      graph.setTargetPointRel(e1, new YPoint(15, -15));
95      
96      //e2 shall leave and enter the node on the top side
97      spc.set(e2, PortConstraint.create(PortConstraint.NORTH));
98      tpc.set(e2, PortConstraint.create(PortConstraint.NORTH));
99      
100     graph.addDataProvider(PortConstraintKeys.SOURCE_PORT_CONSTRAINT_KEY, spc);
101     graph.addDataProvider(PortConstraintKeys.TARGET_PORT_CONSTRAINT_KEY, tpc);
102     
103     IncrementalHierarchicLayouter layouter = new IncrementalHierarchicLayouter();
104     layouter.setLayoutMode(IncrementalHierarchicLayouter.LAYOUT_MODE_FROM_SCRATCH);
105     
106     new BufferedLayouter(layouter).doLayout(graph);
107     
108     //display result
109     LayoutPreviewPanel lpp1 = new LayoutPreviewPanel(new CopiedLayoutGraph(graph));
110     lpp1.createFrame("Hierarchical").setVisible(true);
111     
112     D.bug("\n\nGRAPH LAID OUT HIERARCHICALLY FROM SCRATCH");
113     D.bug("v1 center position = " + graph.getCenter(v1));
114     D.bug("v2 center position = " + graph.getCenter(v2));
115     D.bug("v3 center position = " + graph.getCenter(v3));
116     D.bug("e1 path = " + graph.getPath(e1));
117     D.bug("e2 path = " + graph.getPath(e2));
118 
119     //now add a node and two edges incrementally...
120     Node v4 = graph.createNode();
121     graph.setSize(v4,30,30);
122     
123     Edge e3 = graph.createEdge(v1,v4);
124     Edge e4 = graph.createEdge(v4,v2);
125     
126     IncrementalHintsFactory ihf = layouter.createIncrementalHintsFactory();
127     DataMap map = Maps.createDataMap(new HashMap());
128     
129     map.set(v4, ihf.createLayerIncrementallyHint(v4));
130     map.set(e3, ihf.createSequenceIncrementallyHint(e3));
131     map.set(e4, ihf.createSequenceIncrementallyHint(e4));
132     
133     graph.addDataProvider(IncrementalHierarchicLayouter.INCREMENTAL_HINTS_DPKEY, map);
134     layouter.setLayoutMode(IncrementalHierarchicLayouter.LAYOUT_MODE_INCREMENTAL);
135     
136     new BufferedLayouter(layouter).doLayout(graph);
137     
138     //display result
139     LayoutPreviewPanel lpp2 = new LayoutPreviewPanel(graph);
140     lpp2.createFrame("Hierarchical with incrementally added elements").setVisible(true);
141 
142     D.bug("\n\nGRAPH AFTER ELEMENTS HAVE BEEN ADDED INCREMENTALLY");
143     D.bug("v1 center position = " + graph.getCenter(v1));
144     D.bug("v2 center position = " + graph.getCenter(v2));
145     D.bug("v3 center position = " + graph.getCenter(v3));
146     D.bug("v4 center position = " + graph.getCenter(v4));
147     D.bug("e1 path = " + graph.getPath(e1));
148     D.bug("e2 path = " + graph.getPath(e2));
149     D.bug("e3 path = " + graph.getPath(e3));
150     D.bug("e4 path = " + graph.getPath(e4));
151   }
152 }
153