Ignore:
Timestamp:
2017-08-25T21:51:00+02:00 (9 years ago)
Author:
Don-vip
Message:

see #15182 - code refactoring to avoid dependence on GUI packages from Preferences

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r12634 r12649  
    3030import java.util.HashMap;
    3131import java.util.Iterator;
    32 import java.util.LinkedHashSet;
    3332import java.util.List;
    3433import java.util.Map;
    3534import java.util.Objects;
    36 import java.util.Set;
    3735import java.util.concurrent.CopyOnWriteArrayList;
    3836import java.util.regex.Matcher;
     
    7775import org.openstreetmap.josm.actions.ExtensionFileFilter;
    7876import org.openstreetmap.josm.data.Version;
     77import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
     78import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
     79import org.openstreetmap.josm.data.preferences.sources.SourcePrefHelper;
     80import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
     81import org.openstreetmap.josm.data.preferences.sources.SourceType;
    7982import org.openstreetmap.josm.gui.ExtendedDialog;
    8083import org.openstreetmap.josm.gui.HelpAwareOptionPane;
     
    787790            }
    788791            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 display
    814          * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
    815          */
    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 entry
    823          */
    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 metadata
    835          */
    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         @Override
    860         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         @Override
    867         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             else
    873                 return getDisplayName().compareToIgnoreCase(o.getDisplayName());
    874792        }
    875793    }
     
    17661684
    17671685    /**
    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 key
    1777          */
    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 core
    1785          */
    1786         public abstract Collection<ExtendedSourceEntry> getDefault();
    1787 
    1788         /**
    1789          * Serializes the given source entry as a map.
    1790          * @param entry source entry to serialize
    1791          * @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 entry
    1799          */
    1800         public abstract SourceEntry deserialize(Map<String, String> entryStr);
    1801 
    1802         /**
    1803          * Returns the list of sources.
    1804          * @return The list of sources
    1805          */
    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 sources
    1825          * @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 order
    1852             for (SourceEntry e : get()) {
    1853                 if (e.active) {
    1854                     urls.add(e.url);
    1855                 }
    1856             }
    1857             return urls;
    1858         }
    1859     }
    1860 
    1861     /**
    18621686     * Defers loading of sources to the first time the adequate tab is selected.
    18631687     * @param tab The preferences tab
Note: See TracChangeset for help on using the changeset viewer.