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