Changeset 12825 in josm for trunk


Ignore:
Timestamp:
2017-09-11T20:43:41+02:00 (7 years ago)
Author:
Don-vip
Message:

see #15229 - see #15182 - make FileWatcher generic so it has no more dependence on MapCSS

Location:
trunk
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/ExtendedSourceEntry.java

    r12649 r12825  
    2828    /**
    2929     * Constructs a new {@code ExtendedSourceEntry}.
     30     * @param type type of source entry
    3031     * @param simpleFileName file name used for display
    3132     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
     33     * @since 12825
    3234     */
    33     public ExtendedSourceEntry(String simpleFileName, String url) {
    34         super(url, null, null, true);
     35    public ExtendedSourceEntry(SourceType type, String simpleFileName, String url) {
     36        super(type, url, null, null, true);
    3537        this.simpleFileName = simpleFileName;
    3638    }
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/MapPaintPrefHelper.java

    r12649 r12825  
    3030     */
    3131    public MapPaintPrefHelper() {
    32         super("mappaint.style.entries");
     32        super("mappaint.style.entries", SourceType.MAP_PAINT_STYLE);
    3333    }
    3434
     
    8080    @Override
    8181    public Collection<ExtendedSourceEntry> getDefault() {
    82         ExtendedSourceEntry defJosmMapcss = new ExtendedSourceEntry("elemstyles.mapcss", "resource://styles/standard/elemstyles.mapcss");
     82        ExtendedSourceEntry defJosmMapcss = new ExtendedSourceEntry(type, "elemstyles.mapcss", "resource://styles/standard/elemstyles.mapcss");
    8383        defJosmMapcss.active = true;
    8484        defJosmMapcss.name = "standard";
    8585        defJosmMapcss.title = tr("JOSM default (MapCSS)");
    8686        defJosmMapcss.description = tr("Internal style to be used as base for runtime switchable overlay styles");
    87         ExtendedSourceEntry defPL2 = new ExtendedSourceEntry("potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
     87        ExtendedSourceEntry defPL2 = new ExtendedSourceEntry(type, "potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
    8888        defPL2.active = false;
    8989        defPL2.name = "standard";
     
    108108    @Override
    109109    public SourceEntry deserialize(Map<String, String> s) {
    110         return new SourceEntry(s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
     110        return new SourceEntry(type, s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
    111111    }
    112112}
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/PresetPrefHelper.java

    r12649 r12825  
    2424     */
    2525    public PresetPrefHelper() {
    26         super("taggingpreset.entries");
     26        super("taggingpreset.entries", SourceType.TAGGING_PRESET);
    2727    }
    2828
    2929    @Override
    3030    public Collection<ExtendedSourceEntry> getDefault() {
    31         ExtendedSourceEntry i = new ExtendedSourceEntry("defaultpresets.xml", "resource://data/defaultpresets.xml");
     31        ExtendedSourceEntry i = new ExtendedSourceEntry(type, "defaultpresets.xml", "resource://data/defaultpresets.xml");
    3232        i.title = tr("Internal Preset");
    3333        i.description = tr("The default preset for JOSM");
     
    4545    @Override
    4646    public SourceEntry deserialize(Map<String, String> s) {
    47         return new SourceEntry(s.get("url"), null, s.get("title"), true);
     47        return new SourceEntry(type, s.get("url"), null, s.get("title"), true);
    4848    }
    4949}
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/SourceEntry.java

    r12649 r12825  
    1919
    2020    /**
     21     * The type of source entry.
     22     * @since 12825
     23     **/
     24    public final SourceType type;
     25
     26    /**
    2127     *  A URL can be anything that CachedFile understands, i.e.
    2228     *  a local file, http://, or a file from the current jar
     
    5864    /**
    5965     * Constructs a new {@code SourceEntry}.
     66     * @param type type of source entry
    6067     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
    6168     * @param isZip if url is a zip file and the resource is inside the zip file
     
    7077     * @see #title
    7178     * @see #active
    72      */
    73     public SourceEntry(String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
     79     * @since 12825
     80     */
     81    public SourceEntry(SourceType type, String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
     82        this.type = type;
    7483        this.url = url;
    7584        this.isZip = isZip;
     
    8291    /**
    8392     * Constructs a new {@code SourceEntry}.
     93     * @param type type of source entry
    8494     * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
    8595     * @param name Source name
     
    90100     * @see #title
    91101     * @see #active
    92      */
    93     public SourceEntry(String url, String name, String title, boolean active) {
    94         this(url, false, null, name, title, active);
     102     * @since 12825
     103     */
     104    public SourceEntry(SourceType type, String url, String name, String title, boolean active) {
     105        this(type, url, false, null, name, title, active);
    95106    }
    96107
     
    100111     */
    101112    public SourceEntry(SourceEntry e) {
     113        this.type = e.type;
    102114        this.url = e.url;
    103115        this.isZip = e.isZip;
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/SourcePrefHelper.java

    r12649 r12825  
    1919
    2020    private final String pref;
     21    protected final SourceType type;
    2122
    2223    /**
    2324     * Constructs a new {@code SourcePrefHelper} for the given preference key.
    2425     * @param pref The preference key
     26     * @param type The source type
     27     * @since 12825
    2528     */
    26     public SourcePrefHelper(String pref) {
     29    public SourcePrefHelper(String pref, SourceType type) {
    2730        this.pref = pref;
     31        this.type = type;
    2832    }
    2933
  • trunk/src/org/openstreetmap/josm/data/preferences/sources/ValidatorPrefHelper.java

    r12649 r12825  
    5555     */
    5656    public ValidatorPrefHelper() {
    57         super(MapCSSTagChecker.ENTRIES_PREF_KEY);
     57        super(MapCSSTagChecker.ENTRIES_PREF_KEY, SourceType.TAGCHECKER_RULE);
    5858    }
    5959
     
    8080    }
    8181
    82     private static void addDefault(List<ExtendedSourceEntry> defaults, String filename, String title, String description) {
    83         ExtendedSourceEntry i = new ExtendedSourceEntry(filename+".mapcss", "resource://data/validator/"+filename+".mapcss");
     82    private void addDefault(List<ExtendedSourceEntry> defaults, String filename, String title, String description) {
     83        ExtendedSourceEntry i = new ExtendedSourceEntry(type, filename+".mapcss", "resource://data/validator/"+filename+".mapcss");
    8484        i.title = title;
    8585        i.description = description;
     
    9898    @Override
    9999    public SourceEntry deserialize(Map<String, String> s) {
    100         return new SourceEntry(s.get("url"), null, s.get("title"), Boolean.parseBoolean(s.get("active")));
     100        return new SourceEntry(type, s.get("url"), null, s.get("title"), Boolean.parseBoolean(s.get("active")));
    101101    }
    102102}
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r12822 r12825  
    22package org.openstreetmap.josm.data.validation.tests;
    33
    4 import static org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.FixCommand.evaluateObject;
    54import static org.openstreetmap.josm.tools.I18n.tr;
    65
     
    4241import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    4342import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
     43import org.openstreetmap.josm.data.validation.OsmValidator;
    4444import org.openstreetmap.josm.data.validation.Severity;
    4545import org.openstreetmap.josm.data.validation.Test;
     
    760760                addMapCSS(i);
    761761                if (Main.pref.getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) {
    762                     Main.fileWatcher.registerValidatorRule(source);
     762                    Main.fileWatcher.registerSource(source);
    763763                }
    764764            } catch (IOException | IllegalStateException | IllegalArgumentException ex) {
     
    823823        return Objects.equals(checks, that.checks);
    824824    }
     825
     826    /**
     827     * Reload tagchecker rule.
     828     * @param rule tagchecker rule to reload
     829     * @since 12825
     830     */
     831    public static void reloadRule(SourceEntry rule) {
     832        MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
     833        if (tagChecker != null) {
     834            try {
     835                tagChecker.addMapCSS(rule.url);
     836            } catch (IOException | ParseException e) {
     837                Logging.warn(e);
     838            }
     839        }
     840    }
    825841}
  • trunk/src/org/openstreetmap/josm/gui/MainApplication.java

    r12821 r12825  
    8888import org.openstreetmap.josm.data.osm.UserInfo;
    8989import org.openstreetmap.josm.data.osm.search.SearchMode;
     90import org.openstreetmap.josm.data.preferences.sources.SourceType;
    9091import org.openstreetmap.josm.data.projection.ProjectionCLI;
    9192import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileSource;
     
    9394import org.openstreetmap.josm.data.projection.datum.NTV2Proj4DirGridShiftFileSource;
    9495import org.openstreetmap.josm.data.validation.OsmValidator;
     96import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    9597import org.openstreetmap.josm.gui.ProgramArguments.Option;
    9698import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor;
     
    110112import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    111113import org.openstreetmap.josm.gui.layer.TMSLayer;
     114import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
    112115import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard;
    113116import org.openstreetmap.josm.gui.preferences.ToolbarPreferences;
     
    126129import org.openstreetmap.josm.io.CertificateAmendment;
    127130import org.openstreetmap.josm.io.DefaultProxySelector;
     131import org.openstreetmap.josm.io.FileWatcher;
    128132import org.openstreetmap.josm.io.MessageNotifier;
    129133import org.openstreetmap.josm.io.OnlineResource;
     
    10941098        MessageNotifier.setNotifierCallback(MainApplication::notifyNewMessages);
    10951099        DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback);
     1100        FileWatcher.registerLoader(SourceType.MAP_PAINT_STYLE, MapPaintStyleLoader::reloadStyle);
     1101        FileWatcher.registerLoader(SourceType.TAGCHECKER_RULE, MapCSSTagChecker::reloadRule);
    10961102        OsmUrlToBounds.setMapSizeSupplier(() -> {
    10971103            if (isDisplayingMapView()) {
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r12651 r12825  
    301301        if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) {
    302302            try {
    303                 Main.fileWatcher.registerStyleSource(source);
     303                Main.fileWatcher.registerSource(source);
    304304            } catch (IOException | IllegalStateException | IllegalArgumentException e) {
    305305                Logging.error(e);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r12649 r12825  
    2222import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2323import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
     24import org.openstreetmap.josm.data.preferences.sources.SourceType;
    2425import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
    2526import org.openstreetmap.josm.io.CachedFile;
     
    7071     */
    7172    public StyleSource(String url, String name, String title) {
    72         super(url, name, title, true);
     73        super(SourceType.MAP_PAINT_STYLE, url, name, title, true);
    7374    }
    7475
  • trunk/src/org/openstreetmap/josm/gui/mappaint/loader/MapPaintStyleLoader.java

    r12651 r12825  
    66import java.util.ArrayList;
    77import java.util.Collection;
     8import java.util.Collections;
    89import java.util.List;
    910
     11import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    1012import org.openstreetmap.josm.gui.MainApplication;
    1113import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    6769        MainApplication.worker.submit(new MapPaintStyleLoader(toReload));
    6870    }
     71
     72    /**
     73     * Reload style.
     74     * @param style {@link StyleSource} to reload
     75     * @throws IllegalArgumentException if {@code style} is not a {@code StyleSource} instance
     76     * @since 12825
     77     */
     78    public static void reloadStyle(SourceEntry style) {
     79        if (style instanceof StyleSource) {
     80            MainApplication.worker.submit(new MapPaintStyleLoader(Collections.singleton((StyleSource) style)));
     81        } else {
     82            throw new IllegalArgumentException(style + " is not a StyleSource");
     83        }
     84    }
    6985}
  • trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java

    r12649 r12825  
    741741            if (sources == null) return;
    742742            for (ExtendedSourceEntry info: sources) {
    743                 data.add(new SourceEntry(info.url, info.name, info.getDisplayName(), true));
     743                data.add(new SourceEntry(info.type, info.url, info.name, info.getDisplayName(), true));
    744744            }
    745745            fireTableDataChanged();
     
    969969                    active = editEntryDialog.active();
    970970                }
    971                 final SourceEntry entry = new SourceEntry(
     971                final SourceEntry entry = new SourceEntry(sourceType,
    972972                        editEntryDialog.getURL(),
    973973                        null, editEntryDialog.getTitle(), active);
     
    15091509                        Matcher m = Pattern.compile("^(.+);(.+)$").matcher(line);
    15101510                        if (m.matches()) {
    1511                             last = new ExtendedSourceEntry(m.group(1), m.group(2));
     1511                            last = new ExtendedSourceEntry(sourceType, m.group(1), m.group(2));
    15121512                            sources.add(last);
    15131513                        } else {
  • trunk/src/org/openstreetmap/josm/io/FileWatcher.java

    r12814 r12825  
    1212import java.nio.file.WatchKey;
    1313import java.nio.file.WatchService;
    14 import java.util.Collections;
     14import java.util.EnumMap;
    1515import java.util.HashMap;
    1616import java.util.Map;
    17 import java.util.concurrent.Executors;
     17import java.util.Objects;
     18import java.util.function.Consumer;
    1819
    1920import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
    20 import org.openstreetmap.josm.data.validation.OsmValidator;
    21 import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
     21import org.openstreetmap.josm.data.preferences.sources.SourceType;
    2222import org.openstreetmap.josm.gui.mappaint.StyleSource;
    23 import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader;
    24 import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
    2523import org.openstreetmap.josm.tools.CheckParameterUtil;
    2624import org.openstreetmap.josm.tools.Logging;
    27 import org.openstreetmap.josm.tools.Utils;
    2825
    2926/**
     
    3633    private Thread thread;
    3734
    38     private final Map<Path, StyleSource> styleMap = new HashMap<>();
    39     private final Map<Path, SourceEntry> ruleMap = new HashMap<>();
     35    private static final Map<SourceType, Consumer<SourceEntry>> loaderMap = new EnumMap<>(SourceType.class);
     36    private final Map<Path, SourceEntry> sourceMap = new HashMap<>();
    4037
    4138    /**
     
    6663     * @throws IllegalStateException if the watcher service failed to start
    6764     * @throws IOException if an I/O error occurs
     65     * @deprecated To be removed end of 2017. Use {@link #registerSource} instead
    6866     */
     67    @Deprecated
    6968    public void registerStyleSource(StyleSource style) throws IOException {
    70         register(style, styleMap);
     69        registerSource(style);
    7170    }
    7271
     
    7877     * @throws IOException if an I/O error occurs
    7978     * @since 7276
     79     * @deprecated To be removed end of 2017. Use {@link #registerSource} instead
    8080     */
     81    @Deprecated
    8182    public void registerValidatorRule(SourceEntry rule) throws IOException {
    82         register(rule, ruleMap);
     83        registerSource(rule);
    8384    }
    8485
    85     private <T extends SourceEntry> void register(T obj, Map<Path, T> map) throws IOException {
    86         CheckParameterUtil.ensureParameterNotNull(obj, "obj");
     86    /**
     87     * Registers a source for local file changes, allowing dynamic reloading.
     88     * @param src The source to watch
     89     * @throws IllegalArgumentException if {@code rule} is null or if it does not provide a local file
     90     * @throws IllegalStateException if the watcher service failed to start
     91     * @throws IOException if an I/O error occurs
     92     * @since 12825
     93     */
     94    public void registerSource(SourceEntry src) throws IOException {
     95        CheckParameterUtil.ensureParameterNotNull(src, "src");
    8796        if (watcher == null) {
    8897            throw new IllegalStateException("File watcher is not available");
    8998        }
    9099        // Get local file, as this method is only called for local style sources
    91         File file = new File(obj.url);
     100        File file = new File(src.url);
    92101        // Get parent directory as WatchService allows only to monitor directories, not single files
    93102        File dir = file.getParentFile();
    94103        if (dir == null) {
    95             throw new IllegalArgumentException("Resource "+obj+" does not have a parent directory");
     104            throw new IllegalArgumentException("Resource "+src+" does not have a parent directory");
    96105        }
    97106        synchronized (this) {
     
    99108            // (it returns the same key so it should not send events several times)
    100109            dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE);
    101             map.put(file.toPath(), obj);
     110            sourceMap.put(file.toPath(), src);
    102111        }
     112    }
     113
     114    /**
     115     * Registers a source loader, allowing dynamic reloading when an entry changes.
     116     * @param type the source type for which the loader operates
     117     * @param loader the loader in charge of reloading any source of given type when it changes
     118     * @return the previous loader registered for this source type, if any
     119     * @since 12825
     120     */
     121    public static Consumer<SourceEntry> registerLoader(SourceType type, Consumer<SourceEntry> loader) {
     122        return loaderMap.put(Objects.requireNonNull(type, "type"), Objects.requireNonNull(loader, "loader"));
    103123    }
    104124
     
    148168
    149169                synchronized (this) {
    150                     StyleSource style = styleMap.get(fullPath);
    151                     SourceEntry rule = ruleMap.get(fullPath);
    152                     if (style != null) {
    153                         Logging.info("Map style "+style.getDisplayString()+" has been modified. Reloading style...");
    154                         Executors.newSingleThreadExecutor(Utils.newThreadFactory("mapstyle-reload-%d", Thread.NORM_PRIORITY)).submit(
    155                                 new MapPaintStyleLoader(Collections.singleton(style)));
    156                     } else if (rule != null) {
    157                         Logging.info("Validator rule "+rule.getDisplayString()+" has been modified. Reloading rule...");
    158                         MapCSSTagChecker tagChecker = OsmValidator.getTest(MapCSSTagChecker.class);
    159                         if (tagChecker != null) {
    160                             try {
    161                                 tagChecker.addMapCSS(rule.url);
    162                             } catch (IOException | ParseException e) {
    163                                 Logging.warn(e);
    164                             }
     170                    SourceEntry source = sourceMap.get(fullPath);
     171                    if (source != null) {
     172                        Consumer<SourceEntry> loader = loaderMap.get(source.type);
     173                        if (loader != null) {
     174                            Logging.info("Source "+source.getDisplayString()+" has been modified. Reloading it...");
     175                            loader.accept(source);
     176                        } else {
     177                            Logging.warn("Received {0} event for unregistered source type: {1}", kind.name(), source.type);
    165178                        }
    166179                    } else if (Logging.isDebugEnabled()) {
  • trunk/test/functional/org/openstreetmap/josm/gui/mappaint/MapCSSRendererTest.java

    r12649 r12825  
    3737import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
    3838import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
     39import org.openstreetmap.josm.data.preferences.sources.SourceType;
    3940import org.openstreetmap.josm.gui.NavigatableComponent;
    4041import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
     
    302303
    303304        public SourceEntry getStyleSourceEntry() {
    304             return new SourceEntry(getTestDirectory() + "/style.mapcss",
     305            return new SourceEntry(SourceType.MAP_PAINT_STYLE, getTestDirectory() + "/style.mapcss",
    305306                    "test style", "a test style", true // active
    306307            );
  • trunk/test/performance/org/openstreetmap/josm/gui/mappaint/mapcss/MapCSSPerformanceTest.java

    r12651 r12825  
    1717import org.openstreetmap.josm.data.osm.visitor.paint.StyledMapRenderer;
    1818import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
     19import org.openstreetmap.josm.data.preferences.sources.SourceType;
    1920import org.openstreetmap.josm.gui.NavigatableComponent;
    2021import org.openstreetmap.josm.gui.mappaint.MapRendererPerformanceTest;
     
    7172        MapCSSStyleSource source = new MapCSSStyleSource(
    7273            new SourceEntry(
     74                SourceType.MAP_PAINT_STYLE,
    7375                STYLE_FILE,
    7476                "test style",
Note: See TracChangeset for help on using the changeset viewer.