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