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

Last change on this file since 10152 was 10152, checked in by Don-vip, 8 years ago

sonar - squid:S1939 - Extensions and implementations should not be redundant

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