1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.visualization.control;
13
14 import java.awt.Cursor;
15 import java.awt.event.InputEvent;
16 import java.awt.event.MouseEvent;
17 import java.awt.event.MouseListener;
18 import java.awt.geom.Point2D;
19
20 import javax.swing.JOptionPane;
21
22 import com.google.common.base.Function;
23
24 import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
25 import edu.uci.ics.jung.algorithms.layout.Layout;
26 import edu.uci.ics.jung.algorithms.util.MapSettableTransformer;
27 import edu.uci.ics.jung.visualization.Layer;
28 import edu.uci.ics.jung.visualization.VisualizationViewer;
29
30
31
32
33
34
35 public class LabelEditingGraphMousePlugin<V, E> extends AbstractGraphMousePlugin
36 implements MouseListener {
37
38
39
40
41 protected V vertex;
42
43
44
45
46 protected E edge;
47
48
49
50
51 public LabelEditingGraphMousePlugin() {
52 this(InputEvent.BUTTON1_MASK);
53 }
54
55
56
57
58
59 public LabelEditingGraphMousePlugin(int selectionModifiers) {
60 super(selectionModifiers);
61 this.cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
62 }
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 @SuppressWarnings("unchecked")
82 public void mouseClicked(MouseEvent e) {
83 if (e.getModifiers() == modifiers && e.getClickCount() == 2) {
84 VisualizationViewer<V,E> vv = (VisualizationViewer<V, E>)e.getSource();
85 GraphElementAccessor<V,E> pickSupport = vv.getPickSupport();
86 if (pickSupport != null) {
87 Function<? super V,String> vs = vv.getRenderContext().getVertexLabelTransformer();
88 if (vs instanceof MapSettableTransformer) {
89 MapSettableTransformer<? super V, String> mst =
90 (MapSettableTransformer<? super V, String>)vs;
91 Layout<V,E> layout = vv.getGraphLayout();
92
93 Point2D p = e.getPoint();
94
95 V vertex = pickSupport.getVertex(layout, p.getX(), p.getY());
96 if(vertex != null) {
97 String newLabel = vs.apply(vertex);
98 newLabel = JOptionPane.showInputDialog("New Vertex Label for "+vertex);
99 if(newLabel != null) {
100 mst.set(vertex, newLabel);
101 vv.repaint();
102 }
103 return;
104 }
105 }
106 Function<? super E,String> es = vv.getRenderContext().getEdgeLabelTransformer();
107 if (es instanceof MapSettableTransformer) {
108 MapSettableTransformer<? super E, String> mst =
109 (MapSettableTransformer<? super E, String>)es;
110 Layout<V,E> layout = vv.getGraphLayout();
111
112 Point2D p = e.getPoint();
113
114 Point2D ip = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(Layer.VIEW, p);
115 E edge = pickSupport.getEdge(layout, ip.getX(), ip.getY());
116 if(edge != null) {
117 String newLabel = JOptionPane.showInputDialog("New Edge Label for "+edge);
118 if(newLabel != null) {
119 mst.set(edge, newLabel);
120 vv.repaint();
121 }
122 return;
123 }
124 }
125 }
126 e.consume();
127 }
128 }
129
130
131
132
133
134
135
136 public void mouseReleased(MouseEvent e) {
137 }
138
139
140
141
142
143
144
145
146
147 public void mousePressed(MouseEvent e) {
148 }
149
150 public void mouseEntered(MouseEvent e) {
151 }
152
153 public void mouseExited(MouseEvent e) {
154 }
155
156 }