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.MapFrame; 022import org.openstreetmap.josm.gui.MapFrameListener; 023import org.openstreetmap.josm.gui.MapView; 024import org.openstreetmap.josm.gui.dialogs.LatLonDialog; 025import org.openstreetmap.josm.gui.widgets.JosmTextField; 026import org.openstreetmap.josm.tools.GBC; 027import org.openstreetmap.josm.tools.ImageProvider; 028import org.openstreetmap.josm.tools.OsmUrlToBounds; 029import org.openstreetmap.josm.tools.Shortcut; 030 031/** 032 * Allows to jump to a specific location. 033 * @since 2575 034 */ 035public class JumpToAction extends JosmAction { 036 037 /** 038 * Constructs a new {@code JumpToAction}. 039 */ 040 public JumpToAction() { 041 super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"), 042 Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")), 043 KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true); 044 putValue("help", ht("/Action/JumpToPosition")); 045 } 046 047 private final JosmTextField url = new JosmTextField(); 048 private final JosmTextField lat = new JosmTextField(); 049 private final JosmTextField lon = new JosmTextField(); 050 private final JosmTextField zm = new JosmTextField(); 051 052 class OsmURLListener implements DocumentListener { 053 @Override 054 public void changedUpdate(DocumentEvent e) { 055 parseURL(); 056 } 057 058 @Override 059 public void insertUpdate(DocumentEvent e) { 060 parseURL(); 061 } 062 063 @Override 064 public void removeUpdate(DocumentEvent e) { 065 parseURL(); 066 } 067 } 068 069 class OsmLonLatListener implements DocumentListener { 070 @Override 071 public void changedUpdate(DocumentEvent e) { 072 updateUrl(false); 073 } 074 075 @Override 076 public void insertUpdate(DocumentEvent e) { 077 updateUrl(false); 078 } 079 080 @Override 081 public void removeUpdate(DocumentEvent e) { 082 updateUrl(false); 083 } 084 } 085 086 /** 087 * Displays the "Jump to" dialog. 088 */ 089 public void showJumpToDialog() { 090 if (!Main.isDisplayingMapView()) { 091 return; 092 } 093 MapView mv = Main.map.mapView; 094 LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter()); 095 lat.setText(Double.toString(curPos.lat())); 096 lon.setText(Double.toString(curPos.lon())); 097 098 double dist = mv.getDist100Pixel(); 099 zm.setText(Long.toString(Math.round(dist*100)/100)); 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 111 OsmLonLatListener x = new OsmLonLatListener(); 112 lat.getDocument().addDocumentListener(x); 113 lon.getDocument().addDocumentListener(x); 114 zm.getDocument().addDocumentListener(x); 115 url.getDocument().addDocumentListener(new OsmURLListener()); 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 132 Object[] buttons = {tr("Jump there"), tr("Cancel")}; 133 LatLon ll = null; 134 double zoomLvl = 100; 135 while (ll == null) { 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())); 150 } catch (NumberFormatException ex) { 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 } 158 } 159 } 160 161 double zoomFactor = 1/dist; 162 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl); 163 } 164 165 private void parseURL() { 166 if (!url.hasFocus()) return; 167 String urlText = url.getText(); 168 Bounds b = OsmUrlToBounds.parse(urlText); 169 if (b != null) { 170 lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2)); 171 lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2)); 172 173 int zoomLvl = 16; 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('='); 181 if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue; 182 183 zoomLvl = Integer.parseInt(arg.substring(eq + 1)); 184 break; 185 } 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) { 194 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return; 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 200 // available on standard renderers, so choose this is in case m should be zero 201 int zoomLvl = 18; 202 if (m > 0) 203 zoomLvl = (int) Math.round((-1) * Math.log(m/10000000)/Math.log(2)); 204 205 url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl)); 206 } catch (NumberFormatException x) { 207 Main.debug(x.getMessage()); 208 } 209 } 210 211 @Override 212 public void actionPerformed(ActionEvent e) { 213 showJumpToDialog(); 214 } 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 } 232}