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

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

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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