source: josm/trunk/src/org/openstreetmap/josm/actions/JumpToAction.java@ 6340

Last change on this file since 6340 was 6267, checked in by Don-vip, 11 years ago

Sonar/FindBugs - Replace singular fields by local variables

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