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

Last change on this file since 14742 was 14742, checked in by simon04, 5 years ago

see #17178 - Jump to position: add help button

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