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.MouseEvent;
16 import java.awt.event.MouseListener;
17 import java.awt.event.MouseMotionListener;
18 import java.awt.geom.Point2D;
19
20 import edu.uci.ics.jung.visualization.Layer;
21 import edu.uci.ics.jung.visualization.VisualizationViewer;
22 import edu.uci.ics.jung.visualization.transform.LensTransformer;
23 import edu.uci.ics.jung.visualization.transform.MutableTransformer;
24
25
26
27
28
29
30
31
32
33
34
35 public class LensTranslatingGraphMousePlugin extends TranslatingGraphMousePlugin
36 implements MouseListener, MouseMotionListener {
37
38 protected boolean dragOnLens;
39 protected boolean dragOnEdge;
40 protected double edgeOffset;
41
42
43
44 public LensTranslatingGraphMousePlugin() {
45 this(MouseEvent.BUTTON1_MASK);
46 }
47
48
49
50
51
52 public LensTranslatingGraphMousePlugin(int modifiers) {
53 super(modifiers);
54 }
55
56
57
58
59
60
61
62 public void mousePressed(MouseEvent e) {
63 VisualizationViewer vv = (VisualizationViewer)e.getSource();
64 MutableTransformer vt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
65 if(vt instanceof LensTransformer) {
66 vt = ((LensTransformer)vt).getDelegate();
67 }
68 Point2D p = vt.inverseTransform(e.getPoint());
69 boolean accepted = checkModifiers(e);
70 if(accepted) {
71 vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
72 testViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT), p);
73 testViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
74 vv.repaint();
75 }
76 super.mousePressed(e);
77 }
78
79
80
81
82
83
84 private void setViewCenter(MutableTransformer transformer, Point2D point) {
85 if(transformer instanceof LensTransformer) {
86 LensTransformer ht =
87 (LensTransformer)transformer;
88 ht.setViewCenter(point);
89 }
90 }
91
92
93
94
95
96
97 private void setViewRadius(MutableTransformer transformer, Point2D point) {
98 if(transformer instanceof LensTransformer) {
99 LensTransformer ht =
100 (LensTransformer)transformer;
101 double distanceFromCenter = ht.getDistanceFromCenter(point);
102 ht.setViewRadius(distanceFromCenter+edgeOffset);
103 }
104 }
105
106
107
108
109
110
111 private void testViewCenter(MutableTransformer transformer, Point2D point) {
112 if(transformer instanceof LensTransformer) {
113 LensTransformer ht =
114 (LensTransformer)transformer;
115 double distanceFromCenter = ht.getDistanceFromCenter(point);
116 if(distanceFromCenter < 10) {
117 ht.setViewCenter(point);
118 dragOnLens = true;
119 } else if(Math.abs(distanceFromCenter - ht.getViewRadius()) < 10) {
120 edgeOffset = ht.getViewRadius() - distanceFromCenter;
121 ht.setViewRadius(distanceFromCenter+edgeOffset);
122 dragOnEdge = true;
123 }
124 }
125 }
126
127
128
129
130
131 public void mouseReleased(MouseEvent e) {
132 super.mouseReleased(e);
133 dragOnLens = false;
134 dragOnEdge = false;
135 edgeOffset = 0;
136 }
137
138
139
140
141
142
143 public void mouseDragged(MouseEvent e) {
144 VisualizationViewer vv = (VisualizationViewer)e.getSource();
145 MutableTransformer vt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW);
146 if(vt instanceof LensTransformer) {
147 vt = ((LensTransformer)vt).getDelegate();
148 }
149 Point2D p = vt.inverseTransform(e.getPoint());
150 boolean accepted = checkModifiers(e);
151
152 if(accepted ) {
153 MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
154 vv.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
155 if(dragOnLens) {
156 setViewCenter(modelTransformer, p);
157 setViewCenter(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
158 e.consume();
159 vv.repaint();
160
161 } else if(dragOnEdge) {
162
163 setViewRadius(modelTransformer, p);
164 setViewRadius(vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW), p);
165 e.consume();
166 vv.repaint();
167
168 } else {
169
170 MutableTransformer mt = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
171 Point2D iq = vt.inverseTransform(down);
172 iq = mt.inverseTransform(iq);
173 Point2D ip = vt.inverseTransform(e.getPoint());
174 ip = mt.inverseTransform(ip);
175 float dx = (float) (ip.getX()-iq.getX());
176 float dy = (float) (ip.getY()-iq.getY());
177
178 modelTransformer.translate(dx, dy);
179 down.x = e.getX();
180 down.y = e.getY();
181 }
182 }
183 }
184 }