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 double zoomFactor = 1/dist; 100 101 zm.setText(Long.toString(Math.round(dist*100)/100)); 102 updateUrl(true); 103 104 JPanel panel = new JPanel(new BorderLayout()); 105 panel.add(new JLabel("<html>" 106 + tr("Enter Lat/Lon to jump to position.") 107 + "<br>" 108 + tr("You can also paste an URL from www.openstreetmap.org") 109 + "<br>" 110 + "</html>"), 111 BorderLayout.NORTH); 112 113 OsmLonLatListener x = new OsmLonLatListener(); 114 lat.getDocument().addDocumentListener(x); 115 lon.getDocument().addDocumentListener(x); 116 zm.getDocument().addDocumentListener(x); 117 url.getDocument().addDocumentListener(new OsmURLListener()); 118 119 JPanel p = new JPanel(new GridBagLayout()); 120 panel.add(p, BorderLayout.NORTH); 121 122 p.add(new JLabel(tr("Latitude")), GBC.eol()); 123 p.add(lat, GBC.eol().fill(GBC.HORIZONTAL)); 124 125 p.add(new JLabel(tr("Longitude")), GBC.eol()); 126 p.add(lon, GBC.eol().fill(GBC.HORIZONTAL)); 127 128 p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol()); 129 p.add(zm, GBC.eol().fill(GBC.HORIZONTAL)); 130 131 p.add(new JLabel(tr("URL")), GBC.eol()); 132 p.add(url, GBC.eol().fill(GBC.HORIZONTAL)); 133 134 Object[] buttons = {tr("Jump there"), tr("Cancel")}; 135 LatLon ll = null; 136 double zoomLvl = 100; 137 while (ll == null) { 138 int option = JOptionPane.showOptionDialog( 139 Main.parent, 140 panel, 141 tr("Jump to Position"), 142 JOptionPane.OK_CANCEL_OPTION, 143 JOptionPane.PLAIN_MESSAGE, 144 null, 145 buttons, 146 buttons[0]); 147 148 if (option != JOptionPane.OK_OPTION) return; 149 try { 150 zoomLvl = Double.parseDouble(zm.getText()); 151 ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText())); 152 } catch (NumberFormatException ex) { 153 try { 154 ll = LatLonDialog.parseLatLon(lat.getText() + "; " + lon.getText()); 155 } catch (IllegalArgumentException ex2) { 156 JOptionPane.showMessageDialog(Main.parent, 157 tr("Could not parse Latitude, Longitude or Zoom. Please check."), 158 tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE); 159 } 160 } 161 } 162 163 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl); 164 } 165 166 private void parseURL() { 167 if (!url.hasFocus()) return; 168 String urlText = url.getText(); 169 Bounds b = OsmUrlToBounds.parse(urlText); 170 if (b != null) { 171 lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2)); 172 lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2)); 173 174 int zoomLvl = 16; 175 int hashIndex = urlText.indexOf("#map"); 176 if (hashIndex >= 0) { 177 zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex))); 178 } else { 179 String[] args = urlText.substring(urlText.indexOf('?')+1).split("&"); 180 for (String arg : args) { 181 int eq = arg.indexOf('='); 182 if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue; 183 184 zoomLvl = Integer.parseInt(arg.substring(eq + 1)); 185 break; 186 } 187 } 188 189 // 10 000 000 = 10 000 * 1000 = World * (km -> m) 190 zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl)))); 191 } 192 } 193 194 private void updateUrl(boolean force) { 195 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return; 196 try { 197 double dlat = Double.parseDouble(lat.getText()); 198 double dlon = Double.parseDouble(lon.getText()); 199 double m = Double.parseDouble(zm.getText()); 200 // Inverse function to the one above. 18 is the current maximum zoom 201 // available on standard renderers, so choose this is in case m should be zero 202 int zoomLvl = 18; 203 if (m > 0) 204 zoomLvl = (int) Math.round((-1) * Math.log(m/10000000)/Math.log(2)); 205 206 url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl)); 207 } catch (NumberFormatException x) { 208 Main.debug(x.getMessage()); 209 } 210 } 211 212 @Override 213 public void actionPerformed(ActionEvent e) { 214 showJumpToDialog(); 215 } 216 217 @Override 218 protected void updateEnabledState() { 219 setEnabled(Main.isDisplayingMapView()); 220 } 221 222 @Override 223 protected void installAdapters() { 224 super.installAdapters(); 225 // make this action listen to mapframe change events 226 Main.addMapFrameListener(new MapFrameListener() { 227 @Override 228 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 229 updateEnabledState(); 230 } 231 }); 232 } 233}