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

Last change on this file since 2198 was 1939, checked in by Gubaer, 15 years ago

applied #3165: patch by dmuecke: moving map on a macbook withouth external mouse

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