| 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.data.coor.LatLon;
|
|---|
| 14 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
|---|
| 15 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
|---|
| 16 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 17 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 18 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
|---|
| 19 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 20 | import org.openstreetmap.josm.tools.OpenBrowser;
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * Marker class with Web URL activation.
|
|---|
| 24 | *
|
|---|
| 25 | * @author Frederik Ramm
|
|---|
| 26 | * @since 200
|
|---|
| 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, /* ICON(markers/) */ "web", parentLayer, time, offset);
|
|---|
| 34 | CheckParameterUtil.ensureParameterNotNull(webUrl, "webUrl");
|
|---|
| 35 | this.webUrl = webUrl;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | @Override
|
|---|
| 39 | public void actionPerformed(ActionEvent ev) {
|
|---|
| 40 | String error = OpenBrowser.displayUrl(webUrl.toString());
|
|---|
| 41 | if (error != null) {
|
|---|
| 42 | setErroneous(true);
|
|---|
| 43 | new Notification(
|
|---|
| 44 | "<b>" + tr("There was an error while trying to display the URL for this marker") + "</b><br>" +
|
|---|
| 45 | tr("(URL was: ") + webUrl + ')' + "<br>" + error)
|
|---|
| 46 | .setIcon(JOptionPane.ERROR_MESSAGE)
|
|---|
| 47 | .setDuration(Notification.TIME_LONG)
|
|---|
| 48 | .show();
|
|---|
| 49 | } else {
|
|---|
| 50 | updateErroneous();
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | public WayPoint convertToWayPoint() {
|
|---|
| 56 | WayPoint wpt = super.convertToWayPoint();
|
|---|
| 57 | GpxLink link = new GpxLink(webUrl.toString());
|
|---|
| 58 | link.type = "web";
|
|---|
| 59 | wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
|
|---|
| 60 | return wpt;
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | private void updateErroneous() {
|
|---|
| 64 | if ("file".equals(webUrl.getProtocol())) {
|
|---|
| 65 | String path = webUrl.getPath();
|
|---|
| 66 | try {
|
|---|
| 67 | setErroneous(path.isEmpty() || !new File(path).exists());
|
|---|
| 68 | } catch (SecurityException e) {
|
|---|
| 69 | Logging.warn(e);
|
|---|
| 70 | setErroneous(true);
|
|---|
| 71 | }
|
|---|
| 72 | } else {
|
|---|
| 73 | setErroneous(false);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|