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

Last change on this file was 17188, checked in by Klumbumbus, 4 years ago

fix #19851 - Fix shortcut names

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