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