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