1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.BorderLayout;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import javax.swing.Icon;
|
---|
11 |
|
---|
12 | import javax.swing.JLabel;
|
---|
13 | import javax.swing.JOptionPane;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.event.DocumentEvent;
|
---|
16 | import javax.swing.event.DocumentListener;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.Bounds;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.gui.MapView;
|
---|
22 |
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
25 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
27 |
|
---|
28 | public class JumpToAction extends JosmAction {
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code JumpToAction}.
|
---|
31 | */
|
---|
32 | public JumpToAction() {
|
---|
33 | super(tr("Jump To Position"), (Icon) null, tr("Opens a dialog that allows to jump to a specific location"), Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
|
---|
34 | KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", false);
|
---|
35 | }
|
---|
36 |
|
---|
37 | private JosmTextField url = new JosmTextField();
|
---|
38 | private JosmTextField lat = new JosmTextField();
|
---|
39 | private JosmTextField lon = new JosmTextField();
|
---|
40 | private JosmTextField zm = new JosmTextField();
|
---|
41 |
|
---|
42 | private double zoomFactor = 0;
|
---|
43 | public void showJumpToDialog() {
|
---|
44 | MapView mv = Main.map.mapView;
|
---|
45 | if(mv == null)
|
---|
46 | return;
|
---|
47 | LatLon curPos=mv.getProjection().eastNorth2latlon(mv.getCenter());
|
---|
48 | lat.setText(java.lang.Double.toString(curPos.lat()));
|
---|
49 | lon.setText(java.lang.Double.toString(curPos.lon()));
|
---|
50 |
|
---|
51 | double dist = mv.getDist100Pixel();
|
---|
52 | zoomFactor = 1/dist;
|
---|
53 |
|
---|
54 | zm.setText(java.lang.Long.toString(Math.round(dist*100)/100));
|
---|
55 | updateUrl(true);
|
---|
56 |
|
---|
57 | JPanel panel = new JPanel(new BorderLayout());
|
---|
58 | panel.add(new JLabel("<html>"
|
---|
59 | + tr("Enter Lat/Lon to jump to position.")
|
---|
60 | + "<br>"
|
---|
61 | + tr("You can also paste an URL from www.openstreetmap.org")
|
---|
62 | + "<br>"
|
---|
63 | + "</html>"),
|
---|
64 | BorderLayout.NORTH);
|
---|
65 |
|
---|
66 | class OsmURLListener implements DocumentListener {
|
---|
67 | @Override public void changedUpdate(DocumentEvent e) { parseURL(); }
|
---|
68 | @Override public void insertUpdate(DocumentEvent e) { parseURL(); }
|
---|
69 | @Override public void removeUpdate(DocumentEvent e) { parseURL(); }
|
---|
70 | }
|
---|
71 |
|
---|
72 | class OsmLonLatListener implements DocumentListener {
|
---|
73 | @Override public void changedUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
74 | @Override public void insertUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
75 | @Override public void removeUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
76 | }
|
---|
77 |
|
---|
78 | OsmLonLatListener x = new OsmLonLatListener();
|
---|
79 | lat.getDocument().addDocumentListener(x);
|
---|
80 | lon.getDocument().addDocumentListener(x);
|
---|
81 | zm.getDocument().addDocumentListener(x);
|
---|
82 | url.getDocument().addDocumentListener(new OsmURLListener());
|
---|
83 |
|
---|
84 | JPanel p = new JPanel(new GridBagLayout());
|
---|
85 | panel.add(p, BorderLayout.NORTH);
|
---|
86 |
|
---|
87 | p.add(new JLabel(tr("Latitude")), GBC.eol());
|
---|
88 | p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
89 |
|
---|
90 | p.add(new JLabel(tr("Longitude")), GBC.eol());
|
---|
91 | p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
92 |
|
---|
93 | p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
|
---|
94 | p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
95 |
|
---|
96 | p.add(new JLabel(tr("URL")), GBC.eol());
|
---|
97 | p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
98 |
|
---|
99 | Object[] buttons = { tr("Jump there"), tr("Cancel") };
|
---|
100 | LatLon ll = null;
|
---|
101 | double zoomLvl = 100;
|
---|
102 | while(ll == null) {
|
---|
103 | int option = JOptionPane.showOptionDialog(
|
---|
104 | Main.parent,
|
---|
105 | panel,
|
---|
106 | tr("Jump to Position"),
|
---|
107 | JOptionPane.OK_CANCEL_OPTION,
|
---|
108 | JOptionPane.PLAIN_MESSAGE,
|
---|
109 | null,
|
---|
110 | buttons,
|
---|
111 | buttons[0]);
|
---|
112 |
|
---|
113 | if (option != JOptionPane.OK_OPTION) return;
|
---|
114 | try {
|
---|
115 | zoomLvl = Double.parseDouble(zm.getText());
|
---|
116 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
117 | } catch (NumberFormatException ex) {
|
---|
118 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude, Longitude or Zoom. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
|
---|
123 | }
|
---|
124 |
|
---|
125 | private void parseURL() {
|
---|
126 | if(!url.hasFocus()) return;
|
---|
127 | Bounds b = OsmUrlToBounds.parse(url.getText());
|
---|
128 | if (b != null) {
|
---|
129 | lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2));
|
---|
130 | lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2));
|
---|
131 |
|
---|
132 | int zoomLvl = 16;
|
---|
133 | String[] args = url.getText().substring(url.getText().indexOf('?')+1).split("&");
|
---|
134 | for (String arg : args) {
|
---|
135 | int eq = arg.indexOf('=');
|
---|
136 | if (eq == -1 || !arg.substring(0, eq).equalsIgnoreCase("zoom")) continue;
|
---|
137 |
|
---|
138 | zoomLvl = Integer.parseInt(arg.substring(eq + 1));
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | // 10 000 000 = 10 000 * 1000 = World * (km -> m)
|
---|
143 | zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | private void updateUrl(boolean force) {
|
---|
148 | if(!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
|
---|
149 | try {
|
---|
150 | double dlat = Double.parseDouble(lat.getText());
|
---|
151 | double dlon = Double.parseDouble(lon.getText());
|
---|
152 | double m = Double.parseDouble(zm.getText());
|
---|
153 | // Inverse function to the one above. 18 is the current maximum zoom
|
---|
154 | // available on standard renderers, so choose this is in case m
|
---|
155 | // should be zero
|
---|
156 | int zoomLvl = 18;
|
---|
157 | if(m > 0)
|
---|
158 | zoomLvl = (int)Math.round((-1) * Math.log(m/10000000)/Math.log(2));
|
---|
159 |
|
---|
160 | int decimals = (int) Math.pow(10, (zoomLvl / 3));
|
---|
161 | dlat = Math.round(dlat * decimals);
|
---|
162 | dlat /= decimals;
|
---|
163 | dlon = Math.round(dlon * decimals);
|
---|
164 | dlon /= decimals;
|
---|
165 | url.setText("http://www.openstreetmap.org/?lat="+dlat+"&lon="+dlon+"&zoom="+zoomLvl);
|
---|
166 | } catch (NumberFormatException x) {}
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public void actionPerformed(ActionEvent e) {
|
---|
171 | showJumpToDialog();
|
---|
172 | }
|
---|
173 | }
|
---|