[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2575] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[7812] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[2575] | 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;
|
---|
[11301] | 11 | import java.util.Optional;
|
---|
[6398] | 12 |
|
---|
[2575] | 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;
|
---|
[12792] | 22 | import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
|
---|
[11300] | 23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
[12630] | 24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[2575] | 25 | import org.openstreetmap.josm.gui.MapView;
|
---|
[11301] | 26 | import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
|
---|
[6398] | 27 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[11300] | 28 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
---|
[2575] | 29 | import org.openstreetmap.josm.tools.GBC;
|
---|
[7693] | 30 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[12620] | 31 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2575] | 32 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
| 33 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 34 |
|
---|
[6394] | 35 | /**
|
---|
| 36 | * Allows to jump to a specific location.
|
---|
| 37 | * @since 2575
|
---|
| 38 | */
|
---|
[5965] | 39 | public class JumpToAction extends JosmAction {
|
---|
[7081] | 40 |
|
---|
[11343] | 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 |
|
---|
[5886] | 46 | /**
|
---|
| 47 | * Constructs a new {@code JumpToAction}.
|
---|
| 48 | */
|
---|
[2575] | 49 | public JumpToAction() {
|
---|
[7693] | 50 | super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
|
---|
[6398] | 51 | Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
|
---|
| 52 | KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
|
---|
[7812] | 53 | putValue("help", ht("/Action/JumpToPosition"));
|
---|
[2575] | 54 | }
|
---|
| 55 |
|
---|
[11343] | 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 | }
|
---|
[2575] | 63 |
|
---|
[7081] | 64 | class OsmURLListener implements DocumentListener {
|
---|
[8510] | 65 | @Override
|
---|
[8513] | 66 | public void changedUpdate(DocumentEvent e) {
|
---|
| 67 | parseURL();
|
---|
| 68 | }
|
---|
[8510] | 69 |
|
---|
| 70 | @Override
|
---|
[8513] | 71 | public void insertUpdate(DocumentEvent e) {
|
---|
| 72 | parseURL();
|
---|
| 73 | }
|
---|
[8510] | 74 |
|
---|
| 75 | @Override
|
---|
[8513] | 76 | public void removeUpdate(DocumentEvent e) {
|
---|
| 77 | parseURL();
|
---|
| 78 | }
|
---|
[7081] | 79 | }
|
---|
| 80 |
|
---|
| 81 | class OsmLonLatListener implements DocumentListener {
|
---|
[8510] | 82 | @Override
|
---|
[8513] | 83 | public void changedUpdate(DocumentEvent e) {
|
---|
| 84 | updateUrl(false);
|
---|
| 85 | }
|
---|
[8510] | 86 |
|
---|
| 87 | @Override
|
---|
[8513] | 88 | public void insertUpdate(DocumentEvent e) {
|
---|
| 89 | updateUrl(false);
|
---|
| 90 | }
|
---|
[8510] | 91 |
|
---|
| 92 | @Override
|
---|
[8513] | 93 | public void removeUpdate(DocumentEvent e) {
|
---|
| 94 | updateUrl(false);
|
---|
| 95 | }
|
---|
[7081] | 96 | }
|
---|
| 97 |
|
---|
[6394] | 98 | /**
|
---|
| 99 | * Displays the "Jump to" dialog.
|
---|
| 100 | */
|
---|
[2575] | 101 | public void showJumpToDialog() {
|
---|
[12630] | 102 | if (!MainApplication.isDisplayingMapView()) {
|
---|
[6394] | 103 | return;
|
---|
| 104 | }
|
---|
[12630] | 105 | MapView mv = MainApplication.getMap().mapView;
|
---|
[2575] | 106 |
|
---|
[11301] | 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 | }
|
---|
[2575] | 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 |
|
---|
[6246] | 126 | OsmLonLatListener x = new OsmLonLatListener();
|
---|
[2575] | 127 | lat.getDocument().addDocumentListener(x);
|
---|
| 128 | lon.getDocument().addDocumentListener(x);
|
---|
| 129 | zm.getDocument().addDocumentListener(x);
|
---|
[6246] | 130 | url.getDocument().addDocumentListener(new OsmURLListener());
|
---|
[2575] | 131 |
|
---|
[11300] | 132 | SelectAllOnFocusGainedDecorator.decorate(lat);
|
---|
| 133 | SelectAllOnFocusGainedDecorator.decorate(lon);
|
---|
| 134 | SelectAllOnFocusGainedDecorator.decorate(zm);
|
---|
| 135 | SelectAllOnFocusGainedDecorator.decorate(url);
|
---|
| 136 |
|
---|
[2575] | 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 |
|
---|
[11300] | 152 | String[] buttons = {tr("Jump there"), tr("Cancel")};
|
---|
[2575] | 153 | LatLon ll = null;
|
---|
| 154 | double zoomLvl = 100;
|
---|
[8510] | 155 | while (ll == null) {
|
---|
[11343] | 156 | final int option = new JumpToPositionDialog(buttons, panel).showDialog().getValue();
|
---|
[2575] | 157 |
|
---|
[11300] | 158 | if (option != 1) return;
|
---|
[2575] | 159 | try {
|
---|
| 160 | zoomLvl = Double.parseDouble(zm.getText());
|
---|
| 161 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
[5780] | 162 | } catch (NumberFormatException ex) {
|
---|
[8670] | 163 | try {
|
---|
[12792] | 164 | ll = LatLonParser.parse(lat.getText() + "; " + lon.getText());
|
---|
[8670] | 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 | }
|
---|
[2575] | 170 | }
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[11301] | 173 | double zoomFactor = 1/ mv.getDist100Pixel();
|
---|
[2575] | 174 | mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | private void parseURL() {
|
---|
[6394] | 178 | if (!url.hasFocus()) return;
|
---|
| 179 | String urlText = url.getText();
|
---|
| 180 | Bounds b = OsmUrlToBounds.parse(urlText);
|
---|
[11301] | 181 | setBounds(b);
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | private void setBounds(Bounds b) {
|
---|
[2575] | 185 | if (b != null) {
|
---|
[11301] | 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)));
|
---|
[2575] | 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | private void updateUrl(boolean force) {
|
---|
[8510] | 194 | if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
|
---|
[2575] | 195 | try {
|
---|
| 196 | double dlat = Double.parseDouble(lat.getText());
|
---|
| 197 | double dlon = Double.parseDouble(lon.getText());
|
---|
[11301] | 198 | double zoomLvl = Double.parseDouble(zm.getText());
|
---|
| 199 | url.setText(OsmUrlToBounds.getURL(dlat, dlon, (int) zoomLvl));
|
---|
[11620] | 200 | } catch (NumberFormatException e) {
|
---|
[12620] | 201 | Logging.debug(e.getMessage());
|
---|
[6453] | 202 | }
|
---|
[2575] | 203 | }
|
---|
| 204 |
|
---|
[5966] | 205 | @Override
|
---|
[2575] | 206 | public void actionPerformed(ActionEvent e) {
|
---|
| 207 | showJumpToDialog();
|
---|
| 208 | }
|
---|
[6398] | 209 |
|
---|
| 210 | @Override
|
---|
| 211 | protected void updateEnabledState() {
|
---|
[12630] | 212 | setEnabled(MainApplication.isDisplayingMapView());
|
---|
[6398] | 213 | }
|
---|
| 214 |
|
---|
| 215 | @Override
|
---|
| 216 | protected void installAdapters() {
|
---|
| 217 | super.installAdapters();
|
---|
| 218 | // make this action listen to mapframe change events
|
---|
[12639] | 219 | MainApplication.addMapFrameListener((o, n) -> updateEnabledState());
|
---|
[6398] | 220 | }
|
---|
[2575] | 221 | }
|
---|