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