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

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

checkstyle: enable relevant whitespace checks and fix them

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