Line | Hits | Source |
---|---|---|
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 | ||
47 | 0 | public EditingPopupGraphMousePlugin(SettableVertexLocationFunction vertexLocations) { |
48 | 0 | this.vertexLocations = vertexLocations; |
49 | 0 | } |
50 | ||
51 | protected void handlePopup(MouseEvent e) { | |
52 | 0 | final VisualizationViewer vv = |
53 | (VisualizationViewer)e.getSource(); | |
54 | 0 | final Layout layout = vv.getGraphLayout(); |
55 | 0 | final Graph graph = layout.getGraph(); |
56 | 0 | final Point2D p = e.getPoint(); |
57 | 0 | final Point2D ivp = vv.inverseViewTransform(e.getPoint()); |
58 | 0 | PickSupport pickSupport = vv.getPickSupport(); |
59 | 0 | if(pickSupport != null) { |
60 | ||
61 | 0 | final Vertex vertex = pickSupport.getVertex(ivp.getX(), ivp.getY()); |
62 | 0 | final Edge edge = pickSupport.getEdge(ivp.getX(), ivp.getY()); |
63 | 0 | final PickedState pickedState = vv.getPickedState(); |
64 | 0 | JPopupMenu popup = new JPopupMenu(); |
65 | ||
66 | 0 | if(vertex != null) { |
67 | 0 | Set picked = pickedState.getPickedVertices(); |
68 | 0 | if(picked.size() > 0) { |
69 | 0 | JMenu directedMenu = new JMenu("Create Directed Edge"); |
70 | 0 | popup.add(directedMenu); |
71 | 0 | for(Iterator iterator=picked.iterator(); iterator.hasNext(); ) { |
72 | 0 | final Vertex other = (Vertex)iterator.next(); |
73 | 0 | 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 | } | |
81 | 0 | JMenu undirectedMenu = new JMenu("Create Undirected Edge"); |
82 | 0 | popup.add(undirectedMenu); |
83 | 0 | for(Iterator iterator=picked.iterator(); iterator.hasNext(); ) { |
84 | 0 | final Vertex other = (Vertex)iterator.next(); |
85 | 0 | 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 | } | |
94 | 0 | 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 | }}); | |
100 | 0 | } else if(edge != null) { |
101 | 0 | 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 { | |
108 | 0 | 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 | } | |
125 | 0 | if(popup.getComponentCount() > 0) { |
126 | 0 | popup.show(vv, e.getX(), e.getY()); |
127 | } | |
128 | } | |
129 | 0 | } |
130 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |