From a3563d354e02a5c97034688202e1768eab89009e Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 30 Oct 2019 11:12:56 -0600 Subject: [PATCH] Attempt to workaround JOSM not loading resource urls Signed-off-by: Taylor Smock --- .../mapwithai/backend/MapWithAIDataUtils.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java index 445763a..13ac920 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java @@ -40,6 +40,8 @@ public final class MapWithAIDataUtils { private static ForkJoinPool forkJoinPool; static final Object LAYER_LOCK = new Object(); + private static final String PAINT_STYLE_RESOURCE_URL = "resource://styles/standard/mapwithai.mapcss"; + private MapWithAIDataUtils() { // Hide the constructor } @@ -55,20 +57,32 @@ public final class MapWithAIDataUtils { MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(style -> oldUrls.contains(style.url)) .forEach(MapPaintStyles::removeStyle); - if (MapPaintStyles.getStyles().getStyleSources().parallelStream() - .noneMatch(source -> "resource://styles/standard/mapwithai.mapcss".equals(source.url))) { - final MapCSSStyleSource style = new MapCSSStyleSource("resource://styles/standard/mapwithai.mapcss", - MapWithAIPlugin.NAME, "MapWithAI"); - MapPaintStyles.addStyle(style); + Optional possiblePaintStyle = MapPaintStyles.getStyles().getStyleSources().parallelStream() + .filter(MapCSSStyleSource.class::isInstance).map(MapCSSStyleSource.class::cast) + .filter(source -> PAINT_STYLE_RESOURCE_URL.equals(source.url)).findFirst(); + if (possiblePaintStyle.isPresent()) { + MapCSSStyleSource source = possiblePaintStyle.get(); + if (!source.getErrors().isEmpty()) { + MapPaintStyles.removeStyle(source); + reallyAddPaintStyle(); + } + } else { + reallyAddPaintStyle(); } } + private static final void reallyAddPaintStyle() { + final MapCSSStyleSource style = new MapCSSStyleSource(PAINT_STYLE_RESOURCE_URL, MapWithAIPlugin.NAME, + "MapWithAI"); + MapPaintStyles.addStyle(style); + } + /** * Remove MapWithAI paint styles */ public static void removeMapWithAIPaintStyles() { MapPaintStyles.getStyles().getStyleSources().parallelStream() - .filter(source -> "resource://styles/standard/mapwithai.mapcss".equals(source.url)) + .filter(source -> PAINT_STYLE_RESOURCE_URL.equals(source.url)) .forEach(MapPaintStyles::removeStyle); }