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