From b8c81b4713057d95a2c41f2dc8e359dcbb1404a5 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Thu, 30 May 2024 10:25:38 -0600 Subject: [PATCH] Allow cancellation of download Signed-off-by: Taylor Smock --- .../MapRouletteDownloadTaskBox.java | 11 +++++-- .../gui/task/list/TaskListPanel.java | 32 ++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/maproulette/actions/downloadtasks/MapRouletteDownloadTaskBox.java b/src/main/java/org/openstreetmap/josm/plugins/maproulette/actions/downloadtasks/MapRouletteDownloadTaskBox.java index 7a6b9d3..ce3c0f0 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/maproulette/actions/downloadtasks/MapRouletteDownloadTaskBox.java +++ b/src/main/java/org/openstreetmap/josm/plugins/maproulette/actions/downloadtasks/MapRouletteDownloadTaskBox.java @@ -66,6 +66,7 @@ private static class DownloadTask extends PleaseWaitRunnable { private final Bounds bounds; private final DownloadParams settings; private TaskClusteredPoint[] tasks; + private boolean cancelled; protected DownloadTask(DownloadParams settings, Bounds downloadArea, ProgressMonitor progressMonitor) { super(tr("Downloading MapRoulette Data"), progressMonitor, false); @@ -76,6 +77,8 @@ protected DownloadTask(DownloadParams settings, Bounds downloadArea, ProgressMon @Override protected void cancel() { // We don't support cancelling right now + this.getProgressMonitor().cancel(); + this.cancelled = true; } @Override @@ -84,8 +87,10 @@ protected void realRun() throws IOException, OsmTransferException { tasks = TaskAPI.box(bounds.getMinLon(), bounds.getMinLat(), bounds.getMaxLon(), bounds.getMaxLat(), 1_000, 0, true, null, null, false, true, true); for (var task : tasks) { - // Force cache the challenges - TaskCache.isHidden(task); + if (!this.cancelled && !this.getProgressMonitor().isCanceled()) { + // Force cache the challenges + TaskCache.isHidden(task); + } } } catch (UnauthorizedException unauthorizedException) { ExceptionDialogUtil.explainException(unauthorizedException); @@ -103,7 +108,7 @@ protected void realRun() throws IOException, OsmTransferException { @Override protected void finish() { - if (tasks != null) { + if (tasks != null && !this.cancelled && !this.getProgressMonitor().isCanceled()) { final var taskList = new ArrayList<>(Arrays.asList(tasks)); final var tcMap = taskList.stream().collect(Collectors.toMap(TaskClusteredPoint::id, i -> i)); final var currentLayers = MainApplication.getLayerManager() diff --git a/src/main/java/org/openstreetmap/josm/plugins/maproulette/gui/task/list/TaskListPanel.java b/src/main/java/org/openstreetmap/josm/plugins/maproulette/gui/task/list/TaskListPanel.java index 23843b2..ce8b21d 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/maproulette/gui/task/list/TaskListPanel.java +++ b/src/main/java/org/openstreetmap/josm/plugins/maproulette/gui/task/list/TaskListPanel.java @@ -55,6 +55,7 @@ import org.openstreetmap.josm.plugins.maproulette.gui.widgets.DefaultPanelListCellRenderer; import org.openstreetmap.josm.plugins.maproulette.util.ExceptionDialogUtil; import org.openstreetmap.josm.tools.GBC; +import org.openstreetmap.josm.tools.ImageProvider; import org.openstreetmap.josm.tools.OpenBrowser; import org.openstreetmap.josm.tools.Shortcut; @@ -327,6 +328,7 @@ private static final class DownloadDataAction extends JosmAction { */ @Serial private static final long serialVersionUID = -4078764340309276574L; + private MapRouletteDownloadTaskBox task; /** * Create a new action @@ -340,8 +342,7 @@ private static final class DownloadDataAction extends JosmAction { @Override public void actionPerformed(ActionEvent e) { - this.setEnabled(false); // Keep the user from hitting the download button multiple times in short order - MainApplication.worker.execute(this::download); + this.switchType(true); } /** @@ -349,9 +350,30 @@ public void actionPerformed(ActionEvent e) { */ private void download() { final var bounds = MainApplication.getMap().mapView.getState().getViewArea().getLatLonBoundsBox(); - new MapRouletteDownloadTaskBox().download(new DownloadParams().withNewLayer(false), bounds, - NullProgressMonitor.INSTANCE); - MainApplication.worker.submit(() -> GuiHelper.runInEDT(() -> this.setEnabled(true))); + final var task2 = new MapRouletteDownloadTaskBox(); + task2.download(new DownloadParams().withNewLayer(false), bounds, NullProgressMonitor.INSTANCE); + synchronized (this) { + if (this.task != null) { + this.task.cancel(); + } + this.task = task2; + } + MainApplication.worker.submit(() -> GuiHelper.runInEDT(() -> this.switchType(false))); + } + + private void switchType(boolean performDownload) { + if (performDownload && this.task == null) { + MainApplication.worker.execute(this::download); + new ImageProvider("cancel").getResource().attachImageIcon(this); + this.putValue(NAME, tr("Cancel")); + } else { + if (this.task != null) { + this.task.cancel(); + this.task = null; + } + this.putValue(NAME, tr("Download Data")); + new ImageProvider("download").getResource().attachImageIcon(this); + } } }