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

Last change on this file since 8565 was 8513, checked in by Don-vip, 9 years ago

checkstyle: blocks

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