source: osm/applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/JumpToAction.java@ 16879

Last change on this file since 16879 was 16687, checked in by stoecker, 16 years ago

fixes for newer JOSM

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