From f7c1d3d8b0194e5260033205bfcc4284ab4308de Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Tue, 12 Mar 2024 06:46:53 -0600 Subject: [PATCH] Fix #23529: JSON downloads may wait on EDT while EDT is waiting on downloads to finish Signed-off-by: Taylor Smock --- .../actions/AddMapWithAILayerAction.java | 11 ++++- .../mapwithai/backend/MapWithAIDataUtils.java | 46 ++++++++----------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerAction.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerAction.java index 7bdb05c..40c2c20 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerAction.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerAction.java @@ -84,10 +84,17 @@ public class AddMapWithAILayerAction extends JosmAction implements AdaptableActi @Override public void actionPerformed(ActionEvent e) { - if (!isEnabled()) { - return; + if (isEnabled()) { + MainApplication.worker.execute(() -> realRun(this.info)); } + } + /** + * Run the download tasks. This should be run off of the EDT, see #23529. + * + * @param info The external data to download + */ + private static void realRun(MapWithAIInfo info) { MapWithAILayer layer = MapWithAIDataUtils.getLayer(false); final DataSet ds; final OsmData boundsSource; 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 fb1faa4..37b4a99 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 @@ -18,6 +18,7 @@ import java.util.Optional; import java.util.TreeSet; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.stream.Collectors; import org.openstreetmap.josm.data.Bounds; @@ -213,37 +214,26 @@ public final class MapWithAIDataUtils { } } - private static boolean confirmBigDownload(List realBounds) { - final var confirmation = new ConfirmBigDownload(realBounds); - GuiHelper.runInEDTAndWait(confirmation); - return confirmation.confirmed(); - } - - private static class ConfirmBigDownload implements Runnable { - Boolean bool; - final List realBounds; - - public ConfirmBigDownload(List realBounds) { - this.realBounds = realBounds; - } - - @Override - public void run() { - bool = ConditionalOptionPaneUtil.showConfirmationDialog(MapWithAIPlugin.NAME.concat(".alwaysdownload"), - null, + /** + * Confirm a large download + * + * @param realBounds The list of bounds that will be downloaded + * @return {@code true} if the user still wants to download data + */ + private static synchronized boolean confirmBigDownload(List realBounds) { + final var confirmation = new AtomicBoolean(false); + // This is not a separate class since we don't want to show multiple + // confirmation dialogs + // which is why this method is synchronized. + GuiHelper.runInEDTAndWait(() -> { + final var confirmed = ConditionalOptionPaneUtil.showConfirmationDialog( + MapWithAIPlugin.NAME.concat(".alwaysdownload"), null, tr("You are going to make {0} requests to the MapWithAI server. This may take some time.
Continue?", realBounds.size()), null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.YES_OPTION); - } - - /** - * Check if the user confirmed the download - * - * @return {@code true} if the user wants to continue - */ - public boolean confirmed() { - return bool; - } + confirmation.set(confirmed); + }); + return confirmation.get(); } /**