Changeset 12649 in josm for trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
- Timestamp:
- 2017-08-25T21:51:00+02:00 (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r12634 r12649 30 30 import java.util.HashMap; 31 31 import java.util.Iterator; 32 import java.util.LinkedHashSet;33 32 import java.util.List; 34 33 import java.util.Map; 35 34 import java.util.Objects; 36 import java.util.Set;37 35 import java.util.concurrent.CopyOnWriteArrayList; 38 36 import java.util.regex.Matcher; … … 77 75 import org.openstreetmap.josm.actions.ExtensionFileFilter; 78 76 import org.openstreetmap.josm.data.Version; 77 import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry; 78 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 79 import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper; 80 import org.openstreetmap.josm.data.preferences.sources.SourceProvider; 81 import org.openstreetmap.josm.data.preferences.sources.SourceType; 79 82 import org.openstreetmap.josm.gui.ExtendedDialog; 80 83 import org.openstreetmap.josm.gui.HelpAwareOptionPane; … … 787 790 } 788 791 selectionModel.setValueIsAdjusting(false); 789 }790 }791 792 /**793 * Source entry with additional metadata.794 */795 public static class ExtendedSourceEntry extends SourceEntry implements Comparable<ExtendedSourceEntry> {796 /** file name used for display */797 public String simpleFileName;798 /** version used for display */799 public String version;800 /** author name used for display */801 public String author;802 /** webpage link used for display */803 public String link;804 /** short description used for display */805 public String description;806 /** Style type: can only have one value: "xml". Used to filter out old XML styles. For MapCSS styles, the value is not set. */807 public String styleType;808 /** minimum JOSM version required to enable this source entry */809 public Integer minJosmVersion;810 811 /**812 * Constructs a new {@code ExtendedSourceEntry}.813 * @param simpleFileName file name used for display814 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands815 */816 public ExtendedSourceEntry(String simpleFileName, String url) {817 super(url, null, null, true);818 this.simpleFileName = simpleFileName;819 }820 821 /**822 * @return string representation for GUI list or menu entry823 */824 public String getDisplayName() {825 return title == null ? simpleFileName : title;826 }827 828 private static void appendRow(StringBuilder s, String th, String td) {829 s.append("<tr><th>").append(th).append("</th><td>").append(Utils.escapeReservedCharactersHTML(td)).append("</td</tr>");830 }831 832 /**833 * Returns a tooltip containing available metadata.834 * @return a tooltip containing available metadata835 */836 public String getTooltip() {837 StringBuilder s = new StringBuilder();838 appendRow(s, tr("Short Description:"), getDisplayName());839 appendRow(s, tr("URL:"), url);840 if (author != null) {841 appendRow(s, tr("Author:"), author);842 }843 if (link != null) {844 appendRow(s, tr("Webpage:"), link);845 }846 if (description != null) {847 appendRow(s, tr("Description:"), description);848 }849 if (version != null) {850 appendRow(s, tr("Version:"), version);851 }852 if (minJosmVersion != null) {853 appendRow(s, tr("Minimum JOSM Version:"), Integer.toString(minJosmVersion));854 }855 return "<html><style>th{text-align:right}td{width:400px}</style>"856 + "<table>" + s + "</table></html>";857 }858 859 @Override860 public String toString() {861 return "<html><b>" + getDisplayName() + "</b>"862 + (author == null ? "" : " <span color=\"gray\">" + tr("by {0}", author) + "</color>")863 + "</html>";864 }865 866 @Override867 public int compareTo(ExtendedSourceEntry o) {868 if (url.startsWith("resource") && !o.url.startsWith("resource"))869 return -1;870 if (o.url.startsWith("resource"))871 return 1;872 else873 return getDisplayName().compareToIgnoreCase(o.getDisplayName());874 792 } 875 793 } … … 1766 1684 1767 1685 /** 1768 * Helper class for specialized extensions preferences.1769 */1770 public abstract static class SourcePrefHelper {1771 1772 private final String pref;1773 1774 /**1775 * Constructs a new {@code SourcePrefHelper} for the given preference key.1776 * @param pref The preference key1777 */1778 public SourcePrefHelper(String pref) {1779 this.pref = pref;1780 }1781 1782 /**1783 * Returns the default sources provided by JOSM core.1784 * @return the default sources provided by JOSM core1785 */1786 public abstract Collection<ExtendedSourceEntry> getDefault();1787 1788 /**1789 * Serializes the given source entry as a map.1790 * @param entry source entry to serialize1791 * @return map (key=value)1792 */1793 public abstract Map<String, String> serialize(SourceEntry entry);1794 1795 /**1796 * Deserializes the given map as a source entry.1797 * @param entryStr map (key=value)1798 * @return source entry1799 */1800 public abstract SourceEntry deserialize(Map<String, String> entryStr);1801 1802 /**1803 * Returns the list of sources.1804 * @return The list of sources1805 */1806 public List<SourceEntry> get() {1807 1808 Collection<Map<String, String>> src = Main.pref.getListOfStructs(pref, (Collection<Map<String, String>>) null);1809 if (src == null)1810 return new ArrayList<>(getDefault());1811 1812 List<SourceEntry> entries = new ArrayList<>();1813 for (Map<String, String> sourcePref : src) {1814 SourceEntry e = deserialize(new HashMap<>(sourcePref));1815 if (e != null) {1816 entries.add(e);1817 }1818 }1819 return entries;1820 }1821 1822 /**1823 * Saves a list of sources to JOSM preferences.1824 * @param entries list of sources1825 * @return {@code true}, if something has changed (i.e. value is different than before)1826 */1827 public boolean put(Collection<? extends SourceEntry> entries) {1828 Collection<Map<String, String>> setting = serializeList(entries);1829 boolean unset = Main.pref.getListOfStructs(pref, (Collection<Map<String, String>>) null) == null;1830 if (unset) {1831 Collection<Map<String, String>> def = serializeList(getDefault());1832 if (setting.equals(def))1833 return false;1834 }1835 return Main.pref.putListOfStructs(pref, setting);1836 }1837 1838 private Collection<Map<String, String>> serializeList(Collection<? extends SourceEntry> entries) {1839 Collection<Map<String, String>> setting = new ArrayList<>(entries.size());1840 for (SourceEntry e : entries) {1841 setting.add(serialize(e));1842 }1843 return setting;1844 }1845 1846 /**1847 * Returns the set of active source URLs.1848 * @return The set of active source URLs.1849 */1850 public final Set<String> getActiveUrls() {1851 Set<String> urls = new LinkedHashSet<>(); // retain order1852 for (SourceEntry e : get()) {1853 if (e.active) {1854 urls.add(e.url);1855 }1856 }1857 return urls;1858 }1859 }1860 1861 /**1862 1686 * Defers loading of sources to the first time the adequate tab is selected. 1863 1687 * @param tab The preferences tab
Note:
See TracChangeset
for help on using the changeset viewer.