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

Last change on this file since 11300 was 11300, checked in by simon04, 7 years ago

Jump to Position: focus first input, select text in textfield on focus

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