1
2
3
4
5
6
7
8
9
10 package edu.uci.ics.jung.visualization;
11
12 import java.awt.Dimension;
13 import java.awt.event.KeyListener;
14 import java.awt.event.MouseAdapter;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionListener;
18 import java.awt.event.MouseWheelListener;
19 import java.awt.geom.Point2D;
20
21 import javax.swing.ToolTipManager;
22
23 import org.apache.commons.collections15.Transformer;
24
25 import edu.uci.ics.jung.algorithms.layout.Layout;
26 import edu.uci.ics.jung.visualization.control.GraphMouseListener;
27 import edu.uci.ics.jung.visualization.control.MouseListenerTranslator;
28
29
30
31
32
33
34
35
36 @SuppressWarnings("serial")
37 public class VisualizationViewer<V,E> extends BasicVisualizationServer<V,E> {
38
39 protected Transformer<V,String> vertexToolTipTransformer;
40 protected Transformer<E,String> edgeToolTipTransformer;
41 protected Transformer<MouseEvent,String> mouseEventToolTipTransformer;
42
43
44
45
46
47 protected GraphMouse graphMouse;
48
49 protected MouseListener requestFocusListener = new MouseAdapter() {
50 public void mouseClicked(MouseEvent e) {
51 requestFocusInWindow();
52 }
53 };
54
55
56
57
58
59
60
61
62 public VisualizationViewer(Layout<V,E> layout) {
63 this(new DefaultVisualizationModel<V,E>(layout));
64 }
65
66
67
68
69
70
71
72
73 public VisualizationViewer(Layout<V,E> layout, Dimension preferredSize) {
74 this(new DefaultVisualizationModel<V,E>(layout, preferredSize), preferredSize);
75 }
76
77
78
79
80
81
82
83 public VisualizationViewer(VisualizationModel<V,E> model) {
84 this(model, new Dimension(600,600));
85 }
86
87
88
89
90
91
92
93 @SuppressWarnings("unchecked")
94 public VisualizationViewer(VisualizationModel<V,E> model,
95 Dimension preferredSize) {
96 super(model, preferredSize);
97 setFocusable(true);
98 addMouseListener(requestFocusListener);
99 }
100
101
102
103
104
105
106
107 public void setGraphMouse(GraphMouse graphMouse) {
108 this.graphMouse = graphMouse;
109 MouseListener[] ml = getMouseListeners();
110 for(int i=0; i<ml.length; i++) {
111 if(ml[i] instanceof GraphMouse) {
112 removeMouseListener(ml[i]);
113 }
114 }
115 MouseMotionListener[] mml = getMouseMotionListeners();
116 for(int i=0; i<mml.length; i++) {
117 if(mml[i] instanceof GraphMouse) {
118 removeMouseMotionListener(mml[i]);
119 }
120 }
121 MouseWheelListener[] mwl = getMouseWheelListeners();
122 for(int i=0; i<mwl.length; i++) {
123 if(mwl[i] instanceof GraphMouse) {
124 removeMouseWheelListener(mwl[i]);
125 }
126 }
127 addMouseListener(graphMouse);
128 addMouseMotionListener(graphMouse);
129 addMouseWheelListener(graphMouse);
130 }
131
132
133
134
135 public GraphMouse getGraphMouse() {
136 return graphMouse;
137 }
138
139
140
141
142
143
144 public void addGraphMouseListener( GraphMouseListener<V> gel ) {
145 addMouseListener( new MouseListenerTranslator<V,E>( gel, this ));
146 }
147
148
149
150
151
152 @Override
153 public synchronized void addKeyListener(KeyListener l) {
154 super.addKeyListener(l);
155
156
157 }
158
159
160
161
162 public void setEdgeToolTipTransformer(
163 Transformer<E, String> edgeToolTipTransformer) {
164 this.edgeToolTipTransformer = edgeToolTipTransformer;
165 ToolTipManager.sharedInstance().registerComponent(this);
166 }
167
168
169
170
171 public void setMouseEventToolTipTransformer(
172 Transformer<MouseEvent, String> mouseEventToolTipTransformer) {
173 this.mouseEventToolTipTransformer = mouseEventToolTipTransformer;
174 ToolTipManager.sharedInstance().registerComponent(this);
175 }
176
177
178
179
180 public void setVertexToolTipTransformer(
181 Transformer<V, String> vertexToolTipTransformer) {
182 this.vertexToolTipTransformer = vertexToolTipTransformer;
183 ToolTipManager.sharedInstance().registerComponent(this);
184 }
185
186
187
188
189 public String getToolTipText(MouseEvent event) {
190 Layout<V,E> layout = getGraphLayout();
191 Point2D p = null;
192 if(vertexToolTipTransformer != null) {
193 p = event.getPoint();
194
195 V vertex = getPickSupport().getVertex(layout, p.getX(), p.getY());
196 if(vertex != null) {
197 return vertexToolTipTransformer.transform(vertex);
198 }
199 }
200 if(edgeToolTipTransformer != null) {
201 if(p == null) p = renderContext.getMultiLayerTransformer().inverseTransform(Layer.VIEW, event.getPoint());
202 E edge = getPickSupport().getEdge(layout, p.getX(), p.getY());
203 if(edge != null) {
204 return edgeToolTipTransformer.transform(edge);
205 }
206 }
207 if(mouseEventToolTipTransformer != null) {
208 return mouseEventToolTipTransformer.transform(event);
209 }
210 return super.getToolTipText(event);
211 }
212
213
214
215
216
217 public interface GraphMouse extends MouseListener, MouseMotionListener, MouseWheelListener {}
218
219 }