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.UncheckedIOException;
008import java.net.URI;
009import java.net.URISyntaxException;
010import java.net.URL;
011import java.time.Instant;
012import java.util.Collections;
013import java.util.function.Supplier;
014
015import javax.swing.JOptionPane;
016
017import org.openstreetmap.josm.data.coor.LatLon;
018import org.openstreetmap.josm.data.gpx.GpxConstants;
019import org.openstreetmap.josm.data.gpx.GpxLink;
020import org.openstreetmap.josm.data.gpx.WayPoint;
021import org.openstreetmap.josm.data.imagery.street_level.IImageEntry;
022import org.openstreetmap.josm.gui.Notification;
023import org.openstreetmap.josm.gui.layer.geoimage.ImageViewerDialog;
024import org.openstreetmap.josm.gui.layer.geoimage.RemoteEntry;
025import org.openstreetmap.josm.tools.Logging;
026import org.openstreetmap.josm.tools.Utils;
027
028/**
029 * Marker representing an image. Uses a special icon, and when clicked,
030 * displays an image view dialog. Re-uses some code from GeoImageLayer.
031 *
032 * @author Frederik Ramm
033 *
034 */
035public class ImageMarker extends ButtonMarker {
036
037    public URL imageUrl;
038
039    public ImageMarker(LatLon ll, URL imageUrl, MarkerLayer parentLayer, double time, double offset) {
040        super(ll, /* ICON(markers/) */ "photo", parentLayer, time, offset);
041        this.imageUrl = imageUrl;
042    }
043
044    @Override
045    public void actionPerformed(ActionEvent ev) {
046        this.parentLayer.setCurrentMarker(this);
047        ImageViewerDialog.getInstance().displayImages(Collections.singletonList(getRemoteEntry()));
048    }
049
050    RemoteEntry getRemoteEntry() {
051        try {
052            final RemoteEntry remoteEntry = new MarkerRemoteEntry(imageUrl.toURI(), getFirstImage(), getPreviousImage(),
053                    getNextImage(), getLastImage());
054            // First, extract EXIF data
055            remoteEntry.extractExif();
056            // Then, apply information from this point. This may overwrite details from
057            // the exif, but that will (hopefully) be OK.
058            if (Double.isFinite(this.time)) {
059                remoteEntry.setGpsTime(Instant.ofEpochMilli((long) (this.time * 1000)));
060            }
061            if (this.isLatLonKnown()) {
062                remoteEntry.setPos(this);
063            }
064            if (!Utils.isStripEmpty(this.getText())) {
065                remoteEntry.setDisplayName(this.getText());
066            }
067            return remoteEntry;
068        } catch (URISyntaxException e) {
069            Logging.trace(e);
070            new Notification(tr("Malformed URI: {0}", this.imageUrl.toExternalForm())).setIcon(JOptionPane.WARNING_MESSAGE).show();
071        } catch (UncheckedIOException e) {
072            Logging.trace(e);
073            new Notification(tr("IO Exception: {0}\n{1}", this.imageUrl.toExternalForm(), e.getCause().getMessage()))
074                    .setIcon(JOptionPane.WARNING_MESSAGE).show();
075        }
076        return null;
077    }
078
079    private Supplier<RemoteEntry> getFirstImage() {
080        for (Marker marker : this.parentLayer.data) {
081            if (marker instanceof ImageMarker) {
082                if (marker == this) {
083                    break;
084                }
085                ImageMarker imageMarker = (ImageMarker) marker;
086                return imageMarker::getRemoteEntry;
087            }
088        }
089        return () -> null;
090    }
091
092    private Supplier<RemoteEntry> getPreviousImage() {
093        int index = this.parentLayer.data.indexOf(this);
094        for (int i = index - 1; i >= 0; i--) {
095            Marker marker = this.parentLayer.data.get(i);
096            if (marker instanceof ImageMarker) {
097                ImageMarker imageMarker = (ImageMarker) marker;
098                return imageMarker::getRemoteEntry;
099            }
100        }
101        return () -> null;
102    }
103
104    private Supplier<RemoteEntry> getNextImage() {
105        int index = this.parentLayer.data.indexOf(this);
106        for (int i = index + 1; i < this.parentLayer.data.size(); i++) {
107            Marker marker = this.parentLayer.data.get(i);
108            if (marker instanceof ImageMarker) {
109                ImageMarker imageMarker = (ImageMarker) marker;
110                return imageMarker::getRemoteEntry;
111            }
112        }
113        return () -> null;
114    }
115
116    private Supplier<RemoteEntry> getLastImage() {
117        int index = Math.max(0, this.parentLayer.data.indexOf(this));
118        for (int i = this.parentLayer.data.size() - 1; i >= index; i--) {
119            Marker marker = this.parentLayer.data.get(i);
120            if (marker instanceof ImageMarker) {
121                if (marker == this) {
122                    break;
123                }
124                ImageMarker imageMarker = (ImageMarker) marker;
125                return imageMarker::getRemoteEntry;
126            }
127        }
128        return () -> null;
129    }
130
131    @Override
132    public WayPoint convertToWayPoint() {
133        WayPoint wpt = super.convertToWayPoint();
134        GpxLink link = new GpxLink(imageUrl.toString());
135        link.type = "image";
136        wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
137        return wpt;
138    }
139
140    private class MarkerRemoteEntry extends RemoteEntry {
141        /**
142         * Create a new remote entry
143         *
144         * @param uri           The URI to use
145         * @param firstImage    first image supplier
146         * @param previousImage previous image supplier
147         * @param nextImage     next image supplier
148         * @param lastImage     last image supplier
149         */
150        MarkerRemoteEntry(URI uri, Supplier<RemoteEntry> firstImage, Supplier<RemoteEntry> previousImage,
151                                 Supplier<RemoteEntry> nextImage, Supplier<RemoteEntry> lastImage) {
152            super(uri, firstImage, previousImage, nextImage, lastImage);
153        }
154
155        @Override
156        public void selectImage(ImageViewerDialog imageViewerDialog, IImageEntry<?> entry) {
157            ImageMarker.this.parentLayer.setCurrentMarker(ImageMarker.this);
158            super.selectImage(imageViewerDialog, entry);
159        }
160    }
161}