001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions; 003 004import static org.openstreetmap.josm.gui.help.HelpUtil.ht; 005import static org.openstreetmap.josm.tools.I18n.tr; 006 007import java.awt.BorderLayout; 008import java.awt.GridBagLayout; 009import java.awt.event.ActionEvent; 010import java.awt.event.KeyEvent; 011 012import javax.swing.JLabel; 013import javax.swing.JOptionPane; 014import javax.swing.JPanel; 015import javax.swing.event.DocumentEvent; 016import javax.swing.event.DocumentListener; 017 018import org.openstreetmap.josm.Main; 019import org.openstreetmap.josm.data.Bounds; 020import org.openstreetmap.josm.data.coor.LatLon; 021import org.openstreetmap.josm.gui.MapView; 022import org.openstreetmap.josm.gui.widgets.JosmTextField; 023import org.openstreetmap.josm.tools.GBC; 024import org.openstreetmap.josm.tools.ImageProvider; 025import org.openstreetmap.josm.tools.OsmUrlToBounds; 026import org.openstreetmap.josm.tools.Shortcut; 027 028/** 029 * Allows to jump to a specific location. 030 * @since 2575 031 */ 032public class JumpToAction extends JosmAction { 033 034 /** 035 * Constructs a new {@code JumpToAction}. 036 */ 037 public JumpToAction() { 038 super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"), 039 Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")), 040 KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true); 041 putValue("help", ht("/Action/JumpToPosition")); 042 } 043 044 private final JosmTextField url = new JosmTextField(); 045 private final JosmTextField lat = new JosmTextField(); 046 private final JosmTextField lon = new JosmTextField(); 047 private final JosmTextField zm = new JosmTextField(); 048 049 class OsmURLListener implements DocumentListener { 050 @Override 051 public void changedUpdate(DocumentEvent e) { 052 parseURL(); 053 } 054 055 @Override 056 public void insertUpdate(DocumentEvent e) { 057 parseURL(); 058 } 059 060 @Override 061 public void removeUpdate(DocumentEvent e) { 062 parseURL(); 063 } 064 } 065 066 class OsmLonLatListener implements DocumentListener { 067 @Override 068 public void changedUpdate(DocumentEvent e) { 069 updateUrl(false); 070 } 071 072 @Override 073 public void insertUpdate(DocumentEvent e) { 074 updateUrl(false); 075 } 076 077 @Override 078 public void removeUpdate(DocumentEvent e) { 079 updateUrl(false); 080 } 081 } 082 083 /** 084 * Displays the "Jump to" dialog. 085 */ 086 public void showJumpToDialog() { 087 if (!Main.isDisplayingMapView()) { 088 return; 089 } 090 MapView mv = Main.map.mapView; 091 LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter()); 092 lat.setText(Double.toString(curPos.lat())); 093 lon.setText(Double.toString(curPos.lon())); 094 095 double dist = mv.getDist100Pixel(); 096 zm.setText(Long.toString(Math.round(dist*100)/100)); 097 updateUrl(true); 098 099 JPanel panel = new JPanel(new BorderLayout()); 100 panel.add(new JLabel("<html>" 101 + tr("Enter Lat/Lon to jump to position.") 102 + "<br>" 103 + tr("You can also paste an URL from www.openstreetmap.org") 104 + "<br>" 105 + "</html>"), 106 BorderLayout.NORTH); 107 108 OsmLonLatListener x = new OsmLonLatListener(); 109 lat.getDocument().addDocumentListener(x); 110 lon.getDocument().addDocumentListener(x); 111 zm.getDocument().addDocumentListener(x); 112 url.getDocument().addDocumentListener(new OsmURLListener()); 113 114 JPanel p = new JPanel(new GridBagLayout()); 115 panel.add(p, BorderLayout.NORTH); 116 117 p.add(new JLabel(tr("Latitude")), GBC.eol()); 118 p.add(lat, GBC.eol().fill(GBC.HORIZONTAL)); 119 120 p.add(new JLabel(tr("Longitude")), GBC.eol()); 121 p.add(lon, GBC.eol().fill(GBC.HORIZONTAL)); 122 123 p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol()); 124 p.add(zm, GBC.eol().fill(GBC.HORIZONTAL)); 125 126 p.add(new JLabel(tr("URL")), GBC.eol()); 127 p.add(url, GBC.eol().fill(GBC.HORIZONTAL)); 128 129 Object[] buttons = {tr("Jump there"), tr("Cancel")}; 130 LatLon ll = null; 131 double zoomLvl = 100; 132 while (ll == null) { 133 int option = JOptionPane.showOptionDialog( 134 Main.parent, 135 panel, 136 tr("Jump to Position"), 137 JOptionPane.OK_CANCEL_OPTION, 138 JOptionPane.PLAIN_MESSAGE, 139 null, 140 buttons, 141 buttons[0]); 142 143 if (option != JOptionPane.OK_OPTION) return; 144 try { 145 zoomLvl = Double.parseDouble(zm.getText()); 146 ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText())); 147 } catch (NumberFormatException ex) { 148 try { 149 ll = LatLon.parse(lat.getText() + "; " + lon.getText()); 150 } catch (IllegalArgumentException ex2) { 151 JOptionPane.showMessageDialog(Main.parent, 152 tr("Could not parse Latitude, Longitude or Zoom. Please check."), 153 tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE); 154 } 155 } 156 } 157 158 double zoomFactor = 1/dist; 159 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl); 160 } 161 162 private void parseURL() { 163 if (!url.hasFocus()) return; 164 String urlText = url.getText(); 165 Bounds b = OsmUrlToBounds.parse(urlText); 166 if (b != null) { 167 lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2)); 168 lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2)); 169 170 int zoomLvl = 16; 171 int hashIndex = urlText.indexOf("#map"); 172 if (hashIndex >= 0) { 173 zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex))); 174 } else { 175 String[] args = urlText.substring(urlText.indexOf('?')+1).split("&"); 176 for (String arg : args) { 177 int eq = arg.indexOf('='); 178 if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue; 179 180 zoomLvl = Integer.parseInt(arg.substring(eq + 1)); 181 break; 182 } 183 } 184 185 // 10 000 000 = 10 000 * 1000 = World * (km -> m) 186 zm.setText(Double.toString(Math.round(10000000d * Math.pow(2d, (-1d) * zoomLvl)))); 187 } 188 } 189 190 private void updateUrl(boolean force) { 191 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return; 192 try { 193 double dlat = Double.parseDouble(lat.getText()); 194 double dlon = Double.parseDouble(lon.getText()); 195 double m = Double.parseDouble(zm.getText()); 196 // Inverse function to the one above. 18 is the current maximum zoom 197 // available on standard renderers, so choose this is in case m should be zero 198 int zoomLvl = 18; 199 if (m > 0) 200 zoomLvl = (int) Math.round((-1) * Math.log(m/10_000_000)/Math.log(2)); 201 202 url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl)); 203 } catch (NumberFormatException x) { 204 Main.debug(x.getMessage()); 205 } 206 } 207 208 @Override 209 public void actionPerformed(ActionEvent e) { 210 showJumpToDialog(); 211 } 212 213 @Override 214 protected void updateEnabledState() { 215 setEnabled(Main.isDisplayingMapView()); 216 } 217 218 @Override 219 protected void installAdapters() { 220 super.installAdapters(); 221 // make this action listen to mapframe change events 222 Main.addMapFrameListener((o, n) -> updateEnabledState()); 223 } 224}