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

Last change on this file since 12504 was 12338, checked in by michael2402, 7 years ago

Sonar squid:S1125 - simplify condition.

  • Property svn:eol-style set to native
File size: 9.9 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 Main.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 int macMouseMask = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
172 boolean macMovement = e.getModifiersEx() == macMouseMask;
173 boolean allowedMode = !Main.map.mapModeSelect.equals(Main.map.mapMode)
174 || SelectAction.Mode.SELECT.equals(Main.map.mapModeSelect.getMode());
175 allowMovement |= macMovement && allowedMode;
176 }
177 if (allowMovement) {
178 doMoveForDrag(e);
179 } else {
180 endMovement();
181 }
182 }
183
184 private void doMoveForDrag(MouseEvent e) {
185 if (!movementInProgress()) {
186 startMovement(e);
187 }
188 EastNorth center = nc.getCenter();
189 EastNorth mouseCenter = nc.getEastNorth(e.getX(), e.getY());
190 nc.zoomTo(mousePosMoveStart.getEastNorth().add(center).subtract(mouseCenter));
191 }
192
193 /**
194 * Start the movement, if it was the 3rd button (right button).
195 */
196 @Override
197 public void mousePressed(MouseEvent e) {
198 int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON2_DOWN_MASK;
199 int macMouseMask = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
200 if ((e.getButton() == MouseEvent.BUTTON3 && (e.getModifiersEx() & offMask) == 0) ||
201 (Main.isPlatformOsx() && e.getModifiersEx() == macMouseMask)) {
202 startMovement(e);
203 }
204 }
205
206 /**
207 * Change the cursor back to it's pre-move cursor.
208 */
209 @Override
210 public void mouseReleased(MouseEvent e) {
211 if (e.getButton() == MouseEvent.BUTTON3 || (Main.isPlatformOsx() && e.getButton() == MouseEvent.BUTTON1)) {
212 endMovement();
213 }
214 }
215
216 /**
217 * Start movement by setting a new cursor and remember the current mouse
218 * position.
219 * @param e The mouse event that leat to the movement from.
220 */
221 private void startMovement(MouseEvent e) {
222 if (movementInProgress()) {
223 return;
224 }
225 mousePosMoveStart = nc.getState().getForView(e.getX(), e.getY());
226 nc.setNewCursor(Cursor.MOVE_CURSOR, this);
227 }
228
229 /**
230 * End the movement. Setting back the cursor and clear the movement variables
231 */
232 private void endMovement() {
233 if (!movementInProgress()) {
234 return;
235 }
236 nc.resetCursor(this);
237 mousePosMoveStart = null;
238 }
239
240 /**
241 * Zoom the map by 1/5th of current zoom per wheel-delta.
242 * @param e The wheel event.
243 */
244 @Override
245 public void mouseWheelMoved(MouseWheelEvent e) {
246 int rotation = PROP_ZOOM_REVERSE_WHEEL.get() ? -e.getWheelRotation() : e.getWheelRotation();
247 nc.zoomManyTimes(e.getX(), e.getY(), rotation);
248 }
249
250 /**
251 * Emulates dragging on Mac OSX.
252 */
253 @Override
254 public void mouseMoved(MouseEvent e) {
255 if (!movementInProgress()) {
256 return;
257 }
258 // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired.
259 // Is only the selected mouse button pressed?
260 if (Main.isPlatformOsx()) {
261 if (e.getModifiersEx() == MouseEvent.CTRL_DOWN_MASK) {
262 doMoveForDrag(e);
263 } else {
264 endMovement();
265 }
266 }
267 }
268
269 @Override
270 public void destroy() {
271 for (Pair<ZoomerAction, Shortcut> shortcut : registeredShortcuts) {
272 Main.unregisterActionShortcut(shortcut.a, shortcut.b);
273 }
274 }
275}
Note: See TracBrowser for help on using the repository browser.