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

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

see #15182 - deprecate shortcut handling and mapframe listener methods in Main. Replacement: same methods in gui.MainApplication

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