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.tree;
15  
16  import demo.view.DemoBase;
17  import demo.view.DemoDefaults;
18  
19  import y.base.Node;
20  import y.base.NodeList;
21  import y.layout.tree.GenericTreeLayouter;
22  import y.view.Graph2D;
23  import y.view.NavigationMode;
24  
25  import javax.swing.AbstractAction;
26  import javax.swing.ButtonGroup;
27  import javax.swing.JToggleButton;
28  import javax.swing.JToolBar;
29  import java.awt.EventQueue;
30  import java.awt.event.ActionEvent;
31  import java.net.URL;
32  import java.util.Locale;
33  
34  /**
35   * This demo shows the usage of the TreeLayoutConfiguration.
36   * The TreeLayoutConfiguration offers examples how to configure and run the {@link GenericTreeLayouter}.
37   *
38   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/cls_GenericTreeLayouter.html">Section Generic Tree Layout</a> in the yFiles for Java Developer's Guide
39   **/
40  public class TreeLayoutConfigurationDemo extends DemoBase {
41    public static void main(String[] args) {
42      EventQueue.invokeLater(new Runnable() {
43        public void run() {
44          Locale.setDefault(Locale.ENGLISH);
45          initLnF();
46          new TreeLayoutConfigurationDemo().start();
47        }
48      });    
49    }
50  
51    private void layout(TreeLayoutConfiguration configuration) {
52      GenericTreeLayouter genericTreeLayouter = new GenericTreeLayouter();
53      configuration.layout(genericTreeLayouter, view.getGraph2D());
54      view.fitContent();
55      view.updateView();
56    }
57  
58    protected void initialize() {
59      loadGraph("resource/dfb2004.graphml");
60      layout(TreeLayoutConfiguration.PLAYOFFS);
61    }
62  
63    protected void registerViewModes() {
64      view.addViewMode(new NavigationMode());
65    }
66    
67    protected void loadGraph(URL resource) {
68      super.loadGraph(resource);
69      DemoDefaults.applyRealizerDefaults(view.getGraph2D());
70    }
71    
72    protected JToolBar createToolBar() {
73      final AbstractAction[] actions = {
74          new AbstractAction("Playoffs") {
75            public void actionPerformed(ActionEvent e) {
76              view.getGraph2D().clear();
77              loadGraph("resource/dfb2004.graphml");
78              layout(TreeLayoutConfiguration.PLAYOFFS);
79            }
80          },
81          new AbstractAction("Playoffs Double") {
82            public void actionPerformed(ActionEvent e) {
83              view.getGraph2D().clear();
84              loadGraph("resource/dfb2004.graphml");
85              layout(TreeLayoutConfiguration.PLAYOFFS_DOUBLE);
86            }
87          },
88          new AbstractAction("Double Line") {
89            public void actionPerformed(ActionEvent e) {
90              view.getGraph2D().clear();
91              createTree(view.getGraph2D(), new int[]{1, 4, 6, 8});
92              layout(TreeLayoutConfiguration.DOUBLE_LINE);
93            }
94          },
95          new AbstractAction("Bus") {
96            public void actionPerformed(ActionEvent e) {
97              view.getGraph2D().clear();
98              createTree(view.getGraph2D(), new int[]{1, 4, 3, 8});
99              layout(TreeLayoutConfiguration.BUS);
100           }
101         },
102         new AbstractAction("Layered Tree") {
103           public void actionPerformed(ActionEvent e) {
104             view.getGraph2D().clear();
105             createTree(view.getGraph2D(), new int[]{1, 4, 4, 4});
106             layout(TreeLayoutConfiguration.LAYERED_TREE);
107           }
108         },
109         new AbstractAction("Default Delegating") {
110           public void actionPerformed(ActionEvent e) {
111             view.getGraph2D().clear();
112             createTree(view.getGraph2D(), new int[]{1, 4, 3, 8});
113             layout(TreeLayoutConfiguration.DEFAULT_DELEGATING);
114           }
115         },
116     };
117 
118     final JToolBar jtb = super.createToolBar();
119     jtb.addSeparator();
120     final ButtonGroup group = new ButtonGroup();
121     for (int i = 0; i < actions.length; ++i) {
122       final JToggleButton jb = new JToggleButton(actions[i]);
123       jb.setSelected(i == 0);
124       jtb.add(jb);
125       group.add(jb);
126     }
127     return jtb;
128   }
129 
130   /**
131    * Creates a tree with specified number of children per parent and layer.
132    */
133   public static Graph2D createTree(Graph2D graph, int[] childrenCountPerLayer) {
134     if (childrenCountPerLayer.length == 0) {
135       return graph;
136     }
137     if (childrenCountPerLayer[0] != 1) {
138       throw new IllegalArgumentException("The first layer must contain 1 node");
139     }
140 
141     NodeList lastLayerContent = new NodeList();
142 
143     //First layer
144     Node node = graph.createNode();
145     lastLayerContent.add(node);
146 
147     for (int i = 1; i < childrenCountPerLayer.length; i++) {
148       int childrenCount = childrenCountPerLayer[i];
149 
150       NodeList newLayerContent = new NodeList();
151       for (int j = 0; j < lastLayerContent.size(); j++) {
152         Node parent = (Node) lastLayerContent.get(j);
153 
154         for (int k = 0; k < childrenCount; k++) {
155           Node child = graph.createNode();
156           newLayerContent.add(child);
157           graph.setLabelText(child, String.valueOf(graph.N()));
158           graph.createEdge(parent, child);
159         }
160       }
161 
162       lastLayerContent = newLayerContent;
163     }
164     return graph;
165   }
166 
167 }
168