1 | // License: GPL. For details, see Readme.txt file.
|
---|
2 | package org.openstreetmap.gui.jmapviewer;
|
---|
3 |
|
---|
4 | import java.awt.Point;
|
---|
5 | import java.awt.event.MouseEvent;
|
---|
6 | import java.awt.event.MouseListener;
|
---|
7 | import java.awt.event.MouseMotionListener;
|
---|
8 | import java.awt.event.MouseWheelEvent;
|
---|
9 | import java.awt.event.MouseWheelListener;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Default map controller which implements map moving by pressing the right
|
---|
13 | * mouse button and zooming by double click or by mouse wheel.
|
---|
14 | *
|
---|
15 | * @author Jan Peter Stotz
|
---|
16 | *
|
---|
17 | */
|
---|
18 | public class DefaultMapController extends JMapController implements MouseListener, MouseMotionListener,
|
---|
19 | MouseWheelListener {
|
---|
20 |
|
---|
21 | private static final int MOUSE_BUTTONS_MASK = MouseEvent.BUTTON3_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK
|
---|
22 | | MouseEvent.BUTTON2_DOWN_MASK;
|
---|
23 |
|
---|
24 | private static final int MAC_MOUSE_BUTTON3_MASK = MouseEvent.CTRL_DOWN_MASK | MouseEvent.BUTTON1_DOWN_MASK;
|
---|
25 |
|
---|
26 | private Point lastDragPoint;
|
---|
27 |
|
---|
28 | private boolean isMoving;
|
---|
29 |
|
---|
30 | private boolean movementEnabled = true;
|
---|
31 |
|
---|
32 | private int movementMouseButton = MouseEvent.BUTTON3;
|
---|
33 | private int movementMouseButtonMask = MouseEvent.BUTTON3_DOWN_MASK;
|
---|
34 |
|
---|
35 | private boolean wheelZoomEnabled = true;
|
---|
36 | private boolean doubleClickZoomEnabled = true;
|
---|
37 |
|
---|
38 | public DefaultMapController(JMapViewer map) {
|
---|
39 | super(map);
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public void mouseDragged(MouseEvent e) {
|
---|
44 | if (!movementEnabled || !isMoving)
|
---|
45 | return;
|
---|
46 | // Is only the selected mouse button pressed?
|
---|
47 | if ((e.getModifiersEx() & MOUSE_BUTTONS_MASK) == movementMouseButtonMask
|
---|
48 | || isPlatformOsx() && e.getModifiersEx() == MAC_MOUSE_BUTTON3_MASK) {
|
---|
49 | Point p = e.getPoint();
|
---|
50 | if (lastDragPoint != null) {
|
---|
51 | int diffx = lastDragPoint.x - p.x;
|
---|
52 | int diffy = lastDragPoint.y - p.y;
|
---|
53 | map.moveMap(diffx, diffy);
|
---|
54 | }
|
---|
55 | lastDragPoint = p;
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | @Override
|
---|
60 | public void mouseClicked(MouseEvent e) {
|
---|
61 | if (doubleClickZoomEnabled && e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
|
---|
62 | map.zoomIn(e.getPoint());
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void mousePressed(MouseEvent e) {
|
---|
68 | if (e.getButton() == movementMouseButton || isPlatformOsx() && e.getModifiersEx() == MAC_MOUSE_BUTTON3_MASK) {
|
---|
69 | lastDragPoint = null;
|
---|
70 | isMoving = true;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | @Override
|
---|
75 | public void mouseReleased(MouseEvent e) {
|
---|
76 | if (e.getButton() == movementMouseButton || isPlatformOsx() && e.getButton() == MouseEvent.BUTTON1) {
|
---|
77 | lastDragPoint = null;
|
---|
78 | isMoving = false;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public void mouseWheelMoved(MouseWheelEvent e) {
|
---|
84 | if (wheelZoomEnabled) {
|
---|
85 | map.setZoom(map.getZoom() - e.getWheelRotation(), e.getPoint());
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public boolean isMovementEnabled() {
|
---|
90 | return movementEnabled;
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Enables or disables that the map pane can be moved using the mouse.
|
---|
95 | *
|
---|
96 | * @param movementEnabled {@code true} to allow the map pane to be moved using the mouse
|
---|
97 | */
|
---|
98 | public void setMovementEnabled(boolean movementEnabled) {
|
---|
99 | this.movementEnabled = movementEnabled;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public int getMovementMouseButton() {
|
---|
103 | return movementMouseButton;
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Sets the mouse button that is used for moving the map. Possible values are:
|
---|
108 | * <ul>
|
---|
109 | * <li>{@link MouseEvent#BUTTON1} (left mouse button)</li>
|
---|
110 | * <li>{@link MouseEvent#BUTTON2} (middle mouse button)</li>
|
---|
111 | * <li>{@link MouseEvent#BUTTON3} (right mouse button)</li>
|
---|
112 | * </ul>
|
---|
113 | *
|
---|
114 | * @param movementMouseButton the mouse button that is used for moving the map
|
---|
115 | */
|
---|
116 | public void setMovementMouseButton(int movementMouseButton) {
|
---|
117 | this.movementMouseButton = movementMouseButton;
|
---|
118 | switch (movementMouseButton) {
|
---|
119 | case MouseEvent.BUTTON1:
|
---|
120 | movementMouseButtonMask = MouseEvent.BUTTON1_DOWN_MASK;
|
---|
121 | break;
|
---|
122 | case MouseEvent.BUTTON2:
|
---|
123 | movementMouseButtonMask = MouseEvent.BUTTON2_DOWN_MASK;
|
---|
124 | break;
|
---|
125 | case MouseEvent.BUTTON3:
|
---|
126 | movementMouseButtonMask = MouseEvent.BUTTON3_DOWN_MASK;
|
---|
127 | break;
|
---|
128 | default:
|
---|
129 | throw new RuntimeException("Unsupported button");
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | public boolean isWheelZoomEnabled() {
|
---|
134 | return wheelZoomEnabled;
|
---|
135 | }
|
---|
136 |
|
---|
137 | public void setWheelZoomEnabled(boolean wheelZoomEnabled) {
|
---|
138 | this.wheelZoomEnabled = wheelZoomEnabled;
|
---|
139 | }
|
---|
140 |
|
---|
141 | public boolean isDoubleClickZoomEnabled() {
|
---|
142 | return doubleClickZoomEnabled;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public void setDoubleClickZoomEnabled(boolean doubleClickZoomEnabled) {
|
---|
146 | this.doubleClickZoomEnabled = doubleClickZoomEnabled;
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void mouseEntered(MouseEvent e) {
|
---|
151 | }
|
---|
152 |
|
---|
153 | @Override
|
---|
154 | public void mouseExited(MouseEvent e) {
|
---|
155 | }
|
---|
156 |
|
---|
157 | @Override
|
---|
158 | public void mouseMoved(MouseEvent e) {
|
---|
159 | // Mac OSX simulates with ctrl + mouse 1 the second mouse button hence no dragging events get fired.
|
---|
160 | //
|
---|
161 | if (isPlatformOsx()) {
|
---|
162 | if (!movementEnabled || !isMoving)
|
---|
163 | return;
|
---|
164 | // Is only the selected mouse button pressed?
|
---|
165 | if (e.getModifiersEx() == MouseEvent.CTRL_DOWN_MASK) {
|
---|
166 | Point p = e.getPoint();
|
---|
167 | if (lastDragPoint != null) {
|
---|
168 | int diffx = lastDragPoint.x - p.x;
|
---|
169 | int diffy = lastDragPoint.y - p.y;
|
---|
170 | map.moveMap(diffx, diffy);
|
---|
171 | }
|
---|
172 | lastDragPoint = p;
|
---|
173 | }
|
---|
174 |
|
---|
175 | }
|
---|
176 |
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Replies true if we are currently running on OSX
|
---|
181 | *
|
---|
182 | * @return true if we are currently running on OSX
|
---|
183 | */
|
---|
184 | public static boolean isPlatformOsx() {
|
---|
185 | String os = System.getProperty("os.name");
|
---|
186 | return os != null && os.toLowerCase().startsWith("mac os x");
|
---|
187 | }
|
---|
188 | }
|
---|