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.MutableTransformer;
23
24
25
26
27
28
29
30
31
32
33 public class TranslatingGraphMousePlugin extends AbstractGraphMousePlugin
34 implements MouseListener, MouseMotionListener {
35
36
37
38 public TranslatingGraphMousePlugin() {
39 this(MouseEvent.BUTTON1_MASK);
40 }
41
42
43
44
45
46 public TranslatingGraphMousePlugin(int modifiers) {
47 super(modifiers);
48 this.cursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
49 }
50
51
52
53
54
55
56
57 public void mousePressed(MouseEvent e) {
58 VisualizationViewer vv = (VisualizationViewer)e.getSource();
59 boolean accepted = checkModifiers(e);
60 down = e.getPoint();
61 if(accepted) {
62 vv.setCursor(cursor);
63 }
64 }
65
66
67
68
69
70 public void mouseReleased(MouseEvent e) {
71 VisualizationViewer vv = (VisualizationViewer)e.getSource();
72 down = null;
73 vv.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
74 }
75
76
77
78
79
80
81 public void mouseDragged(MouseEvent e) {
82 VisualizationViewer vv = (VisualizationViewer)e.getSource();
83 boolean accepted = checkModifiers(e);
84 if(accepted) {
85 MutableTransformer modelTransformer = vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT);
86 vv.setCursor(cursor);
87 try {
88 Point2D q = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(down);
89 Point2D p = vv.getRenderContext().getMultiLayerTransformer().inverseTransform(e.getPoint());
90 float dx = (float) (p.getX()-q.getX());
91 float dy = (float) (p.getY()-q.getY());
92
93 modelTransformer.translate(dx, dy);
94 down.x = e.getX();
95 down.y = e.getY();
96 } catch(RuntimeException ex) {
97 System.err.println("down = "+down+", e = "+e);
98 throw ex;
99 }
100
101 e.consume();
102 vv.repaint();
103 }
104 }
105
106 public void mouseClicked(MouseEvent e) {
107
108
109 }
110
111 public void mouseEntered(MouseEvent e) {
112
113
114 }
115
116 public void mouseExited(MouseEvent e) {
117
118
119 }
120
121 public void mouseMoved(MouseEvent e) {
122
123
124 }
125 }