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.organic;
15  
16  import demo.view.DemoBase;
17  import y.layout.CopiedLayoutGraph;
18  import y.layout.LayoutTool;
19  import y.layout.organic.InteractiveOrganicLayouter;
20  import y.view.EditMode;
21  
22  import javax.swing.JButton;
23  import javax.swing.JToolBar;
24  import javax.swing.Timer;
25  
26  import java.awt.EventQueue;
27  import java.awt.event.ActionEvent;
28  import java.awt.event.ActionListener;
29  import java.io.IOException;
30  import java.util.Locale;
31  
32  /**
33   * This demo shows the very basic usage of the
34   *  {@link y.layout.organic.InteractiveOrganicLayouter}.
35   * The layouter is started within a thread. A swing timer is used to update the
36   * positions of the nodes.
37   *
38   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/interactive_organic_layouter.html#interactive_organic_layouter">Section Interactive Organic Layout</a> in the yFiles for Java Developer's Guide
39   */
40  public class InteractiveOrganicDemo extends DemoBase {
41    private InteractiveOrganicLayouter layouter;
42  
43    protected void initialize() {
44      layouter = new InteractiveOrganicLayouter();
45      layouter.setAutomaticStructureUpdateEnabled(true);
46      layouter.enableOnlyCore();
47  
48      loadGraph("resource/peopleNav.graphml");
49      //Reset the paths and the locations of the nodes.
50      LayoutTool.initDiagram(view.getGraph2D());
51  
52      view.setPaintDetailThreshold(0.0);
53      view.fitContent();
54      
55    }
56  
57    /**
58     * Callback used by {@link #registerViewModes()} to create the default EditMode
59     * @return an instance of {@link y.view.EditMode} with showNodeTips enabled
60     */
61    protected EditMode createEditMode() {
62      EditMode editMode = super.createEditMode();
63      editMode.allowBendCreation(false);
64      editMode.allowNodeCreation(true);
65      editMode.allowResizeNodes(false);
66      editMode.allowEdgeCreation(true);
67  
68      //This view mode offers support for "touching the graph"
69      editMode.setMoveSelectionMode(new InteractiveMoveSelectionMode(layouter));
70  
71      return editMode;
72    }
73  
74    protected JToolBar createToolBar() {
75      final JButton startButton = new JButton("Start", SHARED_LAYOUT_ICON);
76      startButton.addActionListener(new ActionListener() {
77        public void actionPerformed(ActionEvent e) {
78          //Disable the button
79          startButton.setEnabled(false);
80  
81          //Start the layout thread
82          layouter.startLayout(new CopiedLayoutGraph(view.getGraph2D()));
83  
84          //Update timer
85          Timer timer = new Timer(21, new ActionListener() {
86            //This listener is notified about 24 times a second.
87            public void actionPerformed(ActionEvent e) {
88              //Write the calculated positions back to the realizers
89              if (layouter.commitPositionsSmoothly(50, 0.15) > 0) {
90                //... and update the view, if something has changed
91                view.updateView();
92              }
93            }
94          });
95          timer.setInitialDelay(500);
96          timer.start();
97        }
98      });
99  
100     JToolBar toolBar = super.createToolBar();
101     toolBar.addSeparator();
102     toolBar.add(startButton);
103     return toolBar;
104   }
105 
106 
107   public static void main(String[] args) throws IOException {
108     EventQueue.invokeLater(new Runnable() {
109       public void run() {
110         Locale.setDefault(Locale.ENGLISH);
111         initLnF();
112         InteractiveOrganicDemo demo = new InteractiveOrganicDemo();
113         demo.start();
114       }
115     });
116 
117   }
118 }
119