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

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

Sonar - squid:S1941 - Variables should not be declared before they are relevant

  • 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 zm.setText(Long.toString(Math.round(dist*100)/100));
100 updateUrl(true);
101
102 JPanel panel = new JPanel(new BorderLayout());
103 panel.add(new JLabel("<html>"
104 + tr("Enter Lat/Lon to jump to position.")
105 + "<br>"
106 + tr("You can also paste an URL from www.openstreetmap.org")
107 + "<br>"
108 + "</html>"),
109 BorderLayout.NORTH);
110
111 OsmLonLatListener x = new OsmLonLatListener();
112 lat.getDocument().addDocumentListener(x);
113 lon.getDocument().addDocumentListener(x);
114 zm.getDocument().addDocumentListener(x);
115 url.getDocument().addDocumentListener(new OsmURLListener());
116
117 JPanel p = new JPanel(new GridBagLayout());
118 panel.add(p, BorderLayout.NORTH);
119
120 p.add(new JLabel(tr("Latitude")), GBC.eol());
121 p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
122
123 p.add(new JLabel(tr("Longitude")), GBC.eol());
124 p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
125
126 p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
127 p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
128
129 p.add(new JLabel(tr("URL")), GBC.eol());
130 p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
131
132 Object[] buttons = {tr("Jump there"), tr("Cancel")};
133 LatLon ll = null;
134 double zoomLvl = 100;
135 while (ll == null) {
136 int option = JOptionPane.showOptionDialog(
137 Main.parent,
138 panel,
139 tr("Jump to Position"),
140 JOptionPane.OK_CANCEL_OPTION,
141 JOptionPane.PLAIN_MESSAGE,
142 null,
143 buttons,
144 buttons[0]);
145
146 if (option != JOptionPane.OK_OPTION) return;
147 try {
148 zoomLvl = Double.parseDouble(zm.getText());
149 ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
150 } catch (NumberFormatException ex) {
151 try {
152 ll = LatLonDialog.parseLatLon(lat.getText() + "; " + lon.getText());
153 } catch (IllegalArgumentException ex2) {
154 JOptionPane.showMessageDialog(Main.parent,
155 tr("Could not parse Latitude, Longitude or Zoom. Please check."),
156 tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
157 }
158 }
159 }
160
161 double zoomFactor = 1/dist;
162 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
163 }
164
165 private void parseURL() {
166 if (!url.hasFocus()) return;
167 String urlText = url.getText();
168 Bounds b = OsmUrlToBounds.parse(urlText);
169 if (b != null) {
170 lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2));
171 lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2));
172
173 int zoomLvl = 16;
174 int hashIndex = urlText.indexOf("#map");
175 if (hashIndex >= 0) {
176 zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex)));
177 } else {
178 String[] args = urlText.substring(urlText.indexOf('?')+1).split("&");
179 for (String arg : args) {
180 int eq = arg.indexOf('=');
181 if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue;
182
183 zoomLvl = Integer.parseInt(arg.substring(eq + 1));
184 break;
185 }
186 }
187
188 // 10 000 000 = 10 000 * 1000 = World * (km -> m)
189 zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
190 }
191 }
192
193 private void updateUrl(boolean force) {
194 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
195 try {
196 double dlat = Double.parseDouble(lat.getText());
197 double dlon = Double.parseDouble(lon.getText());
198 double m = Double.parseDouble(zm.getText());
199 // Inverse function to the one above. 18 is the current maximum zoom
200 // available on standard renderers, so choose this is in case m should be zero
201 int zoomLvl = 18;
202 if (m > 0)
203 zoomLvl = (int) Math.round((-1) * Math.log(m/10000000)/Math.log(2));
204
205 url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl));
206 } catch (NumberFormatException x) {
207 Main.debug(x.getMessage());
208 }
209 }
210
211 @Override
212 public void actionPerformed(ActionEvent e) {
213 showJumpToDialog();
214 }
215
216 @Override
217 protected void updateEnabledState() {
218 setEnabled(Main.isDisplayingMapView());
219 }
220
221 @Override
222 protected void installAdapters() {
223 super.installAdapters();
224 // make this action listen to mapframe change events
225 Main.addMapFrameListener(new MapFrameListener() {
226 @Override
227 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
228 updateEnabledState();
229 }
230 });
231 }
232}
Note: See TracBrowser for help on using the repository browser.