Changeset 12825 in josm for trunk/src/org
- Timestamp:
- 2017-09-11T20:43:41+02:00 (7 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/preferences/sources/ExtendedSourceEntry.java
r12649 r12825 28 28 /** 29 29 * Constructs a new {@code ExtendedSourceEntry}. 30 * @param type type of source entry 30 31 * @param simpleFileName file name used for display 31 32 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands 33 * @since 12825 32 34 */ 33 public ExtendedSourceEntry(S tring 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); 35 37 this.simpleFileName = simpleFileName; 36 38 } -
trunk/src/org/openstreetmap/josm/data/preferences/sources/MapPaintPrefHelper.java
r12649 r12825 30 30 */ 31 31 public MapPaintPrefHelper() { 32 super("mappaint.style.entries" );32 super("mappaint.style.entries", SourceType.MAP_PAINT_STYLE); 33 33 } 34 34 … … 80 80 @Override 81 81 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"); 83 83 defJosmMapcss.active = true; 84 84 defJosmMapcss.name = "standard"; 85 85 defJosmMapcss.title = tr("JOSM default (MapCSS)"); 86 86 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"); 88 88 defPL2.active = false; 89 89 defPL2.name = "standard"; … … 108 108 @Override 109 109 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"))); 111 111 } 112 112 } -
trunk/src/org/openstreetmap/josm/data/preferences/sources/PresetPrefHelper.java
r12649 r12825 24 24 */ 25 25 public PresetPrefHelper() { 26 super("taggingpreset.entries" );26 super("taggingpreset.entries", SourceType.TAGGING_PRESET); 27 27 } 28 28 29 29 @Override 30 30 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"); 32 32 i.title = tr("Internal Preset"); 33 33 i.description = tr("The default preset for JOSM"); … … 45 45 @Override 46 46 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); 48 48 } 49 49 } -
trunk/src/org/openstreetmap/josm/data/preferences/sources/SourceEntry.java
r12649 r12825 19 19 20 20 /** 21 * The type of source entry. 22 * @since 12825 23 **/ 24 public final SourceType type; 25 26 /** 21 27 * A URL can be anything that CachedFile understands, i.e. 22 28 * a local file, http://, or a file from the current jar … … 58 64 /** 59 65 * Constructs a new {@code SourceEntry}. 66 * @param type type of source entry 60 67 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands 61 68 * @param isZip if url is a zip file and the resource is inside the zip file … … 70 77 * @see #title 71 78 * @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; 74 83 this.url = url; 75 84 this.isZip = isZip; … … 82 91 /** 83 92 * Constructs a new {@code SourceEntry}. 93 * @param type type of source entry 84 94 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands 85 95 * @param name Source name … … 90 100 * @see #title 91 101 * @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); 95 106 } 96 107 … … 100 111 */ 101 112 public SourceEntry(SourceEntry e) { 113 this.type = e.type; 102 114 this.url = e.url; 103 115 this.isZip = e.isZip; -
trunk/src/org/openstreetmap/josm/data/preferences/sources/SourcePrefHelper.java
r12649 r12825 19 19 20 20 private final String pref; 21 protected final SourceType type; 21 22 22 23 /** 23 24 * Constructs a new {@code SourcePrefHelper} for the given preference key. 24 25 * @param pref The preference key 26 * @param type The source type 27 * @since 12825 25 28 */ 26 public SourcePrefHelper(String pref ) {29 public SourcePrefHelper(String pref, SourceType type) { 27 30 this.pref = pref; 31 this.type = type; 28 32 } 29 33 -
trunk/src/org/openstreetmap/josm/data/preferences/sources/ValidatorPrefHelper.java
r12649 r12825 55 55 */ 56 56 public ValidatorPrefHelper() { 57 super(MapCSSTagChecker.ENTRIES_PREF_KEY );57 super(MapCSSTagChecker.ENTRIES_PREF_KEY, SourceType.TAGCHECKER_RULE); 58 58 } 59 59 … … 80 80 } 81 81 82 private staticvoid 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"); 84 84 i.title = title; 85 85 i.description = description; … … 98 98 @Override 99 99 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"))); 101 101 } 102 102 } -
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r12822 r12825 2 2 package org.openstreetmap.josm.data.validation.tests; 3 3 4 import static org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker.FixCommand.evaluateObject;5 4 import static org.openstreetmap.josm.tools.I18n.tr; 6 5 … … 42 41 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 43 42 import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper; 43 import org.openstreetmap.josm.data.validation.OsmValidator; 44 44 import org.openstreetmap.josm.data.validation.Severity; 45 45 import org.openstreetmap.josm.data.validation.Test; … … 760 760 addMapCSS(i); 761 761 if (Main.pref.getBoolean("validator.auto_reload_local_rules", true) && source.isLocal()) { 762 Main.fileWatcher.register ValidatorRule(source);762 Main.fileWatcher.registerSource(source); 763 763 } 764 764 } catch (IOException | IllegalStateException | IllegalArgumentException ex) { … … 823 823 return Objects.equals(checks, that.checks); 824 824 } 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 } 825 841 } -
trunk/src/org/openstreetmap/josm/gui/MainApplication.java
r12821 r12825 88 88 import org.openstreetmap.josm.data.osm.UserInfo; 89 89 import org.openstreetmap.josm.data.osm.search.SearchMode; 90 import org.openstreetmap.josm.data.preferences.sources.SourceType; 90 91 import org.openstreetmap.josm.data.projection.ProjectionCLI; 91 92 import org.openstreetmap.josm.data.projection.datum.NTV2GridShiftFileSource; … … 93 94 import org.openstreetmap.josm.data.projection.datum.NTV2Proj4DirGridShiftFileSource; 94 95 import org.openstreetmap.josm.data.validation.OsmValidator; 96 import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker; 95 97 import org.openstreetmap.josm.gui.ProgramArguments.Option; 96 98 import org.openstreetmap.josm.gui.SplashScreen.SplashProgressMonitor; … … 110 112 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 111 113 import org.openstreetmap.josm.gui.layer.TMSLayer; 114 import org.openstreetmap.josm.gui.mappaint.loader.MapPaintStyleLoader; 112 115 import org.openstreetmap.josm.gui.oauth.OAuthAuthorizationWizard; 113 116 import org.openstreetmap.josm.gui.preferences.ToolbarPreferences; … … 126 129 import org.openstreetmap.josm.io.CertificateAmendment; 127 130 import org.openstreetmap.josm.io.DefaultProxySelector; 131 import org.openstreetmap.josm.io.FileWatcher; 128 132 import org.openstreetmap.josm.io.MessageNotifier; 129 133 import org.openstreetmap.josm.io.OnlineResource; … … 1094 1098 MessageNotifier.setNotifierCallback(MainApplication::notifyNewMessages); 1095 1099 DeleteCommand.setDeletionCallback(DeleteAction.defaultDeletionCallback); 1100 FileWatcher.registerLoader(SourceType.MAP_PAINT_STYLE, MapPaintStyleLoader::reloadStyle); 1101 FileWatcher.registerLoader(SourceType.TAGCHECKER_RULE, MapCSSTagChecker::reloadRule); 1096 1102 OsmUrlToBounds.setMapSizeSupplier(() -> { 1097 1103 if (isDisplayingMapView()) { -
trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java
r12651 r12825 301 301 if (Main.pref.getBoolean("mappaint.auto_reload_local_styles", true) && source.isLocal()) { 302 302 try { 303 Main.fileWatcher.registerS tyleSource(source);303 Main.fileWatcher.registerSource(source); 304 304 } catch (IOException | IllegalStateException | IllegalArgumentException e) { 305 305 Logging.error(e); -
trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java
r12649 r12825 22 22 import org.openstreetmap.josm.data.osm.OsmPrimitive; 23 23 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 24 import org.openstreetmap.josm.data.preferences.sources.SourceType; 24 25 import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference; 25 26 import org.openstreetmap.josm.io.CachedFile; … … 70 71 */ 71 72 public StyleSource(String url, String name, String title) { 72 super( url, name, title, true);73 super(SourceType.MAP_PAINT_STYLE, url, name, title, true); 73 74 } 74 75 -
trunk/src/org/openstreetmap/josm/gui/mappaint/loader/MapPaintStyleLoader.java
r12651 r12825 6 6 import java.util.ArrayList; 7 7 import java.util.Collection; 8 import java.util.Collections; 8 9 import java.util.List; 9 10 11 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 10 12 import org.openstreetmap.josm.gui.MainApplication; 11 13 import org.openstreetmap.josm.gui.PleaseWaitRunnable; … … 67 69 MainApplication.worker.submit(new MapPaintStyleLoader(toReload)); 68 70 } 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 } 69 85 } -
trunk/src/org/openstreetmap/josm/gui/preferences/SourceEditor.java
r12649 r12825 741 741 if (sources == null) return; 742 742 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)); 744 744 } 745 745 fireTableDataChanged(); … … 969 969 active = editEntryDialog.active(); 970 970 } 971 final SourceEntry entry = new SourceEntry( 971 final SourceEntry entry = new SourceEntry(sourceType, 972 972 editEntryDialog.getURL(), 973 973 null, editEntryDialog.getTitle(), active); … … 1509 1509 Matcher m = Pattern.compile("^(.+);(.+)$").matcher(line); 1510 1510 if (m.matches()) { 1511 last = new ExtendedSourceEntry( m.group(1), m.group(2));1511 last = new ExtendedSourceEntry(sourceType, m.group(1), m.group(2)); 1512 1512 sources.add(last); 1513 1513 } else { -
trunk/src/org/openstreetmap/josm/io/FileWatcher.java
r12814 r12825 12 12 import java.nio.file.WatchKey; 13 13 import java.nio.file.WatchService; 14 import java.util. Collections;14 import java.util.EnumMap; 15 15 import java.util.HashMap; 16 16 import java.util.Map; 17 import java.util.concurrent.Executors; 17 import java.util.Objects; 18 import java.util.function.Consumer; 18 19 19 20 import org.openstreetmap.josm.data.preferences.sources.SourceEntry; 20 import org.openstreetmap.josm.data.validation.OsmValidator; 21 import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker; 21 import org.openstreetmap.josm.data.preferences.sources.SourceType; 22 22 import 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;25 23 import org.openstreetmap.josm.tools.CheckParameterUtil; 26 24 import org.openstreetmap.josm.tools.Logging; 27 import org.openstreetmap.josm.tools.Utils;28 25 29 26 /** … … 36 33 private Thread thread; 37 34 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<>(); 40 37 41 38 /** … … 66 63 * @throws IllegalStateException if the watcher service failed to start 67 64 * @throws IOException if an I/O error occurs 65 * @deprecated To be removed end of 2017. Use {@link #registerSource} instead 68 66 */ 67 @Deprecated 69 68 public void registerStyleSource(StyleSource style) throws IOException { 70 register (style, styleMap);69 registerSource(style); 71 70 } 72 71 … … 78 77 * @throws IOException if an I/O error occurs 79 78 * @since 7276 79 * @deprecated To be removed end of 2017. Use {@link #registerSource} instead 80 80 */ 81 @Deprecated 81 82 public void registerValidatorRule(SourceEntry rule) throws IOException { 82 register (rule, ruleMap);83 registerSource(rule); 83 84 } 84 85 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"); 87 96 if (watcher == null) { 88 97 throw new IllegalStateException("File watcher is not available"); 89 98 } 90 99 // 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); 92 101 // Get parent directory as WatchService allows only to monitor directories, not single files 93 102 File dir = file.getParentFile(); 94 103 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"); 96 105 } 97 106 synchronized (this) { … … 99 108 // (it returns the same key so it should not send events several times) 100 109 dir.toPath().register(watcher, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_CREATE); 101 map.put(file.toPath(), obj);110 sourceMap.put(file.toPath(), src); 102 111 } 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")); 103 123 } 104 124 … … 148 168 149 169 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); 165 178 } 166 179 } else if (Logging.isDebugEnabled()) {
Note:
See TracChangeset
for help on using the changeset viewer.