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