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.advanced.ports;
15  
16  import y.base.Node;
17  import y.view.CreateEdgeMode;
18  
19  /**
20   * Custom <code>CreateEdgeMode</code> implementation that restricts edge
21   * creation to nodes with node ports. 
22   *
23   */
24  class PortCreateEdgeMode extends CreateEdgeMode {
25    /**
26     * Overwritten to accept only nodes with node ports.
27     * @param source the node to check.
28     * @param x the x-coordinate of the mouse event that triggered edge creation
29     * for the specified node.
30     * @param y the y-coordinate of the mouse event that triggered edge creation
31     * for the specified node.
32     * @return <code>true<code> if the specified node has node ports;
33     * <code>false</code> otherwise.
34     */
35    protected boolean acceptSourceNode( final Node source, final double x, final double y ) {
36      return hasPorts(source);
37    }
38  
39    /**
40     * Overwritten to accept only nodes with node ports.
41     * @param target the node to check.
42     * @param x the x-coordinate of the mouse event that triggered edge creation
43     * for the specified node.
44     * @param y the y-coordinate of the mouse event that triggered edge creation
45     * for the specified node.
46     * @return <code>true<code> if the specified node has node ports;
47     * <code>false</code> otherwise.
48     */
49    protected boolean acceptTargetNode( final Node target, final double x, final double y ) {
50      return hasPorts(target);
51    }
52  
53    private boolean hasPorts( final Node node ) {
54      return view.getGraph2D().getRealizer(node).portCount() > 0;
55    }
56  }
57