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

Last change on this file since 8674 was 8670, checked in by simon04, 9 years ago

fix #11749 - Jump to Position: add D M S and D M.M coordinate support to

  • Property svn:eol-style set to native
File size: 8.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11
12import javax.swing.JLabel;
13import javax.swing.JOptionPane;
14import javax.swing.JPanel;
15import javax.swing.event.DocumentEvent;
16import javax.swing.event.DocumentListener;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.data.Bounds;
20import org.openstreetmap.josm.data.coor.LatLon;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.gui.MapFrameListener;
23import org.openstreetmap.josm.gui.MapView;
24import org.openstreetmap.josm.gui.dialogs.LatLonDialog;
25import org.openstreetmap.josm.gui.widgets.JosmTextField;
26import org.openstreetmap.josm.tools.GBC;
27import org.openstreetmap.josm.tools.ImageProvider;
28import org.openstreetmap.josm.tools.OsmUrlToBounds;
29import org.openstreetmap.josm.tools.Shortcut;
30
31/**
32 * Allows to jump to a specific location.
33 * @since 2575
34 */
35public class JumpToAction extends JosmAction {
36
37 /**
38 * Constructs a new {@code JumpToAction}.
39 */
40 public JumpToAction() {
41 super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
42 Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
43 KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
44 putValue("help", ht("/Action/JumpToPosition"));
45 }
46
47 private final JosmTextField url = new JosmTextField();
48 private final JosmTextField lat = new JosmTextField();
49 private final JosmTextField lon = new JosmTextField();
50 private final JosmTextField zm = new JosmTextField();
51
52 class OsmURLListener implements DocumentListener {
53 @Override
54 public void changedUpdate(DocumentEvent e) {
55 parseURL();
56 }
57
58 @Override
59 public void insertUpdate(DocumentEvent e) {
60 parseURL();
61 }
62
63 @Override
64 public void removeUpdate(DocumentEvent e) {
65 parseURL();
66 }
67 }
68
69 class OsmLonLatListener implements DocumentListener {
70 @Override
71 public void changedUpdate(DocumentEvent e) {
72 updateUrl(false);
73 }
74
75 @Override
76 public void insertUpdate(DocumentEvent e) {
77 updateUrl(false);
78 }
79
80 @Override
81 public void removeUpdate(DocumentEvent e) {
82 updateUrl(false);
83 }
84 }
85
86 /**
87 * Displays the "Jump to" dialog.
88 */
89 public void showJumpToDialog() {
90 if (!Main.isDisplayingMapView()) {
91 return;
92 }
93 MapView mv = Main.map.mapView;
94 LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter());
95 lat.setText(Double.toString(curPos.lat()));
96 lon.setText(Double.toString(curPos.lon()));
97
98 double dist = mv.getDist100Pixel();
99 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}
Note: See TracBrowser for help on using the repository browser.