1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.dialogs;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.awt.event.FocusEvent;
|
---|
9 | import java.awt.event.FocusListener;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.util.Arrays;
|
---|
13 | import java.util.Optional;
|
---|
14 |
|
---|
15 | import javax.swing.BorderFactory;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JSeparator;
|
---|
19 | import javax.swing.JTabbedPane;
|
---|
20 | import javax.swing.text.JTextComponent;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
23 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
24 | import org.openstreetmap.josm.data.coor.conversion.CoordinateFormatManager;
|
---|
25 | import org.openstreetmap.josm.data.coor.conversion.LatLonParser;
|
---|
26 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
---|
27 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
28 | import org.openstreetmap.josm.gui.util.WindowGeometry;
|
---|
29 | import org.openstreetmap.josm.gui.widgets.AbstractTextComponentValidator;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
31 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 | import org.openstreetmap.josm.tools.Logging;
|
---|
34 | import org.openstreetmap.josm.tools.Utils;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A dialog that lets the user add a node at the coordinates he enters.
|
---|
38 | */
|
---|
39 | public class LatLonDialog extends ExtendedDialog {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * The tabs that define the coordinate mode.
|
---|
43 | */
|
---|
44 | public JTabbedPane tabs;
|
---|
45 | private JosmTextField tfLatLon, tfEastNorth;
|
---|
46 | private LatLon latLonCoordinates;
|
---|
47 | private LatLonValidator latLonValidator;
|
---|
48 | private EastNorth eastNorthCoordinates;
|
---|
49 | private EastNorthValidator eastNorthValidator;
|
---|
50 |
|
---|
51 | protected JPanel buildLatLon() {
|
---|
52 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
53 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
54 |
|
---|
55 | pnl.add(new JLabel(tr("Coordinates:")), GBC.std().insets(0, 10, 5, 0));
|
---|
56 | tfLatLon = new JosmTextField(24);
|
---|
57 | pnl.add(tfLatLon, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL).weight(1.0, 0.0));
|
---|
58 |
|
---|
59 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
60 |
|
---|
61 | pnl.add(new HtmlPanel(
|
---|
62 | String.join("<br/>",
|
---|
63 | tr("Enter the coordinates for the new node."),
|
---|
64 | tr("You can separate longitude and latitude with space, comma or semicolon."),
|
---|
65 | tr("Use positive numbers or N, E characters to indicate North or East cardinal direction."),
|
---|
66 | tr("For South and West cardinal directions you can use either negative numbers or S, W characters."),
|
---|
67 | tr("Coordinate value can be in one of three formats:")
|
---|
68 | ) +
|
---|
69 | Utils.joinAsHtmlUnorderedList(Arrays.asList(
|
---|
70 | tr("<i>degrees</i><tt>°</tt>"),
|
---|
71 | tr("<i>degrees</i><tt>°</tt> <i>minutes</i><tt>'</tt>"),
|
---|
72 | tr("<i>degrees</i><tt>°</tt> <i>minutes</i><tt>'</tt> <i>seconds</i><tt>"</tt>")
|
---|
73 | )) +
|
---|
74 | String.join("<br/><br/>",
|
---|
75 | tr("Symbols <tt>°</tt>, <tt>'</tt>, <tt>′</tt>, <tt>"</tt>, <tt>″</tt> are optional."),
|
---|
76 | tr("You can also use the syntax <tt>lat=\"...\" lon=\"...\"</tt> or <tt>lat=''...'' lon=''...''</tt>."),
|
---|
77 | tr("Some examples:")
|
---|
78 | ) +
|
---|
79 | "<table><tr><td>" +
|
---|
80 | Utils.joinAsHtmlUnorderedList(Arrays.asList(
|
---|
81 | "49.29918 19.24788",
|
---|
82 | "49.29918, 19.24788",
|
---|
83 | "49.29918° 19.24788°",
|
---|
84 | "N 49.29918 E 19.24788",
|
---|
85 | "W 49°29.918' S 19°24.788'",
|
---|
86 | "N 49°29'04" E 19°24'43"",
|
---|
87 | "49.29918 N, 19.24788 E",
|
---|
88 | "49°29'21" N 19°24'38" E",
|
---|
89 | "49 29 51, 19 24 18",
|
---|
90 | "49 29, 19 24"
|
---|
91 | )) +
|
---|
92 | "</td><td>" +
|
---|
93 | Utils.joinAsHtmlUnorderedList(Arrays.asList(
|
---|
94 | "E 49 29, N 19 24",
|
---|
95 | "49° 29; 19° 24",
|
---|
96 | "N 49° 29, W 19° 24",
|
---|
97 | "49° 29.5 S, 19° 24.6 E",
|
---|
98 | "N 49 29.918 E 19 15.88",
|
---|
99 | "49 29.4 19 24.5",
|
---|
100 | "-49 29.4 N -19 24.5 W",
|
---|
101 | "48 deg 42' 52.13\" N, 21 deg 11' 47.60\" E",
|
---|
102 | "lat=\"49.29918\" lon=\"19.24788\"",
|
---|
103 | "lat='49.29918' lon='19.24788'"
|
---|
104 | )) +
|
---|
105 | "</td></tr></table>"),
|
---|
106 | GBC.eol().fill().weight(1.0, 1.0));
|
---|
107 |
|
---|
108 | // parse and verify input on the fly
|
---|
109 | latLonValidator = new LatLonValidator(tfLatLon);
|
---|
110 |
|
---|
111 | // select the text in the field on focus
|
---|
112 | //
|
---|
113 | TextFieldFocusHandler focusHandler = new TextFieldFocusHandler();
|
---|
114 | tfLatLon.addFocusListener(focusHandler);
|
---|
115 | return pnl;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private JPanel buildEastNorth() {
|
---|
119 | JPanel pnl = new JPanel(new GridBagLayout());
|
---|
120 | pnl.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
|
---|
121 |
|
---|
122 | pnl.add(new JLabel(tr("Projected coordinates:")), GBC.std().insets(0, 10, 5, 0));
|
---|
123 | tfEastNorth = new JosmTextField(24);
|
---|
124 |
|
---|
125 | pnl.add(tfEastNorth, GBC.eol().insets(0, 10, 0, 0).fill(GBC.HORIZONTAL).weight(1.0, 0.0));
|
---|
126 |
|
---|
127 | pnl.add(new JSeparator(), GBC.eol().fill(GBC.HORIZONTAL).insets(0, 5, 0, 5));
|
---|
128 |
|
---|
129 | pnl.add(new HtmlPanel(
|
---|
130 | tr("Enter easting and northing (x and y) separated by space, comma or semicolon.")),
|
---|
131 | GBC.eol().fill(GBC.HORIZONTAL));
|
---|
132 |
|
---|
133 | pnl.add(GBC.glue(1, 1), GBC.eol().fill().weight(1.0, 1.0));
|
---|
134 |
|
---|
135 | eastNorthValidator = new EastNorthValidator(tfEastNorth);
|
---|
136 |
|
---|
137 | TextFieldFocusHandler focusHandler = new TextFieldFocusHandler();
|
---|
138 | tfEastNorth.addFocusListener(focusHandler);
|
---|
139 |
|
---|
140 | return pnl;
|
---|
141 | }
|
---|
142 |
|
---|
143 | protected void build() {
|
---|
144 | tabs = new JTabbedPane();
|
---|
145 | tabs.addTab(tr("Lat/Lon"), buildLatLon());
|
---|
146 | tabs.addTab(tr("East/North"), buildEastNorth());
|
---|
147 | tabs.getModel().addChangeListener(e -> {
|
---|
148 | switch (tabs.getModel().getSelectedIndex()) {
|
---|
149 | case 0: latLonValidator.validate(); break;
|
---|
150 | case 1: eastNorthValidator.validate(); break;
|
---|
151 | default: throw new AssertionError();
|
---|
152 | }
|
---|
153 | });
|
---|
154 | setContent(tabs, false);
|
---|
155 | addWindowListener(new WindowAdapter() {
|
---|
156 | @Override
|
---|
157 | public void windowOpened(WindowEvent e) {
|
---|
158 | tfLatLon.requestFocusInWindow();
|
---|
159 | }
|
---|
160 | });
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Creates a new {@link LatLonDialog}
|
---|
165 | * @param parent The parent
|
---|
166 | * @param title The title of this dialog
|
---|
167 | * @param help The help text to use
|
---|
168 | */
|
---|
169 | public LatLonDialog(Component parent, String title, String help) {
|
---|
170 | super(parent, title, tr("Ok"), tr("Cancel"));
|
---|
171 | setButtonIcons("ok", "cancel");
|
---|
172 | configureContextsensitiveHelp(help, true);
|
---|
173 |
|
---|
174 | build();
|
---|
175 | setCoordinates(null);
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Check if lat/lon mode is active
|
---|
180 | * @return <code>true</code> iff the user selects lat/lon coordinates
|
---|
181 | */
|
---|
182 | public boolean isLatLon() {
|
---|
183 | return tabs.getModel().getSelectedIndex() == 0;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Sets the coordinate fields to the given coordinates
|
---|
188 | * @param ll The lat/lon coordinates
|
---|
189 | */
|
---|
190 | public void setCoordinates(LatLon ll) {
|
---|
191 | LatLon llc = Optional.ofNullable(ll).orElse(LatLon.ZERO);
|
---|
192 | tfLatLon.setText(CoordinateFormatManager.getDefaultFormat().latToString(llc) + ' ' +
|
---|
193 | CoordinateFormatManager.getDefaultFormat().lonToString(llc));
|
---|
194 | EastNorth en = ProjectionRegistry.getProjection().latlon2eastNorth(llc);
|
---|
195 | tfEastNorth.setText(Double.toString(en.east()) + ' ' + Double.toString(en.north()));
|
---|
196 | // Both latLonCoordinates and eastNorthCoordinates may have been reset to null if ll is out of the world
|
---|
197 | latLonCoordinates = llc;
|
---|
198 | eastNorthCoordinates = en;
|
---|
199 | setOkEnabled(true);
|
---|
200 | }
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Gets the coordinates that are entered by the user.
|
---|
204 | * @return The coordinates
|
---|
205 | */
|
---|
206 | public LatLon getCoordinates() {
|
---|
207 | if (isLatLon()) {
|
---|
208 | return latLonCoordinates;
|
---|
209 | } else {
|
---|
210 | if (eastNorthCoordinates == null) return null;
|
---|
211 | return ProjectionRegistry.getProjection().eastNorth2latlon(eastNorthCoordinates);
|
---|
212 | }
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Gets the coordinates that are entered in the lat/lon field
|
---|
217 | * @return The lat/lon coordinates
|
---|
218 | */
|
---|
219 | public LatLon getLatLonCoordinates() {
|
---|
220 | return latLonCoordinates;
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Gets the coordinates that are entered in the east/north field
|
---|
225 | * @return The east/north coordinates
|
---|
226 | */
|
---|
227 | public EastNorth getEastNorthCoordinates() {
|
---|
228 | return eastNorthCoordinates;
|
---|
229 | }
|
---|
230 |
|
---|
231 | private class LatLonValidator extends AbstractTextComponentValidator {
|
---|
232 | LatLonValidator(JTextComponent tc) {
|
---|
233 | super(tc);
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public void validate() {
|
---|
238 | LatLon latLon;
|
---|
239 | try {
|
---|
240 | latLon = LatLonParser.parse(tfLatLon.getText());
|
---|
241 | if (!LatLon.isValidLat(latLon.lat()) || !LatLon.isValidLon(latLon.lon())) {
|
---|
242 | latLon = null;
|
---|
243 | }
|
---|
244 | } catch (IllegalArgumentException e) {
|
---|
245 | Logging.trace(e);
|
---|
246 | latLon = null;
|
---|
247 | }
|
---|
248 | if (latLon == null) {
|
---|
249 | feedbackInvalid(tr("Please enter a GPS coordinates"));
|
---|
250 | latLonCoordinates = null;
|
---|
251 | setOkEnabled(false);
|
---|
252 | } else {
|
---|
253 | feedbackValid(null);
|
---|
254 | latLonCoordinates = latLon;
|
---|
255 | setOkEnabled(true);
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public boolean isValid() {
|
---|
261 | throw new UnsupportedOperationException();
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | private class EastNorthValidator extends AbstractTextComponentValidator {
|
---|
266 | EastNorthValidator(JTextComponent tc) {
|
---|
267 | super(tc);
|
---|
268 | }
|
---|
269 |
|
---|
270 | @Override
|
---|
271 | public void validate() {
|
---|
272 | EastNorth en;
|
---|
273 | try {
|
---|
274 | en = parseEastNorth(tfEastNorth.getText());
|
---|
275 | } catch (IllegalArgumentException e) {
|
---|
276 | Logging.trace(e);
|
---|
277 | en = null;
|
---|
278 | }
|
---|
279 | if (en == null) {
|
---|
280 | feedbackInvalid(tr("Please enter a Easting and Northing"));
|
---|
281 | latLonCoordinates = null;
|
---|
282 | setOkEnabled(false);
|
---|
283 | } else {
|
---|
284 | feedbackValid(null);
|
---|
285 | eastNorthCoordinates = en;
|
---|
286 | setOkEnabled(true);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | @Override
|
---|
291 | public boolean isValid() {
|
---|
292 | throw new UnsupportedOperationException();
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | private void setOkEnabled(boolean b) {
|
---|
297 | if (buttons != null && !buttons.isEmpty()) {
|
---|
298 | buttons.get(0).setEnabled(b);
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | @Override
|
---|
303 | public void setVisible(boolean visible) {
|
---|
304 | final String preferenceKey = getClass().getName() + ".geometry";
|
---|
305 | if (visible) {
|
---|
306 | new WindowGeometry(
|
---|
307 | preferenceKey,
|
---|
308 | WindowGeometry.centerInWindow(getParent(), getSize())
|
---|
309 | ).applySafe(this);
|
---|
310 | } else {
|
---|
311 | new WindowGeometry(this).remember(preferenceKey);
|
---|
312 | }
|
---|
313 | super.setVisible(visible);
|
---|
314 | }
|
---|
315 |
|
---|
316 | static class TextFieldFocusHandler implements FocusListener {
|
---|
317 | @Override
|
---|
318 | public void focusGained(FocusEvent e) {
|
---|
319 | Component c = e.getComponent();
|
---|
320 | if (c instanceof JosmTextField) {
|
---|
321 | JosmTextField tf = (JosmTextField) c;
|
---|
322 | tf.selectAll();
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | @Override
|
---|
327 | public void focusLost(FocusEvent e) {
|
---|
328 | // Not used
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | /**
|
---|
333 | * Parses a east/north coordinate string
|
---|
334 | * @param s The coordinates. Dot has to be used as decimal separator, as comma can be used to delimit values
|
---|
335 | * @return The east/north coordinates or <code>null</code> on error.
|
---|
336 | */
|
---|
337 | public static EastNorth parseEastNorth(String s) {
|
---|
338 | String[] en = s.split("[;, ]+", -1);
|
---|
339 | if (en.length != 2) return null;
|
---|
340 | try {
|
---|
341 | double east = Double.parseDouble(en[0]);
|
---|
342 | double north = Double.parseDouble(en[1]);
|
---|
343 | return new EastNorth(east, north);
|
---|
344 | } catch (NumberFormatException nfe) {
|
---|
345 | return null;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Gets the text entered in the lat/lon text field.
|
---|
351 | * @return The text the user entered
|
---|
352 | */
|
---|
353 | public String getLatLonText() {
|
---|
354 | return tfLatLon.getText();
|
---|
355 | }
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Set the text in the lat/lon text field.
|
---|
359 | * @param text The new text
|
---|
360 | */
|
---|
361 | public void setLatLonText(String text) {
|
---|
362 | tfLatLon.setText(text);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /**
|
---|
366 | * Gets the text entered in the east/north text field.
|
---|
367 | * @return The text the user entered
|
---|
368 | */
|
---|
369 | public String getEastNorthText() {
|
---|
370 | return tfEastNorth.getText();
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Set the text in the east/north text field.
|
---|
375 | * @param text The new text
|
---|
376 | */
|
---|
377 | public void setEastNorthText(String text) {
|
---|
378 | tfEastNorth.setText(text);
|
---|
379 | }
|
---|
380 | }
|
---|