Coverage details for edu.uci.ics.jung.visualization.control.EditingPopupGraphMousePlugin

LineHitsSource
1 /*
2  * Copyright (c) 2003, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  */
9 /**
10  *
11  */
12 package edu.uci.ics.jung.visualization.control;
13  
14 import java.awt.event.ActionEvent;
15 import java.awt.event.MouseEvent;
16 import java.awt.geom.Point2D;
17 import java.util.Iterator;
18 import java.util.Set;
19  
20 import javax.swing.AbstractAction;
21 import javax.swing.JMenu;
22 import javax.swing.JPopupMenu;
23  
24 import edu.uci.ics.jung.graph.Edge;
25 import edu.uci.ics.jung.graph.Graph;
26 import edu.uci.ics.jung.graph.Vertex;
27 import edu.uci.ics.jung.graph.impl.DirectedSparseEdge;
28 import edu.uci.ics.jung.graph.impl.SparseVertex;
29 import edu.uci.ics.jung.graph.impl.UndirectedSparseEdge;
30 import edu.uci.ics.jung.visualization.Layout;
31 import edu.uci.ics.jung.visualization.PickSupport;
32 import edu.uci.ics.jung.visualization.PickedState;
33 import edu.uci.ics.jung.visualization.SettableVertexLocationFunction;
34 import edu.uci.ics.jung.visualization.VisualizationViewer;
35  
36 /**
37  * a plugin that uses popup menus to create vertices, undirected edges,
38  * and directed edges.
39  *
40  * @author Tom Nelson - RABA Technologies
41  *
42  */
43 public class EditingPopupGraphMousePlugin extends AbstractPopupGraphMousePlugin {
44     
45     SettableVertexLocationFunction vertexLocations;
46     
470    public EditingPopupGraphMousePlugin(SettableVertexLocationFunction vertexLocations) {
480        this.vertexLocations = vertexLocations;
490    }
50  
51     protected void handlePopup(MouseEvent e) {
520        final VisualizationViewer vv =
53             (VisualizationViewer)e.getSource();
540        final Layout layout = vv.getGraphLayout();
550        final Graph graph = layout.getGraph();
560        final Point2D p = e.getPoint();
570        final Point2D ivp = vv.inverseViewTransform(e.getPoint());
580        PickSupport pickSupport = vv.getPickSupport();
590        if(pickSupport != null) {
60             
610            final Vertex vertex = pickSupport.getVertex(ivp.getX(), ivp.getY());
620            final Edge edge = pickSupport.getEdge(ivp.getX(), ivp.getY());
630            final PickedState pickedState = vv.getPickedState();
640            JPopupMenu popup = new JPopupMenu();
65             
660            if(vertex != null) {
670                Set picked = pickedState.getPickedVertices();
680                if(picked.size() > 0) {
690                    JMenu directedMenu = new JMenu("Create Directed Edge");
700                    popup.add(directedMenu);
710                    for(Iterator iterator=picked.iterator(); iterator.hasNext(); ) {
720                        final Vertex other = (Vertex)iterator.next();
730                        directedMenu.add(new AbstractAction("["+other+","+vertex+"]") {
74                             public void actionPerformed(ActionEvent e) {
75                                 Edge newEdge = new DirectedSparseEdge(other, vertex);
76                                 graph.addEdge(newEdge);
77                                 vv.repaint();
78                             }
79                         });
80                     }
810                    JMenu undirectedMenu = new JMenu("Create Undirected Edge");
820                    popup.add(undirectedMenu);
830                    for(Iterator iterator=picked.iterator(); iterator.hasNext(); ) {
840                        final Vertex other = (Vertex)iterator.next();
850                        undirectedMenu.add(new AbstractAction("[" + other+","+vertex+"]") {
86                             public void actionPerformed(ActionEvent e) {
87                                 Edge newEdge = new UndirectedSparseEdge(other, vertex);
88                                 graph.addEdge(newEdge);
89                                 vv.repaint();
90                             }
91                         });
92                     }
93                 }
940                popup.add(new AbstractAction("Delete Vertex") {
95                     public void actionPerformed(ActionEvent e) {
96                         pickedState.pick(vertex, false);
97                         graph.removeVertex(vertex);
98                         vv.repaint();
99                     }});
1000            } else if(edge != null) {
1010                popup.add(new AbstractAction("Delete Edge") {
102                     public void actionPerformed(ActionEvent e) {
103                         pickedState.pick(edge, false);
104                         graph.removeEdge(edge);
105                         vv.repaint();
106                     }});
107             } else {
1080                popup.add(new AbstractAction("Create Vertex") {
109                     public void actionPerformed(ActionEvent e) {
110                         Vertex newVertex = new SparseVertex();
111                         vertexLocations.setLocation(newVertex, vv.inverseTransform(p));
112                         Layout layout = vv.getGraphLayout();
113                         for(Iterator iterator=graph.getVertices().iterator(); iterator.hasNext(); ) {
114                             layout.lockVertex((Vertex)iterator.next());
115                         }
116                         graph.addVertex(newVertex);
117                         vv.getModel().restart();
118                         for(Iterator iterator=graph.getVertices().iterator(); iterator.hasNext(); ) {
119                             layout.unlockVertex((Vertex)iterator.next());
120                         }
121                         vv.repaint();
122                     }
123                 });
124             }
1250            if(popup.getComponentCount() > 0) {
1260                popup.show(vv, e.getX(), e.getY());
127             }
128         }
1290    }
130 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.