001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.layer.geoimage;
003
004import java.awt.Image;
005import java.io.File;
006import java.util.Collections;
007import java.util.Objects;
008
009import org.openstreetmap.josm.data.gpx.GpxImageEntry;
010
011/**
012 * Stores info about each image, with an optional thumbnail
013 * @since 2662
014 */
015public final class ImageEntry extends GpxImageEntry {
016
017    private Image thumbnail;
018
019    /**
020     * Constructs a new {@code ImageEntry}.
021     */
022    public ImageEntry() {
023    }
024
025    /**
026     * Constructs a new {@code ImageEntry} from an existing instance.
027     * @param other existing instance
028     * @since 14625
029     */
030    public ImageEntry(ImageEntry other) {
031        super(other);
032        thumbnail = other.thumbnail;
033    }
034
035    /**
036     * Constructs a new {@code ImageEntry}.
037     * @param file Path to image file on disk
038     */
039    public ImageEntry(File file) {
040        super(file);
041    }
042
043    /**
044     * Determines whether a thumbnail is set
045     * @return {@code true} if a thumbnail is set
046     */
047    public boolean hasThumbnail() {
048        return thumbnail != null;
049    }
050
051    /**
052     * Returns the thumbnail.
053     * @return the thumbnail
054     */
055    public Image getThumbnail() {
056        return thumbnail;
057    }
058
059    /**
060     * Sets the thumbnail.
061     * @param thumbnail thumbnail
062     */
063    public void setThumbnail(Image thumbnail) {
064        this.thumbnail = thumbnail;
065    }
066
067    /**
068     * Loads the thumbnail if it was not loaded yet.
069     * @see ThumbsLoader
070     */
071    public void loadThumbnail() {
072        if (thumbnail == null) {
073            new ThumbsLoader(Collections.singleton(this)).run();
074        }
075    }
076
077    @Override
078    public int hashCode() {
079        return 31 * super.hashCode() + ((thumbnail == null) ? 0 : thumbnail.hashCode());
080    }
081
082    @Override
083    public boolean equals(Object obj) {
084        if (this == obj)
085            return true;
086        if (!super.equals(obj) || getClass() != obj.getClass())
087            return false;
088        ImageEntry other = (ImageEntry) obj;
089        return Objects.equals(thumbnail, other.thumbnail);
090    }
091}