View Javadoc
1   package edu.uci.ics.jung.visualization.control;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.MouseEvent;
5   import java.awt.geom.Point2D;
6   import java.util.Set;
7   
8   import javax.swing.AbstractAction;
9   import javax.swing.JMenu;
10  import javax.swing.JPopupMenu;
11  
12  import com.google.common.base.Supplier;
13  
14  import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
15  import edu.uci.ics.jung.algorithms.layout.Layout;
16  import edu.uci.ics.jung.graph.DirectedGraph;
17  import edu.uci.ics.jung.graph.Graph;
18  import edu.uci.ics.jung.graph.UndirectedGraph;
19  import edu.uci.ics.jung.graph.util.EdgeType;
20  import edu.uci.ics.jung.visualization.VisualizationViewer;
21  import edu.uci.ics.jung.visualization.picking.PickedState;
22  
23  /**
24   * a plugin that uses popup menus to create vertices, undirected edges,
25   * and directed edges.
26   * 
27   * @author Tom Nelson
28   *
29   */
30  public class EditingPopupGraphMousePlugin<V,E> extends AbstractPopupGraphMousePlugin {
31      
32      protected Supplier<V> vertexFactory;
33      protected Supplier<E> edgeFactory;
34  
35      public EditingPopupGraphMousePlugin(Supplier<V> vertexFactory, Supplier<E> edgeFactory) {
36          this.vertexFactory = vertexFactory;
37          this.edgeFactory = edgeFactory;
38      }
39      
40  	@SuppressWarnings({ "unchecked", "serial" })
41  	protected void handlePopup(MouseEvent e) {
42          final VisualizationViewer<V,E> vv =
43              (VisualizationViewer<V,E>)e.getSource();
44          final Layout<V,E> layout = vv.getGraphLayout();
45          final Graph<V,E> graph = layout.getGraph();
46          final Point2D p = e.getPoint();
47          GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
48          if(pickSupport != null) {
49              
50              final V vertex = pickSupport.getVertex(layout, p.getX(), p.getY());
51              final E edge = pickSupport.getEdge(layout, p.getX(), p.getY());
52              final PickedState<V> pickedVertexState = vv.getPickedVertexState();
53              final PickedState<E> pickedEdgeState = vv.getPickedEdgeState();
54              
55              JPopupMenu popup = new JPopupMenu();
56              if(vertex != null) {
57              	Set<V> picked = pickedVertexState.getPicked();
58              	if(picked.size() > 0) {
59              		if(graph instanceof UndirectedGraph == false) {
60              			JMenu directedMenu = new JMenu("Create Directed Edge");
61              			popup.add(directedMenu);
62              			for(final V other : picked) {
63              				directedMenu.add(new AbstractAction("["+other+","+vertex+"]") {
64              					public void actionPerformed(ActionEvent e) {
65              						graph.addEdge(edgeFactory.get(),
66              								other, vertex, EdgeType.DIRECTED);
67              						vv.repaint();
68              					}
69              				});
70              			}
71              		}
72              		if(graph instanceof DirectedGraph == false) {
73              			JMenu undirectedMenu = new JMenu("Create Undirected Edge");
74              			popup.add(undirectedMenu);
75              			for(final V other : picked) {
76              				undirectedMenu.add(new AbstractAction("[" + other+","+vertex+"]") {
77              					public void actionPerformed(ActionEvent e) {
78              						graph.addEdge(edgeFactory.get(),
79              								other, vertex);
80              						vv.repaint();
81              					}
82              				});
83              			}
84              		}
85                  }
86                  popup.add(new AbstractAction("Delete Vertex") {
87                      public void actionPerformed(ActionEvent e) {
88                          pickedVertexState.pick(vertex, false);
89                          graph.removeVertex(vertex);
90                          vv.repaint();
91                      }});
92              } else if(edge != null) {
93                  popup.add(new AbstractAction("Delete Edge") {
94                      public void actionPerformed(ActionEvent e) {
95                          pickedEdgeState.pick(edge, false);
96                          graph.removeEdge(edge);
97                          vv.repaint();
98                      }});
99              } else {
100                 popup.add(new AbstractAction("Create Vertex") {
101                     public void actionPerformed(ActionEvent e) {
102                         V newVertex = vertexFactory.get();
103                         graph.addVertex(newVertex);
104                         layout.setLocation(newVertex, vv.getRenderContext().getMultiLayerTransformer().inverseTransform(p));
105                         vv.repaint();
106                     }
107                 });
108             }
109             if(popup.getComponentCount() > 0) {
110                 popup.show(vv, e.getX(), e.getY());
111             }
112         }
113     }
114 }
115