1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.KeyEvent;
|
---|
7 | import java.beans.PropertyChangeEvent;
|
---|
8 | import java.beans.PropertyChangeListener;
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
13 | import org.openstreetmap.josm.gui.MapView;
|
---|
14 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
15 | import org.openstreetmap.josm.gui.bbox.BBoxChooser;
|
---|
16 | import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
|
---|
17 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A small map of the current edit location implemented as {@link ToggleDialog}.
|
---|
21 | */
|
---|
22 | public class MinimapDialog extends ToggleDialog implements NavigatableComponent.ZoomChangeListener, PropertyChangeListener {
|
---|
23 |
|
---|
24 | private SlippyMapBBoxChooser slippyMap;
|
---|
25 | private boolean skipEvents;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code MinimapDialog}.
|
---|
29 | */
|
---|
30 | public MinimapDialog() {
|
---|
31 | super(tr("Mini map"), "minimap", tr("Displays a small map of the current edit location"),
|
---|
32 | Shortcut.registerShortcut("subwindow:minimap", tr("Windows: {0}", tr("Mini map")),
|
---|
33 | KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), 150);
|
---|
34 | }
|
---|
35 |
|
---|
36 | private synchronized void initialize() {
|
---|
37 | if (slippyMap != null) {
|
---|
38 | return;
|
---|
39 | }
|
---|
40 | slippyMap = new SlippyMapBBoxChooser();
|
---|
41 | createLayout(slippyMap, false, Collections.emptyList());
|
---|
42 | slippyMap.setSizeButtonVisible(false);
|
---|
43 | slippyMap.addPropertyChangeListener(BBoxChooser.BBOX_PROP, this);
|
---|
44 | MainApplication.getLayerManager().addLayerChangeListener(slippyMap);
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public void showDialog() {
|
---|
49 | initialize();
|
---|
50 | NavigatableComponent.addZoomChangeListener(this);
|
---|
51 | super.showDialog();
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void hideDialog() {
|
---|
56 | NavigatableComponent.removeZoomChangeListener(this);
|
---|
57 | super.hideDialog();
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void zoomChanged() {
|
---|
62 | if (!skipEvents && MainApplication.isDisplayingMapView()) {
|
---|
63 | MapView mv = MainApplication.getMap().mapView;
|
---|
64 | final Bounds currentBounds = new Bounds(
|
---|
65 | mv.getLatLon(0, mv.getHeight()),
|
---|
66 | mv.getLatLon(mv.getWidth(), 0)
|
---|
67 | );
|
---|
68 | skipEvents = true;
|
---|
69 | slippyMap.setBoundingBox(currentBounds);
|
---|
70 | slippyMap.zoomOut(); // to give a better overview
|
---|
71 | skipEvents = false;
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public void propertyChange(PropertyChangeEvent evt) {
|
---|
77 | if (!skipEvents) {
|
---|
78 | skipEvents = true;
|
---|
79 | MainApplication.getMap().mapView.zoomTo(slippyMap.getBoundingBox());
|
---|
80 | skipEvents = false;
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|