1
2
3
4
5
6
7
8
9
10
11
12 package edu.uci.ics.jung.visualization.control;
13
14 import java.awt.event.MouseEvent;
15 import java.awt.event.MouseWheelEvent;
16 import java.awt.event.MouseWheelListener;
17 import java.awt.geom.Point2D;
18
19 import edu.uci.ics.jung.visualization.VisualizationViewer;
20
21
22
23
24
25
26
27
28 public class ScalingGraphMousePlugin extends AbstractGraphMousePlugin
29 implements MouseWheelListener {
30
31
32
33
34 protected float in = 1.1f;
35
36
37
38 protected float out = 1/1.1f;
39
40
41
42
43 protected boolean zoomAtMouse = true;
44
45
46
47
48 protected ScalingControl scaler;
49
50 public ScalingGraphMousePlugin(ScalingControl scaler, int modifiers) {
51 this(scaler, modifiers, 1.1f, 1/1.1f);
52 }
53
54 public ScalingGraphMousePlugin(ScalingControl scaler, int modifiers, float in, float out) {
55 super(modifiers);
56 this.scaler = scaler;
57 this.in = in;
58 this.out = out;
59 }
60
61
62
63 public void setZoomAtMouse(boolean zoomAtMouse) {
64 this.zoomAtMouse = zoomAtMouse;
65 }
66
67 public boolean checkModifiers(MouseEvent e) {
68 return e.getModifiers() == modifiers || (e.getModifiers() & modifiers) != 0;
69 }
70
71
72
73
74
75 public void mouseWheelMoved(MouseWheelEvent e) {
76 boolean accepted = checkModifiers(e);
77 if(accepted == true) {
78 VisualizationViewer vv = (VisualizationViewer)e.getSource();
79 Point2D mouse = e.getPoint();
80 Point2D center = vv.getCenter();
81 int amount = e.getWheelRotation();
82 if(zoomAtMouse) {
83 if(amount > 0) {
84 scaler.scale(vv, in, mouse);
85 } else if(amount < 0) {
86 scaler.scale(vv, out, mouse);
87 }
88 } else {
89 if(amount > 0) {
90 scaler.scale(vv, in, center);
91 } else if(amount < 0) {
92 scaler.scale(vv, out, center);
93 }
94 }
95 e.consume();
96 vv.repaint();
97 }
98 }
99
100
101
102 public float getIn() {
103 return in;
104 }
105
106
107
108 public void setIn(float in) {
109 this.in = in;
110 }
111
112
113
114 public float getOut() {
115 return out;
116 }
117
118
119
120 public void setOut(float out) {
121 this.out = out;
122 }
123
124 public ScalingControl getScaler() {
125 return scaler;
126 }
127
128 public void setScaler(ScalingControl scaler) {
129 this.scaler = scaler;
130 }
131 }