source: josm/trunk/src/org/openstreetmap/josm/actions/JumpToAction.java @ 5241

Revision 4982, 7.1 KB checked in by stoecker, 3 months ago (diff)

see #7226 - patch by akks (fixed a bit) - fix shortcut deprecations

  • Property svn:eol-style set to native
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.BorderLayout;
7import java.awt.GridBagLayout;
8import java.awt.event.ActionEvent;
9import java.awt.event.KeyEvent;
10import java.awt.event.MouseEvent;
11import java.awt.event.MouseListener;
12
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.JTextField;
17import javax.swing.event.DocumentEvent;
18import javax.swing.event.DocumentListener;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.actions.JosmAction;
22import org.openstreetmap.josm.data.Bounds;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.gui.MapView;
25import org.openstreetmap.josm.tools.GBC;
26import org.openstreetmap.josm.tools.OsmUrlToBounds;
27import org.openstreetmap.josm.tools.Shortcut;
28
29public class JumpToAction extends JosmAction implements MouseListener {
30    public JumpToAction() {
31        super(tr("Jump To Position"), null, tr("Opens a dialog that allows to jump to a specific location"), Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
32        KeyEvent.VK_J, Shortcut.CTRL), false);
33        putValue("toolbar", "action/jumpto");
34        Main.toolbar.register(this);
35    }
36
37    private JTextField url = new JTextField();
38    private JTextField lat = new JTextField();
39    private JTextField lon = new JTextField();
40    private JTextField zm = new JTextField();
41
42    private double zoomFactor = 0;
43    public void showJumpToDialog() {
44        MapView mv = Main.map.mapView;
45        if(mv == null)
46            return;
47        LatLon curPos=mv.getProjection().eastNorth2latlon(mv.getCenter());
48        lat.setText(java.lang.Double.toString(curPos.lat()));
49        lon.setText(java.lang.Double.toString(curPos.lon()));
50
51        double dist = mv.getDist100Pixel();
52        zoomFactor = 1/dist;
53
54        zm.setText(java.lang.Long.toString(Math.round(dist*100)/100));
55        updateUrl(true);
56
57        JPanel panel = new JPanel(new BorderLayout());
58        panel.add(new JLabel("<html>"
59                              + tr("Enter Lat/Lon to jump to position.")
60                              + "<br>"
61                              + tr("You can also paste an URL from www.openstreetmap.org")
62                              + "<br>"
63                              + "</html>"),
64                  BorderLayout.NORTH);
65
66        class osmURLListener implements DocumentListener {
67            public void changedUpdate(DocumentEvent e) { parseURL(); }
68            public void insertUpdate(DocumentEvent e) { parseURL(); }
69            public void removeUpdate(DocumentEvent e) { parseURL(); }
70        }
71
72        class osmLonLatListener implements DocumentListener {
73            public void changedUpdate(DocumentEvent e) { updateUrl(false); }
74            public void insertUpdate(DocumentEvent e) { updateUrl(false); }
75            public void removeUpdate(DocumentEvent e) { updateUrl(false); }
76        }
77
78        osmLonLatListener x=new osmLonLatListener();
79        lat.getDocument().addDocumentListener(x);
80        lon.getDocument().addDocumentListener(x);
81        zm.getDocument().addDocumentListener(x);
82        url.getDocument().addDocumentListener(new osmURLListener());
83
84        JPanel p = new JPanel(new GridBagLayout());
85        panel.add(p, BorderLayout.NORTH);
86
87        p.add(new JLabel(tr("Latitude")), GBC.eol());
88        p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
89
90        p.add(new JLabel(tr("Longitude")), GBC.eol());
91        p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
92
93        p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
94        p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
95
96        p.add(new JLabel(tr("URL")), GBC.eol());
97        p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
98
99        Object[] buttons = { tr("Jump there"), tr("Cancel") };
100        LatLon ll = null;
101        double zoomLvl = 100;
102        while(ll == null) {
103            int option = JOptionPane.showOptionDialog(
104                            Main.parent,
105                            panel,
106                            tr("Jump to Position"),
107                            JOptionPane.OK_CANCEL_OPTION,
108                            JOptionPane.PLAIN_MESSAGE,
109                            null,
110                            buttons,
111                            buttons[0]);
112
113            if (option != JOptionPane.OK_OPTION) return;
114            try {
115                zoomLvl = Double.parseDouble(zm.getText());
116                ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
117            } catch (Exception ex) {
118                JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude, Longitude or Zoom. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
119            }
120        }
121
122        mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
123    }
124
125    private void parseURL() {
126        if(!url.hasFocus()) return;
127        Bounds b = OsmUrlToBounds.parse(url.getText());
128        if (b != null) {
129            lat.setText(Double.toString((b.getMin().lat() + b.getMax().lat())/2));
130            lon.setText(Double.toString((b.getMin().lon() + b.getMax().lon())/2));
131
132            int zoomLvl = 16;
133            String[] args = url.getText().substring(url.getText().indexOf('?')+1).split("&");
134            for (String arg : args) {
135                int eq = arg.indexOf('=');
136                if (eq == -1 || !arg.substring(0, eq).equalsIgnoreCase("zoom")) continue;
137
138                zoomLvl = Integer.parseInt(arg.substring(eq + 1));
139                break;
140            }
141
142            // 10 000 000 = 10 000 * 1000 = World * (km -> m)
143            zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
144        }
145    }
146
147    private void updateUrl(boolean force) {
148        if(!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
149        try {
150            double dlat = Double.parseDouble(lat.getText());
151            double dlon = Double.parseDouble(lon.getText());
152            double m = Double.parseDouble(zm.getText());
153            // Inverse function to the one above. 18 is the current maximum zoom
154            // available on standard renderers, so choose this is in case m
155            // should be zero
156            int zoomLvl = 18;
157            if(m > 0)
158                zoomLvl = (int)Math.round((-1) * Math.log(m/10000000)/Math.log(2));
159
160            int decimals = (int) Math.pow(10, (zoomLvl / 3));
161            dlat = Math.round(dlat * decimals);
162            dlat /= decimals;
163            dlon = Math.round(dlon * decimals);
164            dlon /= decimals;
165            url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoomLvl);
166        } catch (NumberFormatException x) {}
167    }
168
169    public void actionPerformed(ActionEvent e) {
170        showJumpToDialog();
171    }
172
173    public void mousePressed(MouseEvent e) {}
174
175    public void mouseReleased(MouseEvent e) {}
176
177    public void mouseEntered(MouseEvent e) {}
178
179    public void mouseExited(MouseEvent e) {}
180
181    public void mouseClicked(MouseEvent e) {
182        showJumpToDialog();
183    }
184}
Note: See TracBrowser for help on using the repository browser.