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

Last change on this file since 12792 was 12792, checked in by bastiK, 7 years ago

closes #15273, see #15229, see #15182 - add command line interface module for projections

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