Skip to content

Commit f7c1d3d

Browse files
committed
Fix #23529: JSON downloads may wait on EDT while EDT is waiting on downloads to finish
Signed-off-by: Taylor Smock <[email protected]>
1 parent fbd3f10 commit f7c1d3d

File tree

2 files changed

+27
-30
lines changed

2 files changed

+27
-30
lines changed

src/main/java/org/openstreetmap/josm/plugins/mapwithai/actions/AddMapWithAILayerAction.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,17 @@ public AddMapWithAILayerAction(MapWithAIInfo info) {
8484

8585
@Override
8686
public void actionPerformed(ActionEvent e) {
87-
if (!isEnabled()) {
88-
return;
87+
if (isEnabled()) {
88+
MainApplication.worker.execute(() -> realRun(this.info));
8989
}
90+
}
9091

92+
/**
93+
* Run the download tasks. This should be run off of the EDT, see #23529.
94+
*
95+
* @param info The external data to download
96+
*/
97+
private static void realRun(MapWithAIInfo info) {
9198
MapWithAILayer layer = MapWithAIDataUtils.getLayer(false);
9299
final DataSet ds;
93100
final OsmData<?, ?, ?, ?> boundsSource;

src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIDataUtils.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.TreeSet;
1919
import java.util.concurrent.ForkJoinPool;
2020
import java.util.concurrent.ForkJoinTask;
21+
import java.util.concurrent.atomic.AtomicBoolean;
2122
import java.util.stream.Collectors;
2223

2324
import org.openstreetmap.josm.data.Bounds;
@@ -213,37 +214,26 @@ private static void mergeDataSets(final DataSet original, final List<ForkJoinTas
213214
}
214215
}
215216

216-
private static boolean confirmBigDownload(List<Bounds> realBounds) {
217-
final var confirmation = new ConfirmBigDownload(realBounds);
218-
GuiHelper.runInEDTAndWait(confirmation);
219-
return confirmation.confirmed();
220-
}
221-
222-
private static class ConfirmBigDownload implements Runnable {
223-
Boolean bool;
224-
final List<?> realBounds;
225-
226-
public ConfirmBigDownload(List<?> realBounds) {
227-
this.realBounds = realBounds;
228-
}
229-
230-
@Override
231-
public void run() {
232-
bool = ConditionalOptionPaneUtil.showConfirmationDialog(MapWithAIPlugin.NAME.concat(".alwaysdownload"),
233-
null,
217+
/**
218+
* Confirm a large download
219+
*
220+
* @param realBounds The list of bounds that will be downloaded
221+
* @return {@code true} if the user still wants to download data
222+
*/
223+
private static synchronized boolean confirmBigDownload(List<Bounds> realBounds) {
224+
final var confirmation = new AtomicBoolean(false);
225+
// This is not a separate class since we don't want to show multiple
226+
// confirmation dialogs
227+
// which is why this method is synchronized.
228+
GuiHelper.runInEDTAndWait(() -> {
229+
final var confirmed = ConditionalOptionPaneUtil.showConfirmationDialog(
230+
MapWithAIPlugin.NAME.concat(".alwaysdownload"), null,
234231
tr("You are going to make {0} requests to the MapWithAI server. This may take some time. <br /> Continue?",
235232
realBounds.size()),
236233
null, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, JOptionPane.YES_OPTION);
237-
}
238-
239-
/**
240-
* Check if the user confirmed the download
241-
*
242-
* @return {@code true} if the user wants to continue
243-
*/
244-
public boolean confirmed() {
245-
return bool;
246-
}
234+
confirmation.set(confirmed);
235+
});
236+
return confirmation.get();
247237
}
248238

249239
/**

0 commit comments

Comments
 (0)