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