001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.preferences.sources;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import org.openstreetmap.josm.tools.ImageResource;
007import org.openstreetmap.josm.tools.Utils;
008
009/**
010 * Source entry with additional metadata.
011 * @since 12649 (extracted from gui.preferences package)
012 */
013public class ExtendedSourceEntry extends SourceEntry implements Comparable<ExtendedSourceEntry> {
014    /** file name used for display */
015    public String simpleFileName;
016    /** version used for display */
017    public String version;
018    /** author name used for display */
019    public String author;
020    /** icon used for display */
021    public ImageResource icon;
022    /** webpage link used for display */
023    public String link;
024    /** short description used for display */
025    public String description;
026    /** Style type: can only have one value: "xml". Used to filter out old XML styles. For MapCSS styles, the value is not set. */
027    public String styleType;
028    /** minimum JOSM version required to enable this source entry */
029    public Integer minJosmVersion;
030
031    /**
032     * Constructs a new {@code ExtendedSourceEntry}.
033     * @param type type of source entry
034     * @param simpleFileName file name used for display
035     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
036     * @since 12825
037     */
038    public ExtendedSourceEntry(SourceType type, String simpleFileName, String url) {
039        super(type, url, null, null, true);
040        this.simpleFileName = simpleFileName;
041    }
042
043    /**
044     * @return string representation for GUI list or menu entry
045     */
046    public String getDisplayName() {
047        return title == null ? simpleFileName : title;
048    }
049
050    private static void appendRow(StringBuilder s, String th, String td) {
051        s.append("<tr><th>").append(th).append("</th><td>").append(Utils.escapeReservedCharactersHTML(td)).append("</td</tr>");
052    }
053
054    /**
055     * Returns a tooltip containing available metadata.
056     * @return a tooltip containing available metadata
057     */
058    public String getTooltip() {
059        StringBuilder s = new StringBuilder();
060        appendRow(s, tr("Short Description:"), getDisplayName());
061        appendRow(s, tr("URL:"), url);
062        if (author != null) {
063            appendRow(s, tr("Author:"), author);
064        }
065        if (link != null) {
066            appendRow(s, tr("Webpage:"), link);
067        }
068        if (description != null) {
069            appendRow(s, tr("Description:"), description);
070        }
071        if (version != null) {
072            appendRow(s, tr("Version:"), version);
073        }
074        if (minJosmVersion != null) {
075            appendRow(s, tr("Minimum JOSM Version:"), Integer.toString(minJosmVersion));
076        }
077        return "<html><style>th{text-align:right}td{width:400px}</style>"
078                + "<table>" + s + "</table></html>";
079    }
080
081    @Override
082    public String toString() {
083        return "<html><b>" + getDisplayName() + "</b>"
084                + (author == null ? "" : " <span color=\"gray\">" + tr("by {0}", author) + "</color>")
085                + "</html>";
086    }
087
088    @Override
089    public int compareTo(ExtendedSourceEntry o) {
090        if (url.startsWith("resource") && !o.url.startsWith("resource"))
091            return -1;
092        if (o.url.startsWith("resource"))
093            return 1;
094        else
095            return getDisplayName().compareToIgnoreCase(o.getDisplayName());
096    }
097}