diff --git a/build.xml b/build.xml index 63bc9e2..6bcae7b 100644 --- a/build.xml +++ b/build.xml @@ -27,6 +27,10 @@ + + + + diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAction.java index 94bf82f..2141968 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAction.java @@ -20,6 +20,7 @@ import org.openstreetmap.josm.data.Bounds; import org.openstreetmap.josm.data.osm.DataSet; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.Notification; +import org.openstreetmap.josm.gui.layer.Layer; import org.openstreetmap.josm.gui.layer.OsmDataLayer; import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; import org.openstreetmap.josm.tools.Shortcut; @@ -39,8 +40,26 @@ public class MapWithAIAction extends JosmAction { @Override public void actionPerformed(ActionEvent event) { if (isEnabled()) { - MapWithAIDataUtils.getMapWithAIData(MapWithAIDataUtils.getLayer(true)); - createMessageDialog(); + final boolean hasLayer = MapWithAIDataUtils.getLayer(false) != null; + if (MapWithAIDataUtils.getMapWithAIData(MapWithAIDataUtils.getLayer(true))) { + createMessageDialog(); + } else if (hasLayer) { + toggleLayer(); + } + } + } + + private static void toggleLayer() { + final OsmDataLayer mapwithai = MapWithAIDataUtils.getLayer(false); + final OsmDataLayer osmData = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class).stream() + .filter(layer -> !(layer instanceof MapWithAILayer)).findFirst().orElse(null); + final Layer currentLayer = MainApplication.getLayerManager().getActiveLayer(); + if (currentLayer != null) { + if (currentLayer.equals(mapwithai) && osmData != null) { + MainApplication.getLayerManager().setActiveLayer(osmData); + } else if (currentLayer.equals(osmData) && mapwithai != null) { + MainApplication.getLayerManager().setActiveLayer(mapwithai); + } } } 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 925254e..8e1eec1 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 @@ -182,15 +182,20 @@ public final class MapWithAIDataUtils { * Get data for a {@link MapWithAILayer} * * @param layer The {@link MapWithAILayer} to add data to + * @return true if data was downloaded */ - public static void getMapWithAIData(MapWithAILayer layer) { + public static boolean getMapWithAIData(MapWithAILayer layer) { final List osmLayers = MainApplication.getLayerManager().getLayersOfType(OsmDataLayer.class) .stream().filter(obj -> !MapWithAILayer.class.isInstance(obj)).collect(Collectors.toList()); + boolean gotData = false; for (final OsmDataLayer osmLayer : osmLayers) { if (!osmLayer.isLocked()) { - getMapWithAIData(layer, osmLayer); + if (getMapWithAIData(layer, osmLayer)) { + gotData = true; + } } } + return gotData; } /** @@ -198,9 +203,10 @@ public final class MapWithAIDataUtils { * * @param layer A pre-existing {@link MapWithAILayer} * @param osmLayer The osm datalayer with a set of bounds + * @return true if data was downloaded */ - public static void getMapWithAIData(MapWithAILayer layer, OsmDataLayer osmLayer) { - getMapWithAIData(layer, + public static boolean getMapWithAIData(MapWithAILayer layer, OsmDataLayer osmLayer) { + return getMapWithAIData(layer, osmLayer.getDataSet().getDataSourceBounds().stream().map(Bounds::toBBox).collect(Collectors.toList())); } @@ -209,9 +215,10 @@ public final class MapWithAIDataUtils { * * @param layer A pre-existing {@link MapWithAILayer} * @param bboxes The bboxes to get the data in + * @return true if data was downloaded */ - public static void getMapWithAIData(MapWithAILayer layer, BBox... bboxes) { - getMapWithAIData(layer, Arrays.asList(bboxes)); + public static boolean getMapWithAIData(MapWithAILayer layer, BBox... bboxes) { + return getMapWithAIData(layer, Arrays.asList(bboxes)); } /** @@ -219,8 +226,9 @@ public final class MapWithAIDataUtils { * * @param layer A pre-existing {@link MapWithAILayer} * @param bboxes The bboxes to get the data in + * @return true if data was downloaded */ - public static void getMapWithAIData(MapWithAILayer layer, Collection bboxes) { + public static boolean getMapWithAIData(MapWithAILayer layer, Collection bboxes) { final DataSet mapWithAISet = layer.getDataSet(); final List mapWithAIBounds = mapWithAISet.getDataSourceBounds().stream().map(Bounds::toBBox) .collect(Collectors.toList()); @@ -228,16 +236,19 @@ public final class MapWithAIDataUtils { .filter(bbox -> mapWithAIBounds.stream().noneMatch(tBBox -> tBBox.bounds(bbox))) .collect(Collectors.toList()); final List toDownload = reduceBBox(mapWithAIBounds, editSetBBoxes); - getForkJoinPool().execute(() -> { - final DataSet newData = getData(toDownload); - final Lock lock = layer.getLock(); - lock.lock(); - try { - layer.mergeFrom(newData); - } finally { - lock.unlock(); - } - }); + if (!toDownload.isEmpty()) { + getForkJoinPool().execute(() -> { + final DataSet newData = getData(toDownload); + final Lock lock = layer.getLock(); + lock.lock(); + try { + layer.mergeFrom(newData); + } finally { + lock.unlock(); + } + }); + } + return !toDownload.isEmpty(); } private static List reduceBBox(List alreadyDownloaded, List wantToDownload) { diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java index 69c95ea..ab6d43c 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java @@ -197,7 +197,7 @@ public final class MapWithAIPreferenceHelper { } /** - * @return A map of tags to replacement tags (use {@link Tag.ofString} to parse) + * @return A map of tags to replacement tags (use {@link Tag#ofString} to parse) */ public static Map getReplacementTags() { final Map defaultMap = Collections.emptyMap(); diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/OsmReaderCustom.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/OsmReaderCustom.java index e880041..cc192b6 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/OsmReaderCustom.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/OsmReaderCustom.java @@ -41,7 +41,6 @@ public class OsmReaderCustom extends OsmReader { * @param source the source input stream. Must not be null. * @param progressMonitor the progress monitor. If null, {@link NullProgressMonitor#INSTANCE} is assumed * @param convertUnknownToTags true if unknown xml attributes should be kept as tags - * @param saveOriginalId if true, keep the original id (as a tag, "current_id") * * @return the dataset with the parsed data * @throws IllegalDataException if an error was found while parsing the data from the source diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWays.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWays.java index 69f27b1..a0f899e 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWays.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/commands/MergeDuplicateWays.java @@ -117,8 +117,8 @@ public class MergeDuplicateWays extends Command { /** * Check for ways that are (partial) duplicates, and if so merge them * - * @param way A way to check - * @return non-null command if there are duplicate ways + * @param way A way to check + * @param commands A list of commands to add to */ public static void checkForDuplicateWays(Way way, List commands) { final Collection nearbyWays = way.getDataSet().searchWays(way.getBBox()); @@ -294,7 +294,7 @@ public class MergeDuplicateWays extends Command { * * @param way1 An initial way with nodes * @param way2 A way that may have duplicate nodes with way1 - * @return A map of node -> node(s) duplicates + * @return A map of node -> node(s) duplicates */ public static Map, Map> getDuplicateNodes(Way way1, Way way2) { final Map, Map> duplicateNodes = new LinkedHashMap<>();