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

Last change on this file since 1724 was 1722, checked in by stoecker, 15 years ago

Large rework in projection handling - now allows only switching and more specific projections
TODO:

  • allow subprojections (i.e. settings for projections)
  • setup preferences for subprojections
  • better support of the new projection depending world bounds (how to handle valid data outside of world)
  • do not allow to zoom out of the world - zoom should stop when whole world is displayed
  • fix Lambert and SwissGrid to handle new OutOfWorld style and subprojections
  • fix new UTM projection
  • handle layers with fixed projection on projection change
  • allow easier projection switching (e.g. in menu)

NOTE:
This checkin very likely will cause problems. Please report or fix them. Older plugins may have trouble. The SVN plugins
have been fixed but may have problems nevertheless. This is a BIG change, but will make JOSMs internal structure much cleaner
and reduce lots of projection related problems.

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