diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java index 7578823..c560902 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java @@ -33,12 +33,15 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIRemoteControl; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIUploadHook; import org.openstreetmap.josm.plugins.mapwithai.backend.MergeDuplicateWaysAction; +import org.openstreetmap.josm.spi.preferences.Config; import org.openstreetmap.josm.tools.Destroyable; import org.openstreetmap.josm.tools.Logging; public final class MapWithAIPlugin extends Plugin implements Destroyable { /** The name of the plugin */ public static final String NAME = "MapWithAI"; + + private static final String PAINTSTYLE_PREEXISTS = NAME.concat(".paintstyleprexists"); private static String versionInfo; private final PreferenceSetting preferenceSetting; @@ -72,6 +75,10 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { } } + if (!Config.getPref().getKeySet().contains(PAINTSTYLE_PREEXISTS)) { + Config.getPref().putBoolean(PAINTSTYLE_PREEXISTS, MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); + } + MapWithAIDataUtils.addMapWithAIPaintStyles(); setVersionInfo(info.localversion); @@ -134,7 +141,9 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { MainApplication.getLayerManager().getLayersOfType(MapWithAILayer.class).stream() .forEach(layer -> MainApplication.getLayerManager().removeLayer(layer)); - MapWithAIDataUtils.removeMapWithAIPaintStyles(); + if (Config.getPref().getBoolean(PAINTSTYLE_PREEXISTS)) { + MapWithAIDataUtils.removeMapWithAIPaintStyles(); + } destroyables.forEach(Destroyable::destroy); } 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 13ac920..d632f95 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,7 +40,7 @@ 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 static final String PAINT_STYLE_RESOURCE_URL = "https://josm.openstreetmap.de/josmfile?page=Styles/MapWithAI&zip=1"; private MapWithAIDataUtils() { // Hide the constructor @@ -53,28 +53,24 @@ public final class MapWithAIDataUtils { // Remove old url's that were automatically added -- remove after Jan 01, 2020 final List oldUrls = Arrays.asList( "https://gitlab.com/gokaart/JOSM_MapWithAI/raw/master/src/resources/styles/standard/mapwithai.mapcss", - "https://gitlab.com/smocktaylor/rapid/raw/master/src/resources/styles/standard/rapid.mapcss"); + "https://gitlab.com/smocktaylor/rapid/raw/master/src/resources/styles/standard/rapid.mapcss", + "resource://styles/standard/mapwithai.mapcss"); MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(style -> oldUrls.contains(style.url)) .forEach(MapPaintStyles::removeStyle); - 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(); + if (!checkIfMapWithAIPaintStyleExists()) { + final MapCSSStyleSource style = new MapCSSStyleSource(PAINT_STYLE_RESOURCE_URL, MapWithAIPlugin.NAME, + "MapWithAI"); + MapPaintStyles.addStyle(style); } } - private static final void reallyAddPaintStyle() { - final MapCSSStyleSource style = new MapCSSStyleSource(PAINT_STYLE_RESOURCE_URL, MapWithAIPlugin.NAME, - "MapWithAI"); - MapPaintStyles.addStyle(style); + /** + * @return true if a MapWithAI paint style exists + */ + public static boolean checkIfMapWithAIPaintStyleExists() { + return MapPaintStyles.getStyles().getStyleSources().parallelStream().filter(MapCSSStyleSource.class::isInstance) + .map(MapCSSStyleSource.class::cast).anyMatch(source -> PAINT_STYLE_RESOURCE_URL.equals(source.url)); } /** diff --git a/src/resources/styles/standard/mapwithai.mapcss b/src/resources/styles/standard/mapwithai.mapcss index b2dbdf2..b60de7f 100644 --- a/src/resources/styles/standard/mapwithai.mapcss +++ b/src/resources/styles/standard/mapwithai.mapcss @@ -15,8 +15,8 @@ way[source=~/^(?i)(microsoft|digitalglobe)$/] { } way.mapwithai { - color: fuchsia; + color: mapwithai#FF00FF; } way.mapwithai:closed2 { - fill-color: fuchsia; + fill-color: mapwithai#FF00FF; } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java index ac71ffe..42aa419 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPluginTest.java @@ -60,7 +60,7 @@ public class MapWithAIPluginTest { plugin = new MapWithAIPlugin(info); Assert.assertEquals(dataMenuSize + 4, dataMenu.getMenuComponentCount()); Assert.assertEquals(1, MapPaintStyles.getStyles().getStyleSources().parallelStream() - .filter(source -> source.url != null && source.url.contains("mapwithai")).count()); + .filter(source -> source.url != null && source.name.contains("MapWithAI")).count()); } /**