1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.AWTEvent;
|
---|
7 | import java.awt.Cursor;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
10 | import java.awt.Toolkit;
|
---|
11 | import java.awt.event.AWTEventListener;
|
---|
12 | import java.awt.event.ActionEvent;
|
---|
13 | import java.awt.event.FocusEvent;
|
---|
14 | import java.awt.event.FocusListener;
|
---|
15 | import java.awt.event.KeyEvent;
|
---|
16 | import java.awt.event.MouseEvent;
|
---|
17 | import java.util.Formatter;
|
---|
18 | import java.util.Locale;
|
---|
19 |
|
---|
20 | import javax.swing.JLabel;
|
---|
21 | import javax.swing.JPanel;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.actions.mapmode.MapMode;
|
---|
24 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
25 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
26 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
---|
27 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
28 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
29 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
30 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
31 | import org.openstreetmap.josm.gui.MapView;
|
---|
32 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
33 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
34 | import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
|
---|
35 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
38 | import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider;
|
---|
39 | import org.openstreetmap.josm.tools.Logging;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Adjust the position of an imagery layer.
|
---|
43 | * @since 3715
|
---|
44 | */
|
---|
45 | public class ImageryAdjustAction extends MapMode implements AWTEventListener {
|
---|
46 | private static volatile ImageryOffsetDialog offsetDialog;
|
---|
47 | private static Cursor cursor = ImageProvider.getCursor("normal", "move");
|
---|
48 |
|
---|
49 | private OffsetBookmark old;
|
---|
50 | private OffsetBookmark tempOffset;
|
---|
51 | private EastNorth prevEastNorth;
|
---|
52 | private transient AbstractTileSourceLayer<?> layer;
|
---|
53 | private MapMode oldMapMode;
|
---|
54 | private boolean exitingMode;
|
---|
55 | private boolean restoreOldMode;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Constructs a new {@code ImageryAdjustAction} for the given layer.
|
---|
59 | * @param layer The imagery layer
|
---|
60 | */
|
---|
61 | public ImageryAdjustAction(AbstractTileSourceLayer<?> layer) {
|
---|
62 | super(tr("New offset"), "adjustimg", tr("Adjust the position of this imagery layer"), cursor);
|
---|
63 | putValue("toolbar", Boolean.FALSE);
|
---|
64 | this.layer = layer;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public void enterMode() {
|
---|
69 | super.enterMode();
|
---|
70 | if (layer == null)
|
---|
71 | return;
|
---|
72 | if (!layer.isVisible()) {
|
---|
73 | layer.setVisible(true);
|
---|
74 | }
|
---|
75 | old = layer.getDisplaySettings().getOffsetBookmark();
|
---|
76 | EastNorth curOff = old == null ? EastNorth.ZERO : old.getDisplacement(ProjectionRegistry.getProjection());
|
---|
77 | LatLon center;
|
---|
78 | if (MainApplication.isDisplayingMapView()) {
|
---|
79 | center = ProjectionRegistry.getProjection().eastNorth2latlon(MainApplication.getMap().mapView.getCenter());
|
---|
80 | } else {
|
---|
81 | center = LatLon.ZERO;
|
---|
82 | }
|
---|
83 | tempOffset = new OffsetBookmark(
|
---|
84 | ProjectionRegistry.getProjection().toCode(),
|
---|
85 | layer.getInfo().getId(),
|
---|
86 | layer.getInfo().getName(),
|
---|
87 | null,
|
---|
88 | curOff, center);
|
---|
89 | layer.getDisplaySettings().setOffsetBookmark(tempOffset);
|
---|
90 | addListeners();
|
---|
91 | showOffsetDialog(new ImageryOffsetDialog());
|
---|
92 | }
|
---|
93 |
|
---|
94 | private static void showOffsetDialog(ImageryOffsetDialog dlg) {
|
---|
95 | offsetDialog = dlg;
|
---|
96 | offsetDialog.setVisible(true);
|
---|
97 | }
|
---|
98 |
|
---|
99 | private static void hideOffsetDialog() {
|
---|
100 | offsetDialog.setVisible(false);
|
---|
101 | offsetDialog = null;
|
---|
102 | }
|
---|
103 |
|
---|
104 | protected void addListeners() {
|
---|
105 | MapView mapView = MainApplication.getMap().mapView;
|
---|
106 | mapView.addMouseListener(this);
|
---|
107 | mapView.addMouseMotionListener(this);
|
---|
108 | try {
|
---|
109 | Toolkit.getDefaultToolkit().addAWTEventListener(this, AWTEvent.KEY_EVENT_MASK);
|
---|
110 | } catch (SecurityException ex) {
|
---|
111 | Logging.error(ex);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | public void exitMode() {
|
---|
117 | // do not restore old mode here - this is called when the new mode is already known.
|
---|
118 | restoreOldMode = false;
|
---|
119 | doExitMode();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void exitModeAndRestoreOldMode() {
|
---|
123 | restoreOldMode = true;
|
---|
124 | doExitMode();
|
---|
125 | restoreOldMode = false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void doExitMode() {
|
---|
129 | exitingMode = true;
|
---|
130 | try {
|
---|
131 | super.exitMode();
|
---|
132 | } catch (IllegalArgumentException e) {
|
---|
133 | Logging.trace(e);
|
---|
134 | }
|
---|
135 | if (offsetDialog != null) {
|
---|
136 | if (layer != null) {
|
---|
137 | layer.getDisplaySettings().setOffsetBookmark(old);
|
---|
138 | }
|
---|
139 | hideOffsetDialog();
|
---|
140 | }
|
---|
141 | removeListeners();
|
---|
142 | exitingMode = false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | protected void removeListeners() {
|
---|
146 | try {
|
---|
147 | Toolkit.getDefaultToolkit().removeAWTEventListener(this);
|
---|
148 | } catch (SecurityException ex) {
|
---|
149 | Logging.error(ex);
|
---|
150 | }
|
---|
151 | if (MainApplication.isDisplayingMapView()) {
|
---|
152 | MapFrame map = MainApplication.getMap();
|
---|
153 | map.mapView.removeMouseMotionListener(this);
|
---|
154 | map.mapView.removeMouseListener(this);
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public void eventDispatched(AWTEvent event) {
|
---|
160 | if (!(event instanceof KeyEvent)
|
---|
161 | || (event.getID() != KeyEvent.KEY_PRESSED)
|
---|
162 | || (layer == null)
|
---|
163 | || (offsetDialog != null && offsetDialog.areFieldsInFocus())) {
|
---|
164 | return;
|
---|
165 | }
|
---|
166 | KeyEvent kev = (KeyEvent) event;
|
---|
167 | int dx = 0;
|
---|
168 | int dy = 0;
|
---|
169 | switch (kev.getKeyCode()) {
|
---|
170 | case KeyEvent.VK_UP : dy = +1; break;
|
---|
171 | case KeyEvent.VK_DOWN : dy = -1; break;
|
---|
172 | case KeyEvent.VK_LEFT : dx = -1; break;
|
---|
173 | case KeyEvent.VK_RIGHT : dx = +1; break;
|
---|
174 | case KeyEvent.VK_ESCAPE:
|
---|
175 | if (offsetDialog != null) {
|
---|
176 | restoreOldMode = true;
|
---|
177 | offsetDialog.setVisible(false);
|
---|
178 | return;
|
---|
179 | }
|
---|
180 | break;
|
---|
181 | default: // Do nothing
|
---|
182 | }
|
---|
183 | if (dx != 0 || dy != 0) {
|
---|
184 | double ppd = layer.getPPD();
|
---|
185 | EastNorth d = tempOffset.getDisplacement().add(new EastNorth(dx / ppd, dy / ppd));
|
---|
186 | tempOffset.setDisplacement(d);
|
---|
187 | layer.getDisplaySettings().setOffsetBookmark(tempOffset);
|
---|
188 | if (offsetDialog != null) {
|
---|
189 | offsetDialog.updateOffset();
|
---|
190 | }
|
---|
191 | if (Logging.isDebugEnabled()) {
|
---|
192 | Logging.debug("{0} consuming event {1}", getClass().getName(), kev);
|
---|
193 | }
|
---|
194 | kev.consume();
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | @Override
|
---|
199 | public void mousePressed(MouseEvent e) {
|
---|
200 | if (e.getButton() != MouseEvent.BUTTON1)
|
---|
201 | return;
|
---|
202 |
|
---|
203 | if (layer.isVisible()) {
|
---|
204 | requestFocusInMapView();
|
---|
205 | MapView mapView = MainApplication.getMap().mapView;
|
---|
206 | prevEastNorth = mapView.getEastNorth(e.getX(), e.getY());
|
---|
207 | mapView.setNewCursor(Cursor.MOVE_CURSOR, this);
|
---|
208 | }
|
---|
209 | }
|
---|
210 |
|
---|
211 | @Override
|
---|
212 | public void mouseDragged(MouseEvent e) {
|
---|
213 | if (layer == null || prevEastNorth == null) return;
|
---|
214 | EastNorth eastNorth = MainApplication.getMap().mapView.getEastNorth(e.getX(), e.getY());
|
---|
215 | EastNorth d = tempOffset.getDisplacement().add(eastNorth).subtract(prevEastNorth);
|
---|
216 | tempOffset.setDisplacement(d);
|
---|
217 | layer.getDisplaySettings().setOffsetBookmark(tempOffset);
|
---|
218 | if (offsetDialog != null) {
|
---|
219 | offsetDialog.updateOffset();
|
---|
220 | }
|
---|
221 | prevEastNorth = eastNorth;
|
---|
222 | }
|
---|
223 |
|
---|
224 | @Override
|
---|
225 | public void mouseReleased(MouseEvent e) {
|
---|
226 | MapView mapView = MainApplication.getMap().mapView;
|
---|
227 | mapView.repaint();
|
---|
228 | mapView.resetCursor(this);
|
---|
229 | prevEastNorth = null;
|
---|
230 | }
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void actionPerformed(ActionEvent e) {
|
---|
234 | MapFrame map = MainApplication.getMap();
|
---|
235 | if (offsetDialog != null || layer == null || map == null)
|
---|
236 | return;
|
---|
237 | oldMapMode = map.mapMode;
|
---|
238 | super.actionPerformed(e);
|
---|
239 | }
|
---|
240 |
|
---|
241 | private class ImageryOffsetDialog extends ExtendedDialog implements FocusListener {
|
---|
242 | private final JosmTextField tOffset = new JosmTextField();
|
---|
243 | private final JosmTextField tBookmarkName = new JosmTextField();
|
---|
244 | private boolean ignoreListener;
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Constructs a new {@code ImageryOffsetDialog}.
|
---|
248 | */
|
---|
249 | ImageryOffsetDialog() {
|
---|
250 | super(MainApplication.getMainFrame(),
|
---|
251 | tr("Adjust imagery offset"),
|
---|
252 | new String[] {tr("OK"), tr("Cancel")},
|
---|
253 | false, false); // Do not dispose on close, so HIDE_ON_CLOSE remains the default behaviour and setVisible is called
|
---|
254 | setButtonIcons("ok", "cancel");
|
---|
255 | contentInsets = new Insets(10, 15, 5, 15);
|
---|
256 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
257 | pnl.add(new JMultilineLabel(tr("Use arrow keys or drag the imagery layer with mouse to adjust the imagery offset.\n" +
|
---|
258 | "You can also enter east and north offset in the {0} coordinates.\n" +
|
---|
259 | "If you want to save the offset as bookmark, enter the bookmark name below",
|
---|
260 | ProjectionRegistry.getProjection().toString())), GBC.eop());
|
---|
261 | pnl.add(new JLabel(tr("Offset:")), GBC.std());
|
---|
262 | pnl.add(tOffset, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
|
---|
263 | pnl.add(new JLabel(tr("Bookmark name: ")), GBC.std());
|
---|
264 | pnl.add(tBookmarkName, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
265 | tOffset.setColumns(16);
|
---|
266 | updateOffsetIntl();
|
---|
267 | tOffset.addFocusListener(this);
|
---|
268 | setContent(pnl);
|
---|
269 | setupDialog();
|
---|
270 | setRememberWindowGeometry(getClass().getName() + ".geometry",
|
---|
271 | WindowGeometry.centerInWindow(MainApplication.getMainFrame(), getSize()));
|
---|
272 | }
|
---|
273 |
|
---|
274 | private boolean areFieldsInFocus() {
|
---|
275 | return tOffset.hasFocus();
|
---|
276 | }
|
---|
277 |
|
---|
278 | @Override
|
---|
279 | public void focusGained(FocusEvent e) {
|
---|
280 | // Do nothing
|
---|
281 | }
|
---|
282 |
|
---|
283 | @Override
|
---|
284 | public void focusLost(FocusEvent e) {
|
---|
285 | if (ignoreListener) return;
|
---|
286 | String ostr = tOffset.getText();
|
---|
287 | int semicolon = ostr.indexOf(';');
|
---|
288 | if (layer != null && semicolon >= 0 && semicolon + 1 < ostr.length()) {
|
---|
289 | try {
|
---|
290 | String easting = ostr.substring(0, semicolon).trim();
|
---|
291 | String northing = ostr.substring(semicolon + 1).trim();
|
---|
292 | double dx = JosmDecimalFormatSymbolsProvider.parseDouble(easting);
|
---|
293 | double dy = JosmDecimalFormatSymbolsProvider.parseDouble(northing);
|
---|
294 | tempOffset.setDisplacement(new EastNorth(dx, dy));
|
---|
295 | layer.getDisplaySettings().setOffsetBookmark(tempOffset);
|
---|
296 | } catch (NumberFormatException nfe) {
|
---|
297 | // we repaint offset numbers in any case
|
---|
298 | Logging.trace(nfe);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | updateOffsetIntl();
|
---|
302 | if (layer != null) {
|
---|
303 | layer.invalidate();
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | private void updateOffset() {
|
---|
308 | ignoreListener = true;
|
---|
309 | updateOffsetIntl();
|
---|
310 | ignoreListener = false;
|
---|
311 | }
|
---|
312 |
|
---|
313 | private void updateOffsetIntl() {
|
---|
314 | if (layer != null) {
|
---|
315 | // Support projections with very small numbers (e.g. 4326)
|
---|
316 | int precision = ProjectionRegistry.getProjection().getDefaultZoomInPPD() >= 1.0 ? 2 : 7;
|
---|
317 | // US locale to force decimal separator to be '.'
|
---|
318 | try (Formatter us = new Formatter(Locale.US)) {
|
---|
319 | EastNorth displacement = layer.getDisplaySettings().getDisplacement();
|
---|
320 | tOffset.setText(us.format(new StringBuilder()
|
---|
321 | .append("%1.").append(precision).append("f; %1.").append(precision).append('f').toString(),
|
---|
322 | displacement.east(), displacement.north()).toString());
|
---|
323 | }
|
---|
324 | }
|
---|
325 | }
|
---|
326 |
|
---|
327 | private boolean confirmOverwriteBookmark() {
|
---|
328 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
329 | MainApplication.getMainFrame(),
|
---|
330 | tr("Overwrite"),
|
---|
331 | tr("Overwrite"), tr("Cancel")
|
---|
332 | ) { {
|
---|
333 | contentInsets = new Insets(10, 15, 10, 15);
|
---|
334 | } };
|
---|
335 | dialog.setContent(tr("Offset bookmark already exists. Overwrite?"));
|
---|
336 | dialog.setButtonIcons("ok", "cancel");
|
---|
337 | dialog.setupDialog();
|
---|
338 | dialog.setVisible(true);
|
---|
339 | return dialog.getValue() == 1;
|
---|
340 | }
|
---|
341 |
|
---|
342 | @Override
|
---|
343 | protected void buttonAction(int buttonIndex, ActionEvent evt) {
|
---|
344 | restoreOldMode = true;
|
---|
345 | if (buttonIndex == 0 && tBookmarkName.getText() != null && !tBookmarkName.getText().isEmpty() &&
|
---|
346 | OffsetBookmark.getBookmarkByName(layer, tBookmarkName.getText()) != null &&
|
---|
347 | !confirmOverwriteBookmark()) {
|
---|
348 | return;
|
---|
349 | }
|
---|
350 | super.buttonAction(buttonIndex, evt);
|
---|
351 | }
|
---|
352 |
|
---|
353 | @Override
|
---|
354 | public void setVisible(boolean visible) {
|
---|
355 | super.setVisible(visible);
|
---|
356 | if (visible)
|
---|
357 | return;
|
---|
358 | ignoreListener = true;
|
---|
359 | offsetDialog = null;
|
---|
360 | if (layer != null) {
|
---|
361 | if (getValue() != 1) {
|
---|
362 | layer.getDisplaySettings().setOffsetBookmark(old);
|
---|
363 | } else if (tBookmarkName.getText() != null && !tBookmarkName.getText().isEmpty()) {
|
---|
364 | OffsetBookmark.bookmarkOffset(tBookmarkName.getText(), layer);
|
---|
365 | }
|
---|
366 | }
|
---|
367 | MainApplication.getMenu().imageryMenu.refreshOffsetMenu();
|
---|
368 | restoreMapModeState();
|
---|
369 | }
|
---|
370 |
|
---|
371 | private void restoreMapModeState() {
|
---|
372 | MapFrame map = MainApplication.getMap();
|
---|
373 | if (map == null)
|
---|
374 | return;
|
---|
375 | if (oldMapMode != null) {
|
---|
376 | if (restoreOldMode || (!exitingMode && getValue() == ExtendedDialog.DialogClosedOtherwise)) {
|
---|
377 | map.selectMapMode(oldMapMode);
|
---|
378 | }
|
---|
379 | oldMapMode = null;
|
---|
380 | } else if (!exitingMode && !map.selectSelectTool(false)) {
|
---|
381 | exitModeAndRestoreOldMode();
|
---|
382 | map.mapMode = null;
|
---|
383 | }
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | @Override
|
---|
388 | public void destroy() {
|
---|
389 | super.destroy();
|
---|
390 | removeListeners();
|
---|
391 | this.layer = null;
|
---|
392 | this.oldMapMode = null;
|
---|
393 | }
|
---|
394 | }
|
---|