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

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

checkstyle - enable CatchParameterName rule

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