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