[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;
|
---|
[6398] | 11 |
|
---|
[2575] | 12 | import javax.swing.JLabel;
|
---|
| 13 | import javax.swing.JOptionPane;
|
---|
| 14 | import javax.swing.JPanel;
|
---|
| 15 | import javax.swing.event.DocumentEvent;
|
---|
| 16 | import javax.swing.event.DocumentListener;
|
---|
| 17 |
|
---|
| 18 | import org.openstreetmap.josm.Main;
|
---|
| 19 | import org.openstreetmap.josm.data.Bounds;
|
---|
| 20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[6398] | 21 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
| 22 | import org.openstreetmap.josm.gui.MapFrameListener;
|
---|
[2575] | 23 | import org.openstreetmap.josm.gui.MapView;
|
---|
[8670] | 24 | import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
|
---|
[6398] | 25 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[2575] | 26 | import org.openstreetmap.josm.tools.GBC;
|
---|
[7693] | 27 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[2575] | 28 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
| 29 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 30 |
|
---|
[6394] | 31 | /**
|
---|
| 32 | * Allows to jump to a specific location.
|
---|
| 33 | * @since 2575
|
---|
| 34 | */
|
---|
[5965] | 35 | public class JumpToAction extends JosmAction {
|
---|
[7081] | 36 |
|
---|
[5886] | 37 | /**
|
---|
| 38 | * Constructs a new {@code JumpToAction}.
|
---|
| 39 | */
|
---|
[2575] | 40 | public JumpToAction() {
|
---|
[7693] | 41 | super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
|
---|
[6398] | 42 | Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
|
---|
| 43 | KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
|
---|
[7812] | 44 | putValue("help", ht("/Action/JumpToPosition"));
|
---|
[2575] | 45 | }
|
---|
| 46 |
|
---|
[6394] | 47 | private final JosmTextField url = new JosmTextField();
|
---|
| 48 | private final JosmTextField lat = new JosmTextField();
|
---|
| 49 | private final JosmTextField lon = new JosmTextField();
|
---|
| 50 | private final JosmTextField zm = new JosmTextField();
|
---|
[2575] | 51 |
|
---|
[7081] | 52 | class OsmURLListener implements DocumentListener {
|
---|
[8510] | 53 | @Override
|
---|
[8513] | 54 | public void changedUpdate(DocumentEvent e) {
|
---|
| 55 | parseURL();
|
---|
| 56 | }
|
---|
[8510] | 57 |
|
---|
| 58 | @Override
|
---|
[8513] | 59 | public void insertUpdate(DocumentEvent e) {
|
---|
| 60 | parseURL();
|
---|
| 61 | }
|
---|
[8510] | 62 |
|
---|
| 63 | @Override
|
---|
[8513] | 64 | public void removeUpdate(DocumentEvent e) {
|
---|
| 65 | parseURL();
|
---|
| 66 | }
|
---|
[7081] | 67 | }
|
---|
| 68 |
|
---|
| 69 | class OsmLonLatListener implements DocumentListener {
|
---|
[8510] | 70 | @Override
|
---|
[8513] | 71 | public void changedUpdate(DocumentEvent e) {
|
---|
| 72 | updateUrl(false);
|
---|
| 73 | }
|
---|
[8510] | 74 |
|
---|
| 75 | @Override
|
---|
[8513] | 76 | public void insertUpdate(DocumentEvent e) {
|
---|
| 77 | updateUrl(false);
|
---|
| 78 | }
|
---|
[8510] | 79 |
|
---|
| 80 | @Override
|
---|
[8513] | 81 | public void removeUpdate(DocumentEvent e) {
|
---|
| 82 | updateUrl(false);
|
---|
| 83 | }
|
---|
[7081] | 84 | }
|
---|
| 85 |
|
---|
[6394] | 86 | /**
|
---|
| 87 | * Displays the "Jump to" dialog.
|
---|
| 88 | */
|
---|
[2575] | 89 | public void showJumpToDialog() {
|
---|
[6394] | 90 | if (!Main.isDisplayingMapView()) {
|
---|
| 91 | return;
|
---|
| 92 | }
|
---|
[2575] | 93 | MapView mv = Main.map.mapView;
|
---|
[6394] | 94 | LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter());
|
---|
| 95 | lat.setText(Double.toString(curPos.lat()));
|
---|
| 96 | lon.setText(Double.toString(curPos.lon()));
|
---|
[2575] | 97 |
|
---|
| 98 | double dist = mv.getDist100Pixel();
|
---|
[6394] | 99 | zm.setText(Long.toString(Math.round(dist*100)/100));
|
---|
[2575] | 100 | updateUrl(true);
|
---|
| 101 |
|
---|
| 102 | JPanel panel = new JPanel(new BorderLayout());
|
---|
| 103 | panel.add(new JLabel("<html>"
|
---|
| 104 | + tr("Enter Lat/Lon to jump to position.")
|
---|
| 105 | + "<br>"
|
---|
| 106 | + tr("You can also paste an URL from www.openstreetmap.org")
|
---|
| 107 | + "<br>"
|
---|
| 108 | + "</html>"),
|
---|
| 109 | BorderLayout.NORTH);
|
---|
| 110 |
|
---|
[6246] | 111 | OsmLonLatListener x = new OsmLonLatListener();
|
---|
[2575] | 112 | lat.getDocument().addDocumentListener(x);
|
---|
| 113 | lon.getDocument().addDocumentListener(x);
|
---|
| 114 | zm.getDocument().addDocumentListener(x);
|
---|
[6246] | 115 | url.getDocument().addDocumentListener(new OsmURLListener());
|
---|
[2575] | 116 |
|
---|
| 117 | JPanel p = new JPanel(new GridBagLayout());
|
---|
| 118 | panel.add(p, BorderLayout.NORTH);
|
---|
| 119 |
|
---|
| 120 | p.add(new JLabel(tr("Latitude")), GBC.eol());
|
---|
| 121 | p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 122 |
|
---|
| 123 | p.add(new JLabel(tr("Longitude")), GBC.eol());
|
---|
| 124 | p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 125 |
|
---|
| 126 | p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
|
---|
| 127 | p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 128 |
|
---|
| 129 | p.add(new JLabel(tr("URL")), GBC.eol());
|
---|
| 130 | p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
| 131 |
|
---|
[8443] | 132 | Object[] buttons = {tr("Jump there"), tr("Cancel")};
|
---|
[2575] | 133 | LatLon ll = null;
|
---|
| 134 | double zoomLvl = 100;
|
---|
[8510] | 135 | while (ll == null) {
|
---|
[2575] | 136 | int option = JOptionPane.showOptionDialog(
|
---|
| 137 | Main.parent,
|
---|
| 138 | panel,
|
---|
| 139 | tr("Jump to Position"),
|
---|
| 140 | JOptionPane.OK_CANCEL_OPTION,
|
---|
| 141 | JOptionPane.PLAIN_MESSAGE,
|
---|
| 142 | null,
|
---|
| 143 | buttons,
|
---|
| 144 | buttons[0]);
|
---|
| 145 |
|
---|
| 146 | if (option != JOptionPane.OK_OPTION) return;
|
---|
| 147 | try {
|
---|
| 148 | zoomLvl = Double.parseDouble(zm.getText());
|
---|
| 149 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
[5780] | 150 | } catch (NumberFormatException ex) {
|
---|
[8670] | 151 | try {
|
---|
| 152 | ll = LatLonDialog.parseLatLon(lat.getText() + "; " + lon.getText());
|
---|
| 153 | } catch (IllegalArgumentException ex2) {
|
---|
| 154 | JOptionPane.showMessageDialog(Main.parent,
|
---|
| 155 | tr("Could not parse Latitude, Longitude or Zoom. Please check."),
|
---|
| 156 | tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
|
---|
| 157 | }
|
---|
[2575] | 158 | }
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[9062] | 161 | double zoomFactor = 1/dist;
|
---|
[2575] | 162 | mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | private void parseURL() {
|
---|
[6394] | 166 | if (!url.hasFocus()) return;
|
---|
| 167 | String urlText = url.getText();
|
---|
| 168 | Bounds b = OsmUrlToBounds.parse(urlText);
|
---|
[2575] | 169 | if (b != null) {
|
---|
[6203] | 170 | lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2));
|
---|
| 171 | lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2));
|
---|
[2575] | 172 |
|
---|
| 173 | int zoomLvl = 16;
|
---|
[6394] | 174 | int hashIndex = urlText.indexOf("#map");
|
---|
| 175 | if (hashIndex >= 0) {
|
---|
| 176 | zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex)));
|
---|
| 177 | } else {
|
---|
| 178 | String[] args = urlText.substring(urlText.indexOf('?')+1).split("&");
|
---|
| 179 | for (String arg : args) {
|
---|
| 180 | int eq = arg.indexOf('=');
|
---|
[7025] | 181 | if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue;
|
---|
[7081] | 182 |
|
---|
[6394] | 183 | zoomLvl = Integer.parseInt(arg.substring(eq + 1));
|
---|
| 184 | break;
|
---|
| 185 | }
|
---|
[2575] | 186 | }
|
---|
| 187 |
|
---|
| 188 | // 10 000 000 = 10 000 * 1000 = World * (km -> m)
|
---|
| 189 | zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
|
---|
| 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());
|
---|
| 198 | double m = Double.parseDouble(zm.getText());
|
---|
| 199 | // Inverse function to the one above. 18 is the current maximum zoom
|
---|
[8510] | 200 | // available on standard renderers, so choose this is in case m should be zero
|
---|
[2575] | 201 | int zoomLvl = 18;
|
---|
[8510] | 202 | if (m > 0)
|
---|
| 203 | zoomLvl = (int) Math.round((-1) * Math.log(m/10000000)/Math.log(2));
|
---|
[2575] | 204 |
|
---|
[6453] | 205 | url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl));
|
---|
| 206 | } catch (NumberFormatException x) {
|
---|
| 207 | Main.debug(x.getMessage());
|
---|
| 208 | }
|
---|
[2575] | 209 | }
|
---|
| 210 |
|
---|
[5966] | 211 | @Override
|
---|
[2575] | 212 | public void actionPerformed(ActionEvent e) {
|
---|
| 213 | showJumpToDialog();
|
---|
| 214 | }
|
---|
[6398] | 215 |
|
---|
| 216 | @Override
|
---|
| 217 | protected void updateEnabledState() {
|
---|
| 218 | setEnabled(Main.isDisplayingMapView());
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | @Override
|
---|
| 222 | protected void installAdapters() {
|
---|
| 223 | super.installAdapters();
|
---|
| 224 | // make this action listen to mapframe change events
|
---|
| 225 | Main.addMapFrameListener(new MapFrameListener() {
|
---|
| 226 | @Override
|
---|
| 227 | public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
|
---|
| 228 | updateEnabledState();
|
---|
| 229 | }
|
---|
| 230 | });
|
---|
| 231 | }
|
---|
[2575] | 232 | }
|
---|