1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
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-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.view.layout.tree;
15  
16  import demo.view.DemoBase;
17  import y.layout.tree.GenericTreeLayouter;
18  import y.base.NodeList;
19  import y.base.Node;
20  import y.view.Graph2D;
21  
22  import javax.swing.AbstractAction;
23  import javax.swing.JToolBar;
24  import java.awt.Color;
25  import java.awt.event.ActionEvent;
26  
27  /**
28   * This demo shows the usage of the TreeLayoutConfiguration.
29   * The TreeLayoutConfiguration offers examples how to configure and run the {@link GenericTreeLayouter}.
30   *
31   **/
32  public class TreeLayoutConfigurationDemo extends DemoBase {
33  
34    private static Color[] colors = new Color[11];
35  
36    static {
37      for ( int i = 0; i < 11; i++ ) {
38        colors[ i ] = new Color( 255 - i * 22, i * 22, 0 );
39      }
40    }
41  
42    public static void main( String[] args ) {
43      TreeLayoutConfigurationDemo demo = new TreeLayoutConfigurationDemo();
44      demo.start();
45    }
46  
47    private void layout( TreeLayoutConfiguration configuration ) {
48      GenericTreeLayouter genericTreeLayouter = new GenericTreeLayouter();
49      configuration.layout( genericTreeLayouter, view.getGraph2D() );
50      view.fitContent();
51      view.updateView();
52    }
53  
54    protected void initialize() {
55      loadGraph( "resource/dfb2004.gml"  );
56      layout( TreeLayoutConfiguration.PLAYOFFS );
57    }
58  
59    protected JToolBar createToolBar() {
60      JToolBar toolBar = super.createToolBar();
61      toolBar.add( new AbstractAction( "Playoffs" ) {
62        public void actionPerformed( ActionEvent e ) {
63          view.getGraph2D().clear();
64          loadGraph( "resource/dfb2004.gml"  );
65          layout( TreeLayoutConfiguration.PLAYOFFS );
66        }
67      } );
68      toolBar.add( new AbstractAction( "Playoffs double" ) {
69        public void actionPerformed( ActionEvent e ) {
70          view.getGraph2D().clear();
71          loadGraph( "resource/dfb2004.gml"  );
72          layout( TreeLayoutConfiguration.PLAYOFFS_DOUBLE );
73        }
74      } );
75      toolBar.add( new AbstractAction( "Double line" ) {
76        public void actionPerformed( ActionEvent e ) {
77          view.getGraph2D().clear();
78          createTree( view.getGraph2D(), new int[]{ 1, 4, 6, 8 } );
79          layout( TreeLayoutConfiguration.DOUBLE_LINE );
80        }
81      } );
82      toolBar.add( new AbstractAction( "Bus" ) {
83        public void actionPerformed( ActionEvent e ) {
84          view.getGraph2D().clear();
85          createTree( view.getGraph2D(), new int[]{ 1, 4, 3, 8 } );
86          layout( TreeLayoutConfiguration.BUS );
87        }
88      } );
89      toolBar.add( new AbstractAction( "Layered tree" ) {
90        public void actionPerformed( ActionEvent e ) {
91          view.getGraph2D().clear();
92          createTree( view.getGraph2D(), new int[]{ 1, 4,4,4 } );
93          layout( TreeLayoutConfiguration.LAYERED_TREE );
94        }
95      } );
96      toolBar.add( new AbstractAction( "Default delegating" ) {
97        public void actionPerformed( ActionEvent e ) {
98          view.getGraph2D().clear();
99          createTree( view.getGraph2D(), new int[]{ 1, 4, 3, 8 } );
100         layout( TreeLayoutConfiguration.DEFAULT_DELEGATING );
101       }
102     } );
103     return toolBar;
104   }
105 
106   /**
107    * Creates a tree with specified number of children per parent and layer.
108    */
109   public static Graph2D createTree(Graph2D graph, int[] childrenCountPerLayer) {
110     if (childrenCountPerLayer.length == 0) return graph;
111     if (childrenCountPerLayer[0] != 1) throw new IllegalArgumentException("The first layer must contain 1 node");
112 
113     NodeList lastLayerContent = new NodeList();
114 
115     //First layer
116     Node node = graph.createNode();
117     lastLayerContent.add(node);
118 
119     for (int i = 1; i < childrenCountPerLayer.length; i++) {
120       int childrenCount = childrenCountPerLayer[i];
121 
122       NodeList newLayerContent = new NodeList();
123       for (int j = 0; j < lastLayerContent.size(); j++) {
124         Node parent = (Node) lastLayerContent.get(j);
125 
126         for (int k = 0; k < childrenCount; k++) {
127           Node child = graph.createNode();
128           newLayerContent.add(child);
129           graph.setLabelText(child, String.valueOf(graph.N()));
130           graph.createEdge(parent, child);
131         }
132       }
133 
134       lastLayerContent = newLayerContent;
135     }
136     return graph;
137   }
138 
139 }
140