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     * Returns string representation for GUI list or menu entry.
045     * @return string representation for GUI list or menu entry
046     */
047    public String getDisplayName() {
048        return title == null ? simpleFileName : title;
049    }
050
051    private static void appendRow(StringBuilder s, String th, String td) {
052        s.append("<tr><th>").append(th).append("</th><td>").append(Utils.escapeReservedCharactersHTML(td)).append("</td</tr>");
053    }
054
055    /**
056     * Returns a tooltip containing available metadata.
057     * @return a tooltip containing available metadata
058     */
059    public String getTooltip() {
060        StringBuilder s = new StringBuilder();
061        appendRow(s, tr("Short Description:"), getDisplayName());
062        appendRow(s, tr("URL:"), url);
063        if (author != null) {
064            appendRow(s, tr("Author:"), author);
065        }
066        if (link != null) {
067            appendRow(s, tr("Webpage:"), link);
068        }
069        if (description != null) {
070            appendRow(s, tr("Description:"), description);
071        }
072        if (version != null) {
073            appendRow(s, tr("Version:"), version);
074        }
075        if (minJosmVersion != null) {
076            appendRow(s, tr("Minimum JOSM Version:"), Integer.toString(minJosmVersion));
077        }
078        return "<html><style>th{text-align:right}td{width:400px}</style>"
079                + "<table>" + s + "</table></html>";
080    }
081
082    @Override
083    public String toString() {
084        return "<html><b>" + getDisplayName() + "</b>"
085                + (author == null ? "" : " <span color=\"gray\">" + tr("by {0}", author) + "</color>")
086                + "</html>";
087    }
088
089    @Override
090    public int compareTo(ExtendedSourceEntry o) {
091        if (url.startsWith("resource") && !o.url.startsWith("resource"))
092            return -1;
093        if (o.url.startsWith("resource"))
094            return 1;
095        else
096            return getDisplayName().compareToIgnoreCase(o.getDisplayName());
097    }
098}