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

Last change on this file since 6744 was 6453, checked in by Don-vip, 10 years ago

global use of osm website url and new url scheme

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