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