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

Last change on this file since 7122 was 7012, checked in by Don-vip, 10 years ago

see #8465 - use String switch/case where applicable

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