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