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