1 | package UtilsPlugin;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.InputEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.awt.event.MouseListener;
|
---|
11 | import java.awt.event.MouseEvent;
|
---|
12 | import java.awt.GridBagLayout;
|
---|
13 | import java.awt.GridLayout;
|
---|
14 | import java.awt.Toolkit;
|
---|
15 |
|
---|
16 | import javax.swing.Box;
|
---|
17 | import javax.swing.event.DocumentListener;
|
---|
18 | import javax.swing.event.DocumentEvent;
|
---|
19 | import javax.swing.JComponent;
|
---|
20 | import javax.swing.JDialog;
|
---|
21 | import javax.swing.JLabel;
|
---|
22 | import javax.swing.JOptionPane;
|
---|
23 | import javax.swing.JPanel;
|
---|
24 | import javax.swing.JTextField;
|
---|
25 | import javax.swing.KeyStroke;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
29 | import org.openstreetmap.josm.data.Bounds;
|
---|
30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
31 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
32 | import org.openstreetmap.josm.gui.dialogs.ToggleDialog;
|
---|
33 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
34 | import org.openstreetmap.josm.tools.GBC;
|
---|
35 | import org.openstreetmap.josm.tools.OsmUrlToBounds;
|
---|
36 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
37 |
|
---|
38 | public class JumpToAction extends JosmAction implements MouseListener {
|
---|
39 | public JumpToAction() {
|
---|
40 | 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")),
|
---|
41 | KeyEvent.VK_G, Shortcut.GROUP_HOTKEY), true);
|
---|
42 | }
|
---|
43 |
|
---|
44 | private JTextField url = new JTextField();
|
---|
45 | private JTextField lat = new JTextField();
|
---|
46 | private JTextField lon = new JTextField();
|
---|
47 | private JTextField zm = new JTextField();
|
---|
48 |
|
---|
49 | private double zoomFactor = 0;
|
---|
50 | public void showJumpToDialog() {
|
---|
51 | LatLon curPos=Main.proj.eastNorth2latlon(Main.map.mapView.getCenter());
|
---|
52 | lat.setText(java.lang.Double.toString(curPos.lat()));
|
---|
53 | lon.setText(java.lang.Double.toString(curPos.lon()));
|
---|
54 |
|
---|
55 | LatLon ll1 = Main.map.mapView.getLatLon(0,0);
|
---|
56 | LatLon ll2 = Main.map.mapView.getLatLon(100,0);
|
---|
57 | double dist = ll1.greatCircleDistance(ll2);
|
---|
58 | zoomFactor = Main.map.mapView.getScale()/dist;
|
---|
59 |
|
---|
60 | zm.setText(java.lang.Long.toString(Math.round(dist*100)/100));
|
---|
61 | updateUrl(true);
|
---|
62 |
|
---|
63 | JPanel panel = new JPanel(new BorderLayout());
|
---|
64 | panel.add(new JLabel("<html>"
|
---|
65 | + tr("Enter Lat/Lon to jump to position.")
|
---|
66 | + "<br>"
|
---|
67 | + tr("You can also paste an URL from www.openstreetmap.org")
|
---|
68 | + "<br>"
|
---|
69 | + "</html>"),
|
---|
70 | BorderLayout.NORTH);
|
---|
71 |
|
---|
72 | class osmURLListener implements DocumentListener {
|
---|
73 | public void changedUpdate(DocumentEvent e) { parseURL(); }
|
---|
74 | public void insertUpdate(DocumentEvent e) { parseURL(); }
|
---|
75 | public void removeUpdate(DocumentEvent e) { parseURL(); }
|
---|
76 | }
|
---|
77 |
|
---|
78 | class osmLonLatListener implements DocumentListener {
|
---|
79 | public void changedUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
80 | public void insertUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
81 | public void removeUpdate(DocumentEvent e) { updateUrl(false); }
|
---|
82 | }
|
---|
83 |
|
---|
84 | osmLonLatListener x=new osmLonLatListener();
|
---|
85 | lat.getDocument().addDocumentListener(x);
|
---|
86 | lon.getDocument().addDocumentListener(x);
|
---|
87 | zm.getDocument().addDocumentListener(x);
|
---|
88 | url.getDocument().addDocumentListener(new osmURLListener());
|
---|
89 |
|
---|
90 | JPanel p = new JPanel(new GridBagLayout());
|
---|
91 | panel.add(p, BorderLayout.NORTH);
|
---|
92 |
|
---|
93 | p.add(new JLabel(tr("Latitude")), GBC.eol());
|
---|
94 | p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
95 |
|
---|
96 | p.add(new JLabel(tr("Longitude")), GBC.eol());
|
---|
97 | p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
98 |
|
---|
99 | p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
|
---|
100 | p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
101 |
|
---|
102 | p.add(new JLabel(tr("URL")), GBC.eol());
|
---|
103 | p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
104 |
|
---|
105 | Object[] buttons = { tr("Jump there"), tr("Cancel") };
|
---|
106 | LatLon ll = null;
|
---|
107 | double zoomLvl = 100;
|
---|
108 | while(ll == null) {
|
---|
109 | int option = JOptionPane.showOptionDialog(
|
---|
110 | Main.parent,
|
---|
111 | panel,
|
---|
112 | tr("Jump to Position"),
|
---|
113 | JOptionPane.OK_CANCEL_OPTION,
|
---|
114 | JOptionPane.PLAIN_MESSAGE,
|
---|
115 | null,
|
---|
116 | buttons,
|
---|
117 | buttons[0]);
|
---|
118 |
|
---|
119 | if (option != JOptionPane.OK_OPTION) return;
|
---|
120 | try {
|
---|
121 | zoomLvl = Double.parseDouble(zm.getText());
|
---|
122 | ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
|
---|
123 | } catch (Exception ex) {
|
---|
124 | JOptionPane.showMessageDialog(Main.parent, tr("Could not parse Latitude, Longitude or Zoom. Please check."), tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | Main.map.mapView.zoomTo(Main.proj.latlon2eastNorth(ll), zoomFactor * zoomLvl);
|
---|
129 | }
|
---|
130 |
|
---|
131 | private void parseURL() {
|
---|
132 | if(!url.hasFocus()) return;
|
---|
133 | Bounds b = OsmUrlToBounds.parse(url.getText());
|
---|
134 | if (b != null) {
|
---|
135 | lat.setText(Double.toString((b.min.lat() + b.max.lat())/2));
|
---|
136 | lon.setText(Double.toString((b.min.lon() + b.max.lon())/2));
|
---|
137 |
|
---|
138 | int zoomLvl = 16;
|
---|
139 | String[] args = url.getText().substring(url.getText().indexOf('?')+1).split("&");
|
---|
140 | for (String arg : args) {
|
---|
141 | int eq = arg.indexOf('=');
|
---|
142 | if (eq == -1 || !arg.substring(0, eq).equalsIgnoreCase("zoom")) continue;
|
---|
143 |
|
---|
144 | zoomLvl = Integer.parseInt(arg.substring(eq + 1));
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // 10000000 = 10 000 * 1000 = World * (km -> m)
|
---|
149 | zm.setText(Double.toString(Math.round(10000000 * Math.pow(2, (-1) * zoomLvl))));
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | private void updateUrl(boolean force) {
|
---|
154 | if(!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
|
---|
155 | try {
|
---|
156 | double dlat = Double.parseDouble(lat.getText());
|
---|
157 | double dlon = Double.parseDouble(lon.getText());
|
---|
158 | int zoomLvl = getZoom(zoomFactor * Double.parseDouble(zm.getText()));
|
---|
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 | public void actionPerformed(ActionEvent e) {
|
---|
170 | showJumpToDialog();
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Converts a given scale into OSM-Style zoom factors
|
---|
175 | * @param double scale
|
---|
176 | */
|
---|
177 | public int getZoom(double scale) {
|
---|
178 | double sizex = scale * Main.map.mapView.getWidth();
|
---|
179 | double sizey = scale * Main.map.mapView.getHeight();
|
---|
180 | for (int zoom = 0; zoom <= 32; zoom++, sizex *= 2, sizey *= 2)
|
---|
181 | if (sizex > Main.map.mapView.world.east() || sizey > Main.map.mapView.world.north())
|
---|
182 | return zoom;
|
---|
183 | return 32;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public void mousePressed(MouseEvent e) {}
|
---|
187 |
|
---|
188 | public void mouseReleased(MouseEvent e) {}
|
---|
189 |
|
---|
190 | public void mouseEntered(MouseEvent e) {}
|
---|
191 |
|
---|
192 | public void mouseExited(MouseEvent e) {}
|
---|
193 |
|
---|
194 | public void mouseClicked(MouseEvent e) {
|
---|
195 | showJumpToDialog();
|
---|
196 | }
|
---|
197 | }
|
---|