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