Skip to content

Commit

Permalink
Allow cancellation of download
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed May 30, 2024
1 parent fb5ac8c commit b8c81b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -340,18 +342,38 @@ 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);
}

/**
* Perform the download in a background thread
*/
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);
}
}
}

Expand Down

0 comments on commit b8c81b4

Please sign in to comment.