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.flowchart;
29  
30  import y.base.Edge;
31  import y.view.Graph2DView;
32  import y.view.Graph2DViewMouseWheelZoomListener;
33  import y.view.PopupMode;
34  import y.view.CreateEdgeMode;
35  import y.view.EditMode;
36  import y.view.Graph2DViewActions;
37  import y.view.AutoDragViewMode;
38  import y.view.MovePortMode;
39  import y.base.Node;
40  import y.view.TooltipMode;
41  
42  import javax.swing.JPopupMenu;
43  import javax.swing.JMenuItem;
44  
45  /**
46   * Component that visualizes Flowchart diagrams.
47   */
48  public class FlowchartView extends Graph2DView {
49    private Graph2DViewActions flowchartActions;
50  
51    /**
52     * Creates a new Graph2DView containing an empty graph and register Flowchart specific view modes and actions
53     * The constructor calls the following methods, in oder to register respective view modes and actions:
54     * <ul>
55     * <li> {@link #registerViewModes()}</li>
56     * <li> {@link #registerViewActions()} </li>
57     * <li> {@link #registerViewListeners()}</li>
58     * </ul>
59     */
60    public FlowchartView() {
61      super();
62      //Some default behaviour
63      this.setFitContentOnResize(true);
64  
65      //init
66      registerViewModes();
67      registerViewActions();
68      registerViewListeners();
69    }
70  
71    /**
72     * Callback method, which registers Flowchart specific view actions.
73     */
74    protected void registerViewActions() {
75      flowchartActions = new Graph2DViewActions(this);
76      flowchartActions.install();
77    }
78  
79    /**
80     * Callback method, which registers Flowchart specific view modes and configures them.
81     */
82    protected void registerViewModes() {
83      EditMode editMode = new EditMode();
84      // Route all edges orthogonally.
85      editMode.setOrthogonalEdgeRouting(true);
86  
87      CreateEdgeMode createEdgeMode = (CreateEdgeMode) editMode.getCreateEdgeMode();
88      createEdgeMode.setOrthogonalEdgeCreation(true);
89      createEdgeMode.setIndicatingTargetNode(true);
90      editMode.setSnappingEnabled(true);
91  
92      //add hierarchy actions to the views popup menu
93      editMode.setPopupMode(new FlowchartPopupMode());
94      editMode.getMouseInputMode().setNodeSearchingEnabled(true);
95      editMode.assignNodeLabel(false);
96  
97      ((MovePortMode) editMode.getMovePortMode()).setIndicatingTargetNode(true);
98  
99      //add view mode to display tooltips for node
100     TooltipMode tooltipMode = new TooltipMode();
101     tooltipMode.setEdgeTipEnabled(false);
102     addViewMode(tooltipMode);
103 
104     //allow moving view port with right drag gesture
105     editMode.allowMovingWithPopup(true);
106     addViewMode(editMode);
107 
108     //Auto drag mode
109     addViewMode(new AutoDragViewMode());
110   }
111 
112   /**
113    * Callback method, which registers  specific listeners and configures them.
114    */
115   protected void registerViewListeners() {
116     Graph2DViewMouseWheelZoomListener wheelZoomListener = new Graph2DViewMouseWheelZoomListener();
117     //zoom in/out at mouse pointer location
118     wheelZoomListener.setCenterZooming(false);
119     wheelZoomListener.addToCanvas(this);
120   }
121 
122 
123   //////////////////////////////////////////////////////////////////////////////
124   // VIEW MODES ////////////////////////////////////////////////////////////////
125   //////////////////////////////////////////////////////////////////////////////
126 
127   /**
128    * provides the context sensitive popup menus
129    */
130   private class FlowchartPopupMode extends PopupMode {
131 
132     public JPopupMenu getNodePopup(Node v) {
133       JPopupMenu pm = new JPopupMenu();
134       if (v != null) {
135         JMenuItem deleteItem = pm.add(flowchartActions.getDeleteSelectionAction());
136         deleteItem.setText("Delete Node");
137       }
138       return pm;
139     }
140 
141     public JPopupMenu getEdgePopup(Edge e) {
142       JPopupMenu pm = new JPopupMenu();
143       if (e != null) {
144         JMenuItem deleteItem = pm.add(flowchartActions.getDeleteSelectionAction());
145         deleteItem.setText("Delete Edge");
146       }
147       return pm;
148     }
149 
150     public JPopupMenu getSelectionPopup(double x, double y) {
151       JPopupMenu pm = new JPopupMenu();
152       JMenuItem deleteItem = pm.add(flowchartActions.getDeleteSelectionAction());
153       deleteItem.setText("Delete Selection");
154       return pm;
155     }
156   }
157 }
158