1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.BorderLayout;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.util.Optional;
|
---|
12 |
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 | import javax.swing.JPanel;
|
---|
16 | import javax.swing.event.DocumentEvent;
|
---|
17 | import javax.swing.event.DocumentListener;
|
---|
18 |
|
---|
19 | import org.openstreetmap.josm.Main;
|
---|
20 | import org.openstreetmap.josm.data.Bounds;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
25 | import org.openstreetmap.josm.gui.MapView;
|
---|
26 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
28 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
29 | import org.openstreetmap.josm.tools.GBC;
|
---|
30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
31 | import org.openstreetmap.josm.tools.Logging;
|
---|
32 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
33 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Allows to jump to a specific location.
|
---|
37 | * @since 2575
|
---|
38 | */
|
---|
39 | public class JumpToAction extends JosmAction {
|
---|
40 |
|
---|
41 | private final JosmTextField url = new JosmTextField();
|
---|
42 | private final JosmTextField lat = new JosmTextField();
|
---|
43 | private final JosmTextField lon = new JosmTextField();
|
---|
44 | private final JosmTextField zm = new JosmTextField();
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new {@code JumpToAction}.
|
---|
48 | */
|
---|
49 | public JumpToAction() {
|
---|
50 | super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
|
---|
51 | Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
|
---|
52 | KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
|
---|
53 | putValue("help", ht("/Action/JumpToPosition"));
|
---|
54 | }
|
---|
55 |
|
---|
56 | static class JumpToPositionDialog extends ExtendedDialog {
|
---|
57 | JumpToPositionDialog(String[] buttons, JPanel panel) {
|
---|
58 | super(Main.parent, tr("Jump to Position"), buttons);
|
---|
59 | setContent(panel);
|
---|
60 | setCancelButton(2);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | class OsmURLListener implements DocumentListener {
|
---|
65 | @Override
|
---|
66 | public void changedUpdate(DocumentEvent e) {
|
---|
67 | parseURL();
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void insertUpdate(DocumentEvent e) {
|
---|
72 | parseURL();
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public void removeUpdate(DocumentEvent e) {
|
---|
77 | parseURL();
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | class OsmLonLatListener implements DocumentListener {
|
---|
82 | @Override
|
---|
83 | public void changedUpdate(DocumentEvent e) {
|
---|
84 | updateUrl(false);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public void insertUpdate(DocumentEvent e) {
|
---|
89 | updateUrl(false);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public void removeUpdate(DocumentEvent e) {
|
---|
94 | updateUrl(false);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Displays the "Jump to" dialog.
|
---|
100 | */
|
---|
101 | public void showJumpToDialog() {
|
---|
102 | if (!MainApplication.isDisplayingMapView()) {
|
---|
103 | return;
|
---|
104 | }
|
---|
105 | MapView mv = MainApplication.getMap().mapView;
|
---|
106 |
|
---|
107 | final Optional<Bounds> boundsFromClipboard = Optional
|
---|
108 | .ofNullable(ClipboardUtils.getClipboardStringContent())
|
---|
109 | .map(OsmUrlToBounds::parse);
|
---|
110 | if (boundsFromClipboard.isPresent()) {
|
---|
111 | setBounds(boundsFromClipboard.get());
|
---|
112 | } else {
|
---|
113 | setBounds(mv.getState().getViewArea().getCornerBounds());
|
---|
114 | }
|
---|
115 | updateUrl(true);
|
---|
116 |
|
---|
117 | JPanel panel = new JPanel(new BorderLayout());
|
---|
118 | panel.add(new JLabel("<html>"
|
---|
119 | + tr("Enter Lat/Lon to jump to position.")
|
---|
120 | + "<br>"
|
---|
121 | + tr("You can also paste an URL from www.openstreetmap.org")
|
---|
122 | + "<br>"
|
---|
123 | + "</html>"),
|
---|
124 | BorderLayout.NORTH);
|
---|
125 |
|
---|
126 | OsmLonLatListener x = new OsmLonLatListener();
|
---|
127 | lat.getDocument().addDocumentListener(x);
|
---|
128 | lon.getDocument().addDocumentListener(x);
|
---|
129 | zm.getDocument().addDocumentListener(x);
|
---|
130 | url.getDocument().addDocumentListener(new OsmURLListener());
|
---|
131 |
|
---|
132 | SelectAllOnFocusGainedDecorator.decorate(lat);
|
---|
133 | SelectAllOnFocusGainedDecorator.decorate(lon);
|
---|
134 | SelectAllOnFocusGainedDecorator.decorate(zm);
|
---|
135 | SelectAllOnFocusGainedDecorator.decorate(url);
|
---|
136 |
|
---|
137 | JPanel p = new JPanel(new GridBagLayout());
|
---|
138 | panel.add(p, BorderLayout.NORTH);
|
---|
139 |
|
---|
140 | p.add(new JLabel(tr("Latitude")), GBC.eol());
|
---|
141 | p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
142 |
|
---|
143 | p.add(new JLabel(tr("Longitude")), GBC.eol());
|
---|
144 | p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
145 |
|
---|
146 | p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
|
---|
147 | p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
148 |
|
---|
149 | p.add(new JLabel(tr("URL")), GBC.eol());
|
---|
150 | p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
151 |
|
---|
152 | String[] buttons = {tr("Jump there"), tr("Cancel")};
|
---|
153 | LatLon ll = null;
|
---|
154 | double zoomLvl = 100;
|
---|
155 | while (ll == null) {
|
---|
156 | final int option = new JumpToPositionDialog(buttons, panel).showDialog().getValue();
|
---|
157 |
|
---|
158 | if (option != 1) return;
|
---|
159 | try {
|
---|
160 | zoomLvl = Double.parseDouble(zm.getText());
|
---|
161 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
162 | } catch (NumberFormatException ex) {
|
---|
163 | try {
|
---|
164 | ll = LatLonParser.parse(lat.getText() + "; " + lon.getText());
|
---|
165 | } catch (IllegalArgumentException ex2) {
|
---|
166 | JOptionPane.showMessageDialog(Main.parent,
|
---|
167 | tr("Could not parse Latitude, Longitude or Zoom. Please check."),
|
---|
168 | tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | double zoomFactor = 1/ mv.getDist100Pixel();
|
---|
174 | mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
|
---|
175 | }
|
---|
176 |
|
---|
177 | private void parseURL() {
|
---|
178 | if (!url.hasFocus()) return;
|
---|
179 | String urlText = url.getText();
|
---|
180 | Bounds b = OsmUrlToBounds.parse(urlText);
|
---|
181 | setBounds(b);
|
---|
182 | }
|
---|
183 |
|
---|
184 | private void setBounds(Bounds b) {
|
---|
185 | if (b != null) {
|
---|
186 | final LatLon center = b.getCenter();
|
---|
187 | lat.setText(Double.toString(center.lat()));
|
---|
188 | lon.setText(Double.toString(center.lon()));
|
---|
189 | zm.setText(Double.toString(OsmUrlToBounds.getZoom(b)));
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | private void updateUrl(boolean force) {
|
---|
194 | if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
|
---|
195 | try {
|
---|
196 | double dlat = Double.parseDouble(lat.getText());
|
---|
197 | double dlon = Double.parseDouble(lon.getText());
|
---|
198 | double zoomLvl = Double.parseDouble(zm.getText());
|
---|
199 | url.setText(OsmUrlToBounds.getURL(dlat, dlon, (int) zoomLvl));
|
---|
200 | } catch (NumberFormatException e) {
|
---|
201 | Logging.debug(e.getMessage());
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | @Override
|
---|
206 | public void actionPerformed(ActionEvent e) {
|
---|
207 | showJumpToDialog();
|
---|
208 | }
|
---|
209 |
|
---|
210 | @Override
|
---|
211 | protected void updateEnabledState() {
|
---|
212 | setEnabled(MainApplication.isDisplayingMapView());
|
---|
213 | }
|
---|
214 |
|
---|
215 | @Override
|
---|
216 | protected void installAdapters() {
|
---|
217 | super.installAdapters();
|
---|
218 | // make this action listen to mapframe change events
|
---|
219 | MainApplication.addMapFrameListener((o, n) -> updateEnabledState());
|
---|
220 | }
|
---|
221 | }
|
---|