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