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

Last change on this file since 13417 was 13417, checked in by Don-vip, 6 years ago

fix #14224: advanced preference jumpto.use.clipboard to disable "Jump to Position: automatically paste URL from clipboard" feature

  • Property svn:eol-style set to native
File size: 7.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.BorderLayout;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.KeyEvent;
11import java.util.Optional;
12
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.event.DocumentEvent;
17import javax.swing.event.DocumentListener;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.Bounds;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
23import org.openstreetmap.josm.gui.ExtendedDialog;
24import org.openstreetmap.josm.gui.MainApplication;
25import org.openstreetmap.josm.gui.MapView;
26import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
27import org.openstreetmap.josm.gui.widgets.JosmTextField;
28import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
29import org.openstreetmap.josm.spi.preferences.Config;
30import org.openstreetmap.josm.tools.GBC;
31import org.openstreetmap.josm.tools.ImageProvider;
32import org.openstreetmap.josm.tools.Logging;
33import org.openstreetmap.josm.tools.OsmUrlToBounds;
34import org.openstreetmap.josm.tools.Shortcut;
35
36/**
37 * Allows to jump to a specific location.
38 * @since 2575
39 */
40public class JumpToAction extends JosmAction {
41
42 private final JosmTextField url = new JosmTextField();
43 private final JosmTextField lat = new JosmTextField();
44 private final JosmTextField lon = new JosmTextField();
45 private final JosmTextField zm = new JosmTextField();
46
47 /**
48 * Constructs a new {@code JumpToAction}.
49 */
50 public JumpToAction() {
51 super(tr("Jump To Position"), (ImageProvider) null, tr("Opens a dialog that allows to jump to a specific location"),
52 Shortcut.registerShortcut("tools:jumpto", tr("Tool: {0}", tr("Jump To Position")),
53 KeyEvent.VK_J, Shortcut.CTRL), true, "action/jumpto", true);
54 putValue("help", ht("/Action/JumpToPosition"));
55 }
56
57 static class JumpToPositionDialog extends ExtendedDialog {
58 JumpToPositionDialog(String[] buttons, JPanel panel) {
59 super(Main.parent, tr("Jump to Position"), buttons);
60 setContent(panel);
61 setCancelButton(2);
62 }
63 }
64
65 class OsmURLListener implements DocumentListener {
66 @Override
67 public void changedUpdate(DocumentEvent e) {
68 parseURL();
69 }
70
71 @Override
72 public void insertUpdate(DocumentEvent e) {
73 parseURL();
74 }
75
76 @Override
77 public void removeUpdate(DocumentEvent e) {
78 parseURL();
79 }
80 }
81
82 class OsmLonLatListener implements DocumentListener {
83 @Override
84 public void changedUpdate(DocumentEvent e) {
85 updateUrl(false);
86 }
87
88 @Override
89 public void insertUpdate(DocumentEvent e) {
90 updateUrl(false);
91 }
92
93 @Override
94 public void removeUpdate(DocumentEvent e) {
95 updateUrl(false);
96 }
97 }
98
99 /**
100 * Displays the "Jump to" dialog.
101 */
102 public void showJumpToDialog() {
103 if (!MainApplication.isDisplayingMapView()) {
104 return;
105 }
106 MapView mv = MainApplication.getMap().mapView;
107
108 final Optional<Bounds> boundsFromClipboard = Optional
109 .ofNullable(ClipboardUtils.getClipboardStringContent())
110 .map(OsmUrlToBounds::parse);
111 if (boundsFromClipboard.isPresent() && Config.getPref().getBoolean("jumpto.use.clipboard", true)) {
112 setBounds(boundsFromClipboard.get());
113 } else {
114 setBounds(mv.getState().getViewArea().getCornerBounds());
115 }
116 updateUrl(true);
117
118 JPanel panel = new JPanel(new BorderLayout());
119 panel.add(new JLabel("<html>"
120 + tr("Enter Lat/Lon to jump to position.")
121 + "<br>"
122 + tr("You can also paste an URL from www.openstreetmap.org")
123 + "<br>"
124 + "</html>"),
125 BorderLayout.NORTH);
126
127 OsmLonLatListener x = new OsmLonLatListener();
128 lat.getDocument().addDocumentListener(x);
129 lon.getDocument().addDocumentListener(x);
130 zm.getDocument().addDocumentListener(x);
131 url.getDocument().addDocumentListener(new OsmURLListener());
132
133 SelectAllOnFocusGainedDecorator.decorate(lat);
134 SelectAllOnFocusGainedDecorator.decorate(lon);
135 SelectAllOnFocusGainedDecorator.decorate(zm);
136 SelectAllOnFocusGainedDecorator.decorate(url);
137
138 JPanel p = new JPanel(new GridBagLayout());
139 panel.add(p, BorderLayout.NORTH);
140
141 p.add(new JLabel(tr("Latitude")), GBC.eol());
142 p.add(lat, GBC.eol().fill(GBC.HORIZONTAL));
143
144 p.add(new JLabel(tr("Longitude")), GBC.eol());
145 p.add(lon, GBC.eol().fill(GBC.HORIZONTAL));
146
147 p.add(new JLabel(tr("Zoom (in metres)")), GBC.eol());
148 p.add(zm, GBC.eol().fill(GBC.HORIZONTAL));
149
150 p.add(new JLabel(tr("URL")), GBC.eol());
151 p.add(url, GBC.eol().fill(GBC.HORIZONTAL));
152
153 String[] buttons = {tr("Jump there"), tr("Cancel")};
154 LatLon ll = null;
155 double zoomLvl = 100;
156 while (ll == null) {
157 final int option = new JumpToPositionDialog(buttons, panel).showDialog().getValue();
158
159 if (option != 1) return;
160 try {
161 zoomLvl = Double.parseDouble(zm.getText());
162 ll = new LatLon(Double.parseDouble(lat.getText()), Double.parseDouble(lon.getText()));
163 } catch (NumberFormatException ex) {
164 try {
165 ll = LatLonParser.parse(lat.getText() + "; " + lon.getText());
166 } catch (IllegalArgumentException ex2) {
167 JOptionPane.showMessageDialog(Main.parent,
168 tr("Could not parse Latitude, Longitude or Zoom. Please check."),
169 tr("Unable to parse Lon/Lat"), JOptionPane.ERROR_MESSAGE);
170 }
171 }
172 }
173
174 double zoomFactor = 1/ mv.getDist100Pixel();
175 mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
176 }
177
178 private void parseURL() {
179 if (!url.hasFocus()) return;
180 String urlText = url.getText();
181 Bounds b = OsmUrlToBounds.parse(urlText);
182 setBounds(b);
183 }
184
185 private void setBounds(Bounds b) {
186 if (b != null) {
187 final LatLon center = b.getCenter();
188 lat.setText(Double.toString(center.lat()));
189 lon.setText(Double.toString(center.lon()));
190 zm.setText(Double.toString(OsmUrlToBounds.getZoom(b)));
191 }
192 }
193
194 private void updateUrl(boolean force) {
195 if (!lat.hasFocus() && !lon.hasFocus() && !zm.hasFocus() && !force) return;
196 try {
197 double dlat = Double.parseDouble(lat.getText());
198 double dlon = Double.parseDouble(lon.getText());
199 double zoomLvl = Double.parseDouble(zm.getText());
200 url.setText(OsmUrlToBounds.getURL(dlat, dlon, (int) zoomLvl));
201 } catch (NumberFormatException e) {
202 Logging.debug(e.getMessage());
203 }
204 }
205
206 @Override
207 public void actionPerformed(ActionEvent e) {
208 showJumpToDialog();
209 }
210
211 @Override
212 protected void updateEnabledState() {
213 setEnabled(MainApplication.isDisplayingMapView());
214 }
215
216 @Override
217 protected void installAdapters() {
218 super.installAdapters();
219 // make this action listen to mapframe change events
220 MainApplication.addMapFrameListener((o, n) -> updateEnabledState());
221 }
222}
Note: See TracBrowser for help on using the repository browser.