1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.File;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.Collections;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
15 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
17 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
18 | import org.openstreetmap.josm.gui.Notification;
|
---|
19 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
20 | import org.openstreetmap.josm.tools.OpenBrowser;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Marker class with Web URL activation.
|
---|
24 | *
|
---|
25 | * @author Frederik Ramm
|
---|
26 | *
|
---|
27 | */
|
---|
28 | public class WebMarker extends ButtonMarker {
|
---|
29 |
|
---|
30 | private final URL webUrl;
|
---|
31 |
|
---|
32 | public WebMarker(LatLon ll, URL webUrl, MarkerLayer parentLayer, double time, double offset) {
|
---|
33 | super(ll, "web", parentLayer, time, offset);
|
---|
34 | CheckParameterUtil.ensureParameterNotNull(webUrl, "webUrl");
|
---|
35 | this.webUrl = webUrl;
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override public void actionPerformed(ActionEvent ev) {
|
---|
39 | String error = OpenBrowser.displayUrl(webUrl.toString());
|
---|
40 | if (error != null) {
|
---|
41 | setErroneous(true);
|
---|
42 | new Notification(
|
---|
43 | "<b>" + tr("There was an error while trying to display the URL for this marker") + "</b><br>" +
|
---|
44 | tr("(URL was: ") + webUrl.toString() + ")" + "<br>" + error)
|
---|
45 | .setIcon(JOptionPane.ERROR_MESSAGE)
|
---|
46 | .setDuration(Notification.TIME_LONG)
|
---|
47 | .show();
|
---|
48 | } else {
|
---|
49 | updateErroneous();
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public WayPoint convertToWayPoint() {
|
---|
55 | WayPoint wpt = super.convertToWayPoint();
|
---|
56 | GpxLink link = new GpxLink(webUrl.toString());
|
---|
57 | link.type = "web";
|
---|
58 | wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
|
---|
59 | return wpt;
|
---|
60 | }
|
---|
61 |
|
---|
62 | private final void updateErroneous() {
|
---|
63 | if ("file".equals(webUrl.getProtocol())) {
|
---|
64 | String path = webUrl.getPath();
|
---|
65 | try {
|
---|
66 | setErroneous(path.isEmpty() || !new File(path).exists());
|
---|
67 | } catch (Exception e) {
|
---|
68 | Main.warn(e);
|
---|
69 | setErroneous(true);
|
---|
70 | }
|
---|
71 | } else {
|
---|
72 | setErroneous(false);
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|