source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/AbstractChangesetDownloadTask.java@ 10124

Last change on this file since 10124 was 10124, checked in by Don-vip, 8 years ago

refactor and move changeset download tasks to actions.downloadtasks package

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import java.awt.Component;
5import java.net.URL;
6import java.util.HashSet;
7import java.util.Set;
8import java.util.concurrent.Future;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.data.Bounds;
12import org.openstreetmap.josm.data.osm.Changeset;
13import org.openstreetmap.josm.gui.PleaseWaitRunnable;
14import org.openstreetmap.josm.gui.progress.ProgressMonitor;
15import org.openstreetmap.josm.io.OsmServerChangesetReader;
16
17/**
18 * Common abstract implementation of other changeset download tasks.
19 * @since 10124
20 */
21public 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&lt;?&gt; 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&lt;?&gt; 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}
Note: See TracBrowser for help on using the repository browser.