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

Last change on this file since 11117 was 11100, checked in by Don-vip, 8 years ago

sonar - squid:S2148 - Underscores should be used to make large numbers readable

  • Property svn:eol-style set to native
File size: 7.9 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.MapView;
22import org.openstreetmap.josm.gui.widgets.JosmTextField;
23import org.openstreetmap.josm.tools.GBC;
24import org.openstreetmap.josm.tools.ImageProvider;
25import org.openstreetmap.josm.tools.OsmUrlToBounds;
26import org.openstreetmap.josm.tools.Shortcut;
27
28/**
29 * Allows to jump to a specific location.
30 * @since 2575
31 */
32public class JumpToAction extends JosmAction {
33
34 /**
35 * Constructs a new {@code JumpToAction}.
36 */
37 public JumpToAction() {
38 super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
39 Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
40 KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
41 putValue("help", ht("/Action/JumpToPosition"));
42 }
43
44 private final JosmTextField url = new JosmTextField();
45 private final JosmTextField lat = new JosmTextField();
46 private final JosmTextField lon = new JosmTextField();
47 private final JosmTextField zm = new JosmTextField();
48
49 class OsmURLListener implements DocumentListener {
50 @Override
51 public void changedUpdate(DocumentEvent e) {
52 parseURL();
53 }
54
55 @Override
56 public void insertUpdate(DocumentEvent e) {
57 parseURL();
58 }
59
60 @Override
61 public void removeUpdate(DocumentEvent e) {
62 parseURL();
63 }
64 }
65
66 class OsmLonLatListener implements DocumentListener {
67 @Override
68 public void changedUpdate(DocumentEvent e) {
69 updateUrl(false);
70 }
71
72 @Override
73 public void insertUpdate(DocumentEvent e) {
74 updateUrl(false);
75 }
76
77 @Override
78 public void removeUpdate(DocumentEvent e) {
79 updateUrl(false);
80 }
81 }
82
83 /**
84 * Displays the "Jump to" dialog.
85 */
86 public void showJumpToDialog() {
87 if (!Main.isDisplayingMapView()) {
88 return;
89 }
90 MapView mv = Main.map.mapView;
91 LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter());
92 lat.setText(Double.toString(curPos.lat()));
93 lon.setText(Double.toString(curPos.lon()));
94
95 double dist = mv.getDist100Pixel();
96 zm.setText(Long.toString(Math.round(dist*100)/100));
97 updateUrl(true);
98
99 JPanel panel = new JPanel(new BorderLayout());
100 panel.add(new JLabel("<html>"
101 + tr("Enter Lat/Lon to jump to position.")
102 + "<br>"
103 + tr("You can also paste an URL from www.openstreetmap.org")
104 + "<br>"
105 + "</html>"),
106 BorderLayout.NORTH);
107
108 OsmLonLatListener x = new OsmLonLatListener();
109 lat.getDocument().addDocumentListener(x);
110 lon.getDocument().addDocumentListener(x);
111 zm.getDocument().addDocumentListener(x);
112 url.getDocument().addDocumentListener(new OsmURLListener());
113
114 JPanel p = new JPanel(new GridBagLayout());
115 panel.add(p, BorderLayout.NORTH);
116
117 p.add(new JLabel(tr("Latitude")), GBC.eol());
118 p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
119
120 p.add(new JLabel(tr("Longitude")), GBC.eol());
121 p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
122
123 p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
124 p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
125
126 p.add(new JLabel(tr("URL")), GBC.eol());
127 p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
128
129 Object[] buttons = {tr("Jump there"), tr("Cancel")};
130 LatLon ll = null;
131 double zoomLvl = 100;
132 while (ll == null) {
133 int option = JOptionPane.showOptionDialog(
134 Main.parent,
135 panel,
136 tr("Jump to Position"),
137 JOptionPane.OK_CANCEL_OPTION,
138 JOptionPane.PLAIN_MESSAGE,
139 null,
140 buttons,
141 buttons[0]);
142
143 if (option != JOptionPane.OK_OPTION) return;
144 try {
145 zoomLvl = Double.parseDouble(zm.getText());
146 ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
147 } catch (NumberFormatException ex) {
148 try {
149 ll = LatLon.parse(lat.getText() + "; " + lon.getText());
150 } catch (IllegalArgumentException ex2) {
151 JOptionPane.showMessageDialog(Main.parent,
152 tr("Could not parse Latitude, Longitude or Zoom. Please check."),
153 tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
154 }
155 }
156 }
157
158 double zoomFactor = 1/dist;
159 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
160 }
161
162 private void parseURL() {
163 if (!url.hasFocus()) return;
164 String urlText = url.getText();
165 Bounds b = OsmUrlToBounds.parse(urlText);
166 if (b != null) {
167 lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2));
168 lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2));
169
170 int zoomLvl = 16;
171 int hashIndex = urlText.indexOf("#map");
172 if (hashIndex >= 0) {
173 zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex)));
174 } else {
175 String[] args = urlText.substring(urlText.indexOf('?')+1).split("&");
176 for (String arg : args) {
177 int eq = arg.indexOf('=');
178 if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue;
179
180 zoomLvl = Integer.parseInt(arg.substring(eq + 1));
181 break;
182 }
183 }
184
185 // 10 000 000 = 10 000 * 1000 = World * (km -> m)
186 zm.setText(Double.toString(Math.round(10000000d * Math.pow(2d, (-1d) * zoomLvl))));
187 }
188 }
189
190 private void updateUrl(boolean force) {
191 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
192 try {
193 double dlat = Double.parseDouble(lat.getText());
194 double dlon = Double.parseDouble(lon.getText());
195 double m = Double.parseDouble(zm.getText());
196 // Inverse function to the one above. 18 is the current maximum zoom
197 // available on standard renderers, so choose this is in case m should be zero
198 int zoomLvl = 18;
199 if (m > 0)
200 zoomLvl = (int) Math.round((-1) * Math.log(m/10_000_000)/Math.log(2));
201
202 url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl));
203 } catch (NumberFormatException x) {
204 Main.debug(x.getMessage());
205 }
206 }
207
208 @Override
209 public void actionPerformed(ActionEvent e) {
210 showJumpToDialog();
211 }
212
213 @Override
214 protected void updateEnabledState() {
215 setEnabled(Main.isDisplayingMapView());
216 }
217
218 @Override
219 protected void installAdapters() {
220 super.installAdapters();
221 // make this action listen to mapframe change events
222 Main.addMapFrameListener((o, n) -> updateEnabledState());
223 }
224}
Note: See TracBrowser for help on using the repository browser.