001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.layer.markerlayer; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.io.File; 008import java.net.URL; 009import java.util.Collections; 010 011import javax.swing.JOptionPane; 012 013import org.openstreetmap.josm.Main; 014import org.openstreetmap.josm.data.coor.LatLon; 015import org.openstreetmap.josm.data.gpx.GpxConstants; 016import org.openstreetmap.josm.data.gpx.GpxLink; 017import org.openstreetmap.josm.data.gpx.WayPoint; 018import org.openstreetmap.josm.gui.Notification; 019import org.openstreetmap.josm.tools.CheckParameterUtil; 020import org.openstreetmap.josm.tools.OpenBrowser; 021 022/** 023 * Marker class with Web URL activation. 024 * 025 * @author Frederik Ramm 026 * @since 200 027 */ 028public class WebMarker extends ButtonMarker { 029 030 private final URL webUrl; 031 032 public WebMarker(LatLon ll, URL webUrl, MarkerLayer parentLayer, double time, double offset) { 033 super(ll, "web", parentLayer, time, offset); 034 CheckParameterUtil.ensureParameterNotNull(webUrl, "webUrl"); 035 this.webUrl = webUrl; 036 } 037 038 @Override 039 public void actionPerformed(ActionEvent ev) { 040 String error = OpenBrowser.displayUrl(webUrl.toString()); 041 if (error != null) { 042 setErroneous(true); 043 new Notification( 044 "<b>" + tr("There was an error while trying to display the URL for this marker") + "</b><br>" + 045 tr("(URL was: ") + webUrl + ')' + "<br>" + error) 046 .setIcon(JOptionPane.ERROR_MESSAGE) 047 .setDuration(Notification.TIME_LONG) 048 .show(); 049 } else { 050 updateErroneous(); 051 } 052 } 053 054 @Override 055 public WayPoint convertToWayPoint() { 056 WayPoint wpt = super.convertToWayPoint(); 057 GpxLink link = new GpxLink(webUrl.toString()); 058 link.type = "web"; 059 wpt.put(GpxConstants.META_LINKS, Collections.singleton(link)); 060 return wpt; 061 } 062 063 private void updateErroneous() { 064 if ("file".equals(webUrl.getProtocol())) { 065 String path = webUrl.getPath(); 066 try { 067 setErroneous(path.isEmpty() || !new File(path).exists()); 068 } catch (SecurityException e) { 069 Main.warn(e); 070 setErroneous(true); 071 } 072 } else { 073 setErroneous(false); 074 } 075 } 076}