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.module;
15  
16  import demo.view.DemoBase;
17  import y.module.CircularLayoutModule;
18  import y.module.IncrementalHierarchicLayoutModule;
19  import y.module.LayoutModule;
20  import y.module.OrthogonalLayoutModule;
21  import y.module.RandomLayoutModule;
22  import y.module.SmartOrganicLayoutModule;
23  import y.module.TreeLayoutModule;
24  import y.module.YModule;
25  import y.option.OptionHandler;
26  import y.view.Arrow;
27  import y.view.hierarchy.HierarchyManager;
28  
29  import java.awt.Component;
30  import java.awt.EventQueue;
31  import java.awt.event.ActionEvent;
32  import java.awt.event.ActionListener;
33  import java.util.Locale;
34  
35  import javax.swing.AbstractAction;
36  import javax.swing.DefaultListCellRenderer;
37  import javax.swing.JComboBox;
38  import javax.swing.JList;
39  import javax.swing.JToolBar;
40  import javax.swing.ListCellRenderer;
41  import javax.swing.Action;
42  
43  /**
44   * Demonstrates how layout modules can be added to the GUI of an application.
45   * A layout module is a layout algorithm combined
46   * with an option dialog, that allows to change the
47   * options of a layout algorithm interactively
48   * (only available if layout is part of distribution).
49   *
50   */
51  public class LayoutModuleDemo extends DemoBase {
52    public LayoutModuleDemo() {
53      //use a delta arrow to make edge directions clear
54      view.getGraph2D().getDefaultEdgeRealizer().setArrow(Arrow.DELTA);
55  
56      //to enable loading of hierarchical grouped graphs
57      new HierarchyManager(view.getGraph2D());
58      loadGraph("resource/sample.graphml");
59    }
60  
61    /**
62     * Creates a toolbar for choosing, configuring, and running layout algorithms.
63     */
64    protected JToolBar createToolBar() {
65      final JComboBox modules = new JComboBox();
66      modules.setRenderer(new YModuleListCellRenderer());
67      modules.addItem(new IncrementalHierarchicLayoutModule());
68      modules.addItem(new SmartOrganicLayoutModule());
69      modules.addItem(new OrthogonalLayoutModule());
70      modules.addItem(new CircularLayoutModule());
71      modules.addItem(new DiagonalLayoutModule());
72      modules.addItem(new RandomLayoutModule());
73      modules.setSelectedIndex(1);
74      modules.setMaximumSize(modules.getPreferredSize());
75      modules.addActionListener(new ActionListener() {
76        public void actionPerformed(ActionEvent e) {
77          final LayoutModule module = (LayoutModule) modules.getSelectedItem();
78          if (module != null) {
79            module.start(view.getGraph2D());
80          }
81        }
82      });
83  
84      final Action propertiesAction = new AbstractAction(
85              "Settings...", getIconResource("resource/properties.png")) {
86        public void actionPerformed(final ActionEvent e) {
87          final LayoutModule module = (LayoutModule) modules.getSelectedItem();
88          if (module != null) {
89            final OptionHandler settings = module.getOptionHandler();
90            if (settings != null) {
91              OptionSupport.showDialog(module, view.getGraph2D(), false, view.getFrame());
92            }
93          }
94        }
95      };
96  
97      final Action layoutAction = new AbstractAction(
98              "Layout", SHARED_LAYOUT_ICON) {
99        public void actionPerformed(final ActionEvent e) {
100         final LayoutModule module = (LayoutModule) modules.getSelectedItem();
101         if (module != null) {
102           module.start(view.getGraph2D());
103         }
104       }
105     };
106 
107     final JToolBar toolBar = super.createToolBar();
108     toolBar.addSeparator();
109     toolBar.add(createActionControl(layoutAction));
110     toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
111     toolBar.add(modules);
112     toolBar.addSeparator(TOOLBAR_SMALL_SEPARATOR);
113     toolBar.add(createActionControl(propertiesAction));
114 
115     return toolBar;
116   }
117 
118   public static void main( String[] args ) {
119     EventQueue.invokeLater(new Runnable() {
120       public void run() {
121         Locale.setDefault(Locale.ENGLISH);
122         initLnF();
123         new LayoutModuleDemo().start();
124       }
125     });
126   }
127 
128   static class YModuleListCellRenderer implements ListCellRenderer {
129     final DefaultListCellRenderer dlcr = new DefaultListCellRenderer();
130 
131     public Component getListCellRendererComponent(
132             final JList list,
133             final Object value,
134             final int index,
135             final boolean isSelected,
136             final boolean cellHasFocus
137     ) {
138       if (value instanceof LayoutModule) {
139         final String name;
140         if (value instanceof CircularLayoutModule) {
141           name = "Circular";
142         } else if (value instanceof DiagonalLayoutModule) {
143           name = "Diagonal";
144         } else if (value instanceof IncrementalHierarchicLayoutModule) {
145           name = "Hierarchic";
146         } else if (value instanceof SmartOrganicLayoutModule) {
147           name = "Organic";
148         } else if (value instanceof OrthogonalLayoutModule) {
149           name = "Orthogonal";
150         } else if (value instanceof RandomLayoutModule) {
151           name = "Random";
152         } else if (value instanceof TreeLayoutModule) {
153           name = "Tree";
154         } else {
155           name = ((YModule) value).getModuleName().toLowerCase();
156         }
157         return dlcr.getListCellRendererComponent(list, name, index, isSelected, cellHasFocus);
158       } else {
159         return dlcr.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
160       }
161     }
162   }
163 }
164