1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.editor;
19
20 import org.eclipse.draw2d.geometry.Dimension;
21 import org.eclipse.draw2d.geometry.Point;
22 import org.eclipse.gef.EditPartViewer;
23 import org.eclipse.gef.MouseWheelHandler;
24 import org.eclipse.gef.editparts.ZoomManager;
25 import org.eclipse.swt.widgets.Event;
26
27
28
29
30
31
32 public final class ExtendedMouseWheelZoomHandler implements MouseWheelHandler {
33
34
35
36
37 private static final class SingletonHolder {
38 private static final ExtendedMouseWheelZoomHandler INSTANCE = new ExtendedMouseWheelZoomHandler();
39 }
40
41
42
43
44
45
46
47
48
49 private double width;
50
51 private double height;
52
53
54
55
56
57
58
59
60
61
62
63 private Point zoomMgrLocation = new Point();
64
65
66
67
68
69
70 public static ExtendedMouseWheelZoomHandler getInstance() {
71 return SingletonHolder.INSTANCE;
72 }
73
74
75
76
77 private ExtendedMouseWheelZoomHandler() {
78 }
79
80
81
82
83
84
85
86
87
88 public void handleMouseWheel(Event event, EditPartViewer viewer) {
89 ZoomManager zoomMgr = (ZoomManager)viewer.getProperty(ZoomManager.class.toString());
90
91
92
93 if (!zoomMgr.canZoomIn() && event.count > 0) {
94 return;
95 }
96 if (!zoomMgr.canZoomOut() && event.count < 0) {
97 return;
98 }
99
100 if (zoomMgr != null) {
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 Point cursorLocation = new Point(event.x, event.y);
123
124 zoomMgr.getScalableFigure().translateToRelative(cursorLocation);
125
126 zoomMgrLocation.x = cursorLocation.x;
127 zoomMgrLocation.y = cursorLocation.y;
128
129
130
131
132 if (event.count > 0) {
133 zoomMgr.zoomIn();
134 } else {
135 zoomMgr.zoomOut();
136 }
137
138
139
140
141 Dimension scalableSize = zoomMgr.getScalableFigure().getSize();
142
143 zoomMgrLocation.x *= scalableSize.width / width;
144 zoomMgrLocation.y *= scalableSize.height / height;
145
146 width = scalableSize.width;
147 height = scalableSize.height;
148
149 Dimension viewportSize = zoomMgr.getViewport().getSize();
150
151 zoomMgrLocation.x -= viewportSize.width / 2;
152 zoomMgrLocation.y -= viewportSize.height / 2;
153
154 zoomMgr.setViewLocation(zoomMgrLocation);
155
156 zoomMgrLocation.x += viewportSize.width / 2;
157 zoomMgrLocation.y += viewportSize.height / 2;
158
159 event.doit = false;
160 }
161 }
162
163 }