1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.datatransfer.importers;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.datatransfer.DataFlavor;
|
---|
6 | import java.awt.datatransfer.UnsupportedFlavorException;
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.util.regex.Matcher;
|
---|
9 | import java.util.regex.Pattern;
|
---|
10 |
|
---|
11 | import javax.swing.TransferHandler.TransferSupport;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.actions.OpenLocationAction;
|
---|
14 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.gui.MapView;
|
---|
17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Handles the paste / drop of an OSM address.
|
---|
21 | * <p>
|
---|
22 | * e.g. http://www.openstreetmap.org/node/123 downloads node 123
|
---|
23 | *
|
---|
24 | * @author Michael Zangl
|
---|
25 | * @since 10881
|
---|
26 | */
|
---|
27 | public class OsmLinkPaster extends AbstractOsmDataPaster {
|
---|
28 |
|
---|
29 | static final class NoWarnOpenLocationAction extends OpenLocationAction {
|
---|
30 | @Override
|
---|
31 | protected void warnNoSuitableTasks(String url) {
|
---|
32 | // ignore this.
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | private static final String OSM_SERVER = "^https?\\://(\\w+\\.)?(osm|openstreetmap)\\.org/";
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create a new Osm address paster
|
---|
40 | */
|
---|
41 | public OsmLinkPaster() {
|
---|
42 | super(DataFlavor.stringFlavor);
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public boolean importData(TransferSupport support, OsmDataLayer layer, EastNorth pasteAt)
|
---|
47 | throws UnsupportedFlavorException, IOException {
|
---|
48 | if (!supports(support)) {
|
---|
49 | throw new UnsupportedFlavorException(df);
|
---|
50 | }
|
---|
51 |
|
---|
52 | String transferData = (String) support.getTransferable().getTransferData(df);
|
---|
53 | if (!new NoWarnOpenLocationAction().openUrl(transferData).isEmpty()) {
|
---|
54 | return true;
|
---|
55 | }
|
---|
56 |
|
---|
57 | LatLon ll = parseLatLon(transferData);
|
---|
58 | if (ll != null) {
|
---|
59 | Component comp = support.getComponent();
|
---|
60 | if (comp instanceof MapView) {
|
---|
61 | ((MapView) comp).zoomTo(ll);
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | static LatLon parseLatLon(String transferData) {
|
---|
69 | Matcher matcher = Pattern
|
---|
70 | .compile(OSM_SERVER + "#map=(?<zoom>\\d+)/(?<lat>-?\\d+\\.\\d+)/(?<lon>-?\\d+\\.\\d+)$")
|
---|
71 | .matcher(transferData);
|
---|
72 |
|
---|
73 | if (!matcher.matches()) {
|
---|
74 | return null;
|
---|
75 | } else {
|
---|
76 | return new LatLon(Double.parseDouble(matcher.group("lat")), Double.parseDouble(matcher.group("lon")));
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|