1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.downloadtasks;
|
---|
3 |
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.net.URL;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Set;
|
---|
8 | import java.util.concurrent.Future;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.Bounds;
|
---|
12 | import org.openstreetmap.josm.data.osm.Changeset;
|
---|
13 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
15 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Common abstract implementation of other changeset download tasks.
|
---|
19 | * @since 10124
|
---|
20 | */
|
---|
21 | public abstract class AbstractChangesetDownloadTask extends AbstractDownloadTask<Set<Changeset>> {
|
---|
22 |
|
---|
23 | abstract class RunnableDownloadTask extends PleaseWaitRunnable {
|
---|
24 | /** the reader object used to read changesets from the API */
|
---|
25 | protected final OsmServerChangesetReader reader = new OsmServerChangesetReader();
|
---|
26 | /** the set of downloaded changesets */
|
---|
27 | protected final Set<Changeset> downloadedChangesets = new HashSet<>();
|
---|
28 | /** keeps the last exception thrown in the task, if any */
|
---|
29 | protected Exception lastException;
|
---|
30 |
|
---|
31 | RunnableDownloadTask(Component parent, String title) {
|
---|
32 | super(parent, title, false /* don't ignore exceptions */);
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | protected void cancel() {
|
---|
37 | setCanceled(true);
|
---|
38 | synchronized (this) {
|
---|
39 | if (reader != null) {
|
---|
40 | reader.cancel();
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | protected final void rememberLastException(Exception e) {
|
---|
46 | lastException = e;
|
---|
47 | setFailed(true);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | private RunnableDownloadTask downloadTaskRunnable;
|
---|
52 |
|
---|
53 | protected final void setDownloadTask(RunnableDownloadTask downloadTask) {
|
---|
54 | this.downloadTaskRunnable = downloadTask;
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public final Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
|
---|
59 | return download();
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Asynchronously launches the changeset download task. This is equivalent to {@code download(false, null, null)}.
|
---|
64 | *
|
---|
65 | * You can wait for the asynchronous download task to finish by synchronizing on the returned
|
---|
66 | * {@link Future}, but make sure not to freeze up JOSM. Example:
|
---|
67 | * <pre>
|
---|
68 | * Future<?> future = task.download();
|
---|
69 | * // DON'T run this on the Swing EDT or JOSM will freeze
|
---|
70 | * future.get(); // waits for the dowload task to complete
|
---|
71 | * </pre>
|
---|
72 | *
|
---|
73 | * The following example uses a pattern which is better suited if a task is launched from the Swing EDT:
|
---|
74 | * <pre>
|
---|
75 | * final Future<?> future = task.download();
|
---|
76 | * Runnable runAfterTask = new Runnable() {
|
---|
77 | * public void run() {
|
---|
78 | * // this is not strictly necessary because of the type of executor service
|
---|
79 | * // Main.worker is initialized with, but it doesn't harm either
|
---|
80 | * //
|
---|
81 | * future.get(); // wait for the download task to complete
|
---|
82 | * doSomethingAfterTheTaskCompleted();
|
---|
83 | * }
|
---|
84 | * }
|
---|
85 | * Main.worker.submit(runAfterTask);
|
---|
86 | * </pre>
|
---|
87 | *
|
---|
88 | * @return the future representing the asynchronous task
|
---|
89 | */
|
---|
90 | public final Future<?> download() {
|
---|
91 | return Main.worker.submit(downloadTaskRunnable);
|
---|
92 | }
|
---|
93 |
|
---|
94 | @Override
|
---|
95 | public final Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
|
---|
96 | return Main.worker.submit(downloadTaskRunnable);
|
---|
97 | }
|
---|
98 |
|
---|
99 | @Override
|
---|
100 | public final void cancel() {
|
---|
101 | downloadTaskRunnable.cancel();
|
---|
102 | }
|
---|
103 |
|
---|
104 | @Override
|
---|
105 | public String getConfirmationMessage(URL url) {
|
---|
106 | return null;
|
---|
107 | }
|
---|
108 | }
|
---|