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.advanced;
29  
30  import demo.view.DemoBase;
31  import demo.view.DemoDefaults;
32  
33  import y.base.Edge;
34  import y.base.EdgeList;
35  import y.base.Node;
36  import y.base.NodeCursor;
37  import y.base.NodeList;
38  import y.view.DefaultGraph2DRenderer;
39  import y.view.Drawable;
40  import y.view.EditMode;
41  import y.view.Graph2D;
42  import y.view.Graph2DRenderer;
43  import y.view.NodeRealizer;
44  import y.view.PopupMode;
45  
46  import javax.swing.AbstractAction;
47  import javax.swing.JPopupMenu;
48  
49  import java.awt.Color;
50  import java.awt.Graphics2D;
51  import java.awt.Rectangle;
52  import java.awt.EventQueue;
53  import java.awt.event.ActionEvent;
54  import java.util.Locale;
55  
56  
57  /**
58   * Demonstrates how to put a part of a graph in an inactive background layer of the view.
59   * When parts of the graph are selected, then a right mouse click brings up a menu that offers to
60   * put the selected part of the graph to the inactive background.  
61   * If none is selected then a right mouse click brings up a popup menu that allows to bring 
62   * the inactive graph part back to life.
63   *
64   * @see <a href="http://docs.yworks.com/yfiles/doc/api/index.html#/dguide/mvc_controller" target="_blank">Section User Interaction</a> in the yFiles for Java Developer's Guide
65   */
66  public class InactiveLayerDemo extends DemoBase {
67    Graph2D inactiveGraph;
68    EdgeList hiddenEdges;
69    
70    protected void initialize() { 
71      super.initialize();
72      loadGraph("resource/InactiveLayerDemo.graphml");
73      DemoDefaults.applyRealizerDefaults(view.getGraph2D(), true, true);
74      inactiveGraph = new Graph2D();
75      hiddenEdges = new EdgeList();
76      view.addBackgroundDrawable(new Graph2DDrawable(inactiveGraph));
77    }
78  
79    protected void registerViewModes() {
80      EditMode editMode = new EditMode();
81      editMode.setPopupMode(new MyPopupMode());
82      view.addViewMode(editMode);
83    }
84  
85    class MyPopupMode extends PopupMode {
86   
87      public JPopupMenu getNodePopup(final Node v) {
88        JPopupMenu pm = new JPopupMenu();            
89        pm.add(new AbstractAction("Deactivate") {
90          public void actionPerformed(ActionEvent e) {
91            deactivateSelected();          
92          }
93        });
94        return pm;
95      }
96      
97      public JPopupMenu getSelectionPopup(double x, double y) {
98        JPopupMenu pm = new JPopupMenu();      
99        pm.add(new AbstractAction("Deactivate") {
100         public void actionPerformed(ActionEvent e) {
101           deactivateSelected();          
102         }
103       });      
104       return pm;
105     }
106 
107     void deactivateSelected() {
108       Graph2D graph = view.getGraph2D();
109       graph.firePreEvent();
110       graph.backupRealizers(graph.selectedNodes());
111       try {
112         NodeList selectedNodes = new NodeList(graph.selectedNodes());
113         for (NodeCursor nc = selectedNodes.nodes(); nc.ok(); nc.next()) {
114           NodeRealizer r = graph.getRealizer(nc.node());
115           r.setSelected(false);
116           r.setFillColor(Color.lightGray);
117           r.setLineColor(Color.gray);
118         }
119         hiddenEdges.addAll(graph.moveSubGraph(selectedNodes, inactiveGraph));
120         view.updateView();
121       } finally {
122         graph.firePostEvent();
123       }
124     }
125     
126     public JPopupMenu getPaperPopup(double x, double y) {
127       JPopupMenu pm = new JPopupMenu();
128       pm.add(new AbstractAction("Activate All") {
129         public void actionPerformed(ActionEvent e) {
130           Graph2D graph = view.getGraph2D();
131           graph.firePreEvent();
132           graph.backupRealizers(inactiveGraph.nodes());
133           try {
134             NodeList selectedNodes = new NodeList(inactiveGraph.nodes());
135             for (NodeCursor nc = selectedNodes.nodes(); nc.ok(); nc.next()) {
136               NodeRealizer r = graph.getRealizer(nc.node());
137               r.setFillColor(DemoDefaults.DEFAULT_NODE_COLOR);
138               r.setLineColor(DemoDefaults.DEFAULT_NODE_LINE_COLOR);
139             }
140             inactiveGraph.moveSubGraph(selectedNodes, graph);
141             while(!hiddenEdges.isEmpty()) {
142               Edge edge = hiddenEdges.popEdge();
143               if(graph.contains(edge.source()) && graph.contains(edge.target())) {
144                 graph.reInsertEdge(edge);
145               }
146             }
147             view.updateView();
148           } finally {
149             graph.firePostEvent();
150           }
151         }
152       });
153       return pm;
154     }
155   }
156 
157   static class Graph2DDrawable implements Drawable {
158     Graph2D graph;
159     Graph2DRenderer renderer;
160 
161     Graph2DDrawable(Graph2D g) {
162       this.graph = g;
163       renderer = new DefaultGraph2DRenderer();
164     }
165 
166     public void paint(Graphics2D gfx) {
167       renderer.paint(gfx, graph);
168     }
169 
170     public Rectangle getBounds() {
171       return graph.getBoundingBox();
172     }
173   }
174 
175   public static void main(String[] args) {
176     EventQueue.invokeLater(new Runnable() {
177       public void run() {
178         Locale.setDefault(Locale.ENGLISH);
179         initLnF();
180         (new InactiveLayerDemo()).start("Inactive Layer Demo");
181       }
182     });
183   }
184 
185 }
186 
187     
188 
189       
190