| InteractiveOrganicDemo.java |
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.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 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.io.IOException;
28
29 /**
30 * This demo shows the very basic useage of the
31 * {@link y.layout.organic.InteractiveOrganicLayouter}.
32 * The layouter is started within a thread. A swing timer is used to update the
33 * positions of the nodes.
34 */
35 public class InteractiveOrganicDemo extends DemoBase {
36 private InteractiveOrganicLayouter layouter;
37
38 protected void initialize() {
39 layouter = new InteractiveOrganicLayouter();
40
41 loadGraph( "resource/peopleNav.ygf" );
42 //Reset the paths and the locations of the nodes.
43 LayoutTool.initDiagram( view.getGraph2D() );
44
45 view.updateWorldRect();
46 view.fitContent();
47 view.setZoom( 0.3 );
48 }
49
50 /**
51 * Callback used by {@link #registerViewModes()} to create the default EditMode
52 * @return an instance of {@link y.view.EditMode} with showNodeTips enabled
53 */
54 protected EditMode createEditMode() {
55 EditMode editMode = super.createEditMode();
56 editMode.allowBendCreation( false );
57 editMode.allowNodeCreation( false );
58 editMode.allowResizeNodes( false );
59 editMode.allowEdgeCreation( false );
60
61 //This view mode offers support for "touching the graph"
62 editMode.setMoveSelectionMode( new InteractiveMoveSelectionMode( layouter ) );
63
64 return editMode;
65 }
66
67 protected JToolBar createToolBar() {
68 JToolBar toolBar = super.createToolBar();
69 final JButton button = new JButton( "Layout" );
70 toolBar.add( button );
71
72 button.addActionListener( new ActionListener() {
73 public void actionPerformed( ActionEvent e ) {
74 //Disable the button
75 button.setEnabled( false );
76
77 //Start the layout thread
78 new Thread( new Runnable() {
79 public void run() {
80 //This call will not return until {@link y.layout.organic.InteractiveOrganicLayouter#stop()} is called.
81 //The calculated layout is *not* automatically applied on the graph.
82 layouter.doLayout( new CopiedLayoutGraph( view.getGraph2D() ) );
83 }
84 } ).start();
85
86 //Update timer
87 Timer timer = new Timer( 21, new ActionListener() {
88 //This listener is notified about 24 times a second.
89 public void actionPerformed( ActionEvent e ) {
90 //Write the calculated positions back to the realizers
91 if ( layouter.commitPositionsSmoothly( 50, 0.15 ) > 0 ) {
92 //... and update the view, if something has changed
93 view.updateView();
94 }
95 }
96 } );
97 timer.setInitialDelay( 500 );
98 timer.start();
99 }
100 } );
101
102 return toolBar;
103 }
104
105
106 public static void main( String[] args ) throws IOException {
107 InteractiveOrganicDemo demo = new InteractiveOrganicDemo();
108 demo.start();
109 }
110 }
111