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