source: josm/trunk/src/org/openstreetmap/josm/gui/MapMover.java@ 12682

Last change on this file since 12682 was 12639, checked in by Don-vip, 7 years ago

see #15182 - deprecate shortcut handling and mapframe listener methods in Main. Replacement: same methods in gui.MainApplication

  • Property svn:eol-style set to native
File size: 10.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Cursor;
7import java.awt.Point;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.awt.event.MouseAdapter;
11import java.awt.event.MouseEvent;
12import java.awt.event.MouseWheelEvent;
13import java.util.ArrayList;
14import java.util.Optional;
15
16import javax.swing.AbstractAction;
17
18import org.openstreetmap.gui.jmapviewer.JMapViewer;
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.actions.mapmode.SelectAction;
21import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
22import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
23import org.openstreetmap.josm.data.coor.EastNorth;
24import org.openstreetmap.josm.data.preferences.BooleanProperty;
25import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
26import org.openstreetmap.josm.tools.Destroyable;
27import org.openstreetmap.josm.tools.Pair;
28import org.openstreetmap.josm.tools.Shortcut;
29
30/**
31 * Enables moving of the map by holding down the right mouse button and drag
32 * the mouse. Also, enables zooming by the mouse wheel.
33 *
34 * @author imi
35 */
36public class MapMover extends MouseAdapter implements Destroyable {
37
38 /**
39 * Zoom wheel is reversed.
40 */
41 public static final BooleanProperty PROP_ZOOM_REVERSE_WHEEL = new BooleanProperty("zoom.reverse-wheel", false);
42
43 static {
44 new JMapViewerUpdater();
45 }
46
47 private static class JMapViewerUpdater implements PreferenceChangedListener {
48
49 JMapViewerUpdater() {
50 Main.pref.addPreferenceChangeListener(this);
51 updateJMapViewer();
52 }
53
54 @Override
55 public void preferenceChanged(PreferenceChangeEvent e) {
56 if (MapMover.PROP_ZOOM_REVERSE_WHEEL.getKey().equals(e.getKey())) {
57 updateJMapViewer();
58 }
59 }
60
61 private static void updateJMapViewer() {
62 JMapViewer.zoomReverseWheel = MapMover.PROP_ZOOM_REVERSE_WHEEL.get();
63 }
64 }
65
66 private final class ZoomerAction extends AbstractAction {
67 private final String action;
68
69 ZoomerAction(String action) {
70 this(action, "MapMover.Zoomer." + action);
71 }
72
73 ZoomerAction(String action, String name) {
74 this.action = action;
75 putValue(NAME, name);
76 }
77
78 @Override
79 public void actionPerformed(ActionEvent e) {
80 if (".".equals(action) || ",".equals(action)) {
81 Point mouse = Optional.ofNullable(nc.getMousePosition()).orElseGet(
82 () -> new Point((int) nc.getBounds().getCenterX(), (int) nc.getBounds().getCenterY()));
83 mouseWheelMoved(new MouseWheelEvent(nc, e.getID(), e.getWhen(), e.getModifiers(), mouse.x, mouse.y, 0, false,
84 MouseWheelEvent.WHEEL_UNIT_SCROLL, 1, ",".equals(action) ? -1 : 1));
85 } else {
86 EastNorth center = nc.getCenter();
87 EastNorth newcenter = nc.getEastNorth(nc.getWidth()/2+nc.getWidth()/5, nc.getHeight()/2+nc.getHeight()/5);
88 switch(action) {
89 case "left":
90 nc.zoomTo(new EastNorth(2*center.east()-newcenter.east(), center.north()));
91 break;
92 case "right":
93 nc.zoomTo(new EastNorth(newcenter.east(), center.north()));
94 break;
95 case "up":
96 nc.zoomTo(new EastNorth(center.east(), 2*center.north()-newcenter.north()));
97 break;
98 case "down":
99 nc.zoomTo(new EastNorth(center.east(), newcenter.north()));
100 break;
101 default: // Do nothing
102 }
103 }
104 }
105 }
106
107 /**
108 * The point in the map that was the under the mouse point
109 * when moving around started.
110 *
111 * This is <code>null</code> if movement is not active
112 */
113 private MapViewPoint mousePosMoveStart;
114
115 /**
116 * The map to move around.
117 */
118 private final NavigatableComponent nc;
119
120 private final ArrayList<Pair<ZoomerAction, Shortcut>> registeredShortcuts = new ArrayList<>();
121
122 /**
123 * Constructs a new {@code MapMover}.
124 * @param navComp the navigatable component
125 * @since 11713
126 */
127 public MapMover(NavigatableComponent navComp) {
128 this.nc = navComp;
129 nc.addMouseListener(this);
130 nc.addMouseMotionListener(this);
131 nc.addMouseWheelListener(this);
132
133 registerActionShortcut(new ZoomerAction("right"),
134 Shortcut.registerShortcut("system:movefocusright", tr("Map: {0}", tr("Move right")), KeyEvent.VK_RIGHT, Shortcut.CTRL));
135
136 registerActionShortcut(new ZoomerAction("left"),
137 Shortcut.registerShortcut("system:movefocusleft", tr("Map: {0}", tr("Move left")), KeyEvent.VK_LEFT, Shortcut.CTRL));
138
139 registerActionShortcut(new ZoomerAction("up"),
140 Shortcut.registerShortcut("system:movefocusup", tr("Map: {0}", tr("Move up")), KeyEvent.VK_UP, Shortcut.CTRL));
141 registerActionShortcut(new ZoomerAction("down"),
142 Shortcut.registerShortcut("system:movefocusdown", tr("Map: {0}", tr("Move down")), KeyEvent.VK_DOWN, Shortcut.CTRL));
143
144 // see #10592 - Disable these alternate shortcuts on OS X because of conflict with system shortcut
145 if (!Main.isPlatformOsx()) {
146 registerActionShortcut(new ZoomerAction(",", "MapMover.Zoomer.in"),
147 Shortcut.registerShortcut("view:zoominalternate", tr("Map: {0}", tr("Zoom in")), KeyEvent.VK_COMMA, Shortcut.CTRL));
148
149 registerActionShortcut(new ZoomerAction(".", "MapMover.Zoomer.out"),
150 Shortcut.registerShortcut("view:zoomoutalternate", tr("Map: {0}", tr("Zoom out")), KeyEvent.VK_PERIOD, Shortcut.CTRL));
151 }
152 }
153
154 private void registerActionShortcut(ZoomerAction action, Shortcut shortcut) {
155 MainApplication.registerActionShortcut(action, shortcut);
156 registeredShortcuts.add(new Pair<>(action, shortcut));
157 }
158
159 private boolean movementInProgress() {
160 return mousePosMoveStart != null;
161 }
162
163 /**
164 * If the right (and only the right) mouse button is pressed, move the map.
165 */
166 @Override
167 public void mouseDragged(MouseEvent e) {
168 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
169 boolean allowMovement = (e.getModifiersEx() & (MouseEvent.BUTTON3_DOWN_MASK | offMask)) == MouseEvent.BUTTON3_DOWN_MASK;
170 if (Main.isPlatformOsx()) {
171 MapFrame map = MainApplication.getMap();
172 int macMouseMask = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
173 boolean macMovement = e.getModifiersEx() == macMouseMask;
174 boolean allowedMode = !map.mapModeSelect.equals(map.mapMode)
175 || SelectAction.Mode.SELECT.equals(map.mapModeSelect.getMode());
176 allowMovement |= macMovement && allowedMode;
177 }
178 if (allowMovement) {
179 doMoveForDrag(e);
180 } else {
181 endMovement();
182 }
183 }
184
185 private void doMoveForDrag(MouseEvent e) {
186 if (!movementInProgress()) {
187 startMovement(e);
188 }
189 EastNorth center = nc.getCenter();
190 EastNorth mouseCenter = nc.getEastNorth(e.getX(), e.getY());
191 nc.zoomTo(mousePosMoveStart.getEastNorth().add(center).subtract(mouseCenter));
192 }
193
194 /**
195 * Start the movement, if it was the 3rd button (right button).
196 */
197 @Override
198 public void mousePressed(MouseEvent e) {
199 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
200 int macMouseMask = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
201 if ((e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0) ||
202 (Main.isPlatformOsx() && e.getModifiersEx() == macMouseMask)) {
203 startMovement(e);
204 }
205 }
206
207 /**
208 * Change the cursor back to it's pre-move cursor.
209 */
210 @Override
211 public void mouseReleased(MouseEvent e) {
212 if (e.getButton() == MouseEvent.BUTTON3 || (Main.isPlatformOsx() && e.getButton() == MouseEvent.BUTTON1)) {
213 endMovement();
214 }
215 }
216
217 /**
218 * Start movement by setting a new cursor and remember the current mouse
219 * position.
220 * @param e The mouse event that leat to the movement from.
221 */
222 private void startMovement(MouseEvent e) {
223 if (movementInProgress()) {
224 return;
225 }
226 mousePosMoveStart = nc.getState().getForView(e.getX(), e.getY());
227 nc.setNewCursor(Cursor.MOVE_CURSOR, this);
228 }
229
230 /**
231 * End the movement. Setting back the cursor and clear the movement variables
232 */
233 private void endMovement() {
234 if (!movementInProgress()) {
235 return;
236 }
237 nc.resetCursor(this);
238 mousePosMoveStart = null;
239 }
240
241 /**
242 * Zoom the map by 1/5th of current zoom per wheel-delta.
243 * @param e The wheel event.
244 */
245 @Override
246 public void mouseWheelMoved(MouseWheelEvent e) {
247 int rotation = PROP_ZOOM_REVERSE_WHEEL.get() ? -e.getWheelRotation() : e.getWheelRotation();
248 nc.zoomManyTimes(e.getX(), e.getY(), rotation);
249 }
250
251 /**
252 * Emulates dragging on Mac OSX.
253 */
254 @Override
255 public void mouseMoved(MouseEvent e) {
256 if (!movementInProgress()) {
257 return;
258 }
259 // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired.
260 // Is only the selected mouse button pressed?
261 if (Main.isPlatformOsx()) {
262 if (e.getModifiersEx() == MouseEvent.CTRL_DOWN_MASK) {
263 doMoveForDrag(e);
264 } else {
265 endMovement();
266 }
267 }
268 }
269
270 @Override
271 public void destroy() {
272 for (Pair<ZoomerAction, Shortcut> shortcut : registeredShortcuts) {
273 MainApplication.unregisterActionShortcut(shortcut.a, shortcut.b);
274 }
275 }
276}
Note: See TracBrowser for help on using the repository browser.