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.view.viewmode;
15  
16  import demo.view.DemoBase;
17  import y.base.Edge;
18  import y.base.Node;
19  import y.view.CreateEdgeMode;
20  import y.view.EditMode;
21  import y.view.Graph2D;
22  import y.view.HitInfo;
23  
24  import javax.swing.Action;
25  import javax.swing.JOptionPane;
26  import java.awt.Color;
27  import java.awt.EventQueue;
28  import java.util.Locale;
29  
30  /**
31   * Demonstrates how to customize CreateEdgeMode in order to control
32   * the creation of edges and to provide feedback whether
33     creating an edge to the node the mouse is hovering over is possible.
34   * <br>
35   * This demo does only allow the creation of edges that start from nodes labeled
36   * "start" and end at nodes labeled "end".
37   * @see <a href="http://docs.yworks.com/yfiles/doc/developers-guide/mvc_controller.html#custom_edit_mode">Section User Interaction</a> in the yFiles for Java Developer's Guide
38   */
39  public class CreateEdgeModeDemo extends DemoBase {
40    // whether or not to display a message box when edge creation 
41    // is not allowed.
42    boolean showMessage = true;
43  
44    protected void registerViewModes() {
45      EditMode editMode = new EditMode() {
46        //Alternately, create nodes labeled "start" and "end"
47        protected void configureNode(final Graph2D graph, final Node node) {
48            graph.setLabelText(node, node.index() % 2 == 0 ? "start" : "end");
49        }
50      };
51      view.addViewMode( editMode );
52      //set a custom CreateEdgeMode for the edge mode
53      editMode.setCreateEdgeMode( new DemoCreateEdgeMode() );
54      loadGraph("resource/popup.graphml");
55    }
56  
57    protected Action createLoadAction() {
58      //Overridden method to disable the Load menu in the demo
59      return null;
60    }
61  
62    protected Action createSaveAction() {
63      //Overridden method to disable the Save menu in the demo
64      return null;
65    }
66  
67    class DemoCreateEdgeMode extends CreateEdgeMode {
68  
69      public void edgeMoved( double x, double y ) {
70        super.edgeMoved( x, y );
71        updateDummy( x, y );
72      }
73  
74      public void edgeCreated( Edge e ) {
75        getGraph2D().getRealizer( e ).setLineColor( getGraph2D().getDefaultEdgeRealizer().getLineColor() );
76      }
77  
78  
79  
80      private void updateDummy( double x, double y ) {
81        Node hitNode = (new HitInfo(view, x, y, true, HitInfo.NODE)).getHitNode();
82        if ( hitNode != null ) {
83          if ( acceptTargetNode( hitNode, x, y ) ) {
84            getDummyEdgeRealizer().setLineColor( Color.green );
85          } else {
86            getDummyEdgeRealizer().setLineColor( Color.red );
87          }
88        } else {
89          getDummyEdgeRealizer().setLineColor( getGraph2D().getDefaultEdgeRealizer().getLineColor() );
90        }
91      }
92  
93      protected boolean acceptSourceNode( Node source, double x, double y ) {
94        return getGraph2D().getLabelText( source ).equals("start");
95      }
96  
97      protected void sourceNodeDeclined( Node source, double x, double y ) {
98        if ( showMessage ) {
99          cancelEdgeCreation();
100         JOptionPane.showMessageDialog( this.view,
101                                        "Edges may only start from nodes marked as start nodes.",
102                                        "Forbidden!",
103                                        JOptionPane.ERROR_MESSAGE );
104       }
105     }
106 
107     protected boolean acceptTargetNode( Node target, double x, double y ) {
108       return getGraph2D().getLabelText( target ).equals("end");
109     }
110 
111     protected void targetNodeDeclined( Node target, double x, double y ) {
112       if ( showMessage ) {
113         cancelEdgeCreation();
114         JOptionPane.showMessageDialog( this.view,
115                                        "Edges may only end at nodes marked as end nodes.",
116                                        "Forbidden!",
117                                        JOptionPane.ERROR_MESSAGE );
118 
119       }
120     }
121   }
122 
123   public static void main(String[] args) {
124     EventQueue.invokeLater(new Runnable() {
125       public void run() {
126         Locale.setDefault(Locale.ENGLISH);
127         initLnF();
128         (new CreateEdgeModeDemo()).start("Create Edge Mode Demo");
129       }
130     });
131   }
132 }
133 
134 
135       
136