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