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

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

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 5.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import java.awt.Component;
5import java.lang.reflect.InvocationTargetException;
6import java.net.URL;
7import java.util.HashSet;
8import java.util.Set;
9import java.util.concurrent.Future;
10
11import javax.swing.SwingUtilities;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Bounds;
15import org.openstreetmap.josm.data.osm.Changeset;
16import org.openstreetmap.josm.data.osm.ChangesetCache;
17import org.openstreetmap.josm.gui.PleaseWaitRunnable;
18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
19import org.openstreetmap.josm.io.OsmServerChangesetReader;
20import org.openstreetmap.josm.tools.ExceptionUtil;
21import org.openstreetmap.josm.tools.Logging;
22import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
23
24/**
25 * Common abstract implementation of other changeset download tasks.
26 * @since 10124
27 */
28public abstract class AbstractChangesetDownloadTask extends AbstractDownloadTask<Set<Changeset>> {
29
30 abstract class RunnableDownloadTask extends PleaseWaitRunnable {
31 /** the reader object used to read changesets from the API */
32 protected final OsmServerChangesetReader reader = new OsmServerChangesetReader();
33 /** the set of downloaded changesets */
34 protected final Set<Changeset> downloadedChangesets = new HashSet<>();
35 /** keeps the last exception thrown in the task, if any */
36 protected Exception lastException;
37
38 RunnableDownloadTask(Component parent, String title) {
39 super(parent, title, false /* don't ignore exceptions */);
40 }
41
42 @Override
43 protected void cancel() {
44 setCanceled(true);
45 synchronized (this) {
46 if (reader != null) {
47 reader.cancel();
48 }
49 }
50 }
51
52 protected final void rememberLastException(Exception e) {
53 lastException = e;
54 setFailed(true);
55 }
56
57 protected final void updateChangesets() {
58 // update the global changeset cache with the downloaded changesets.
59 // this will trigger change events which views are listening to. They
60 // will update their views accordingly.
61 //
62 // Run on the EDT because UI updates are triggered.
63 //
64 Runnable r = () -> ChangesetCache.getInstance().update(downloadedChangesets);
65 if (SwingUtilities.isEventDispatchThread()) {
66 r.run();
67 } else {
68 try {
69 SwingUtilities.invokeAndWait(r);
70 } catch (InterruptedException e) {
71 Logging.warn("InterruptedException in "+getClass().getSimpleName()+" while updating changeset cache");
72 Thread.currentThread().interrupt();
73 } catch (InvocationTargetException e) {
74 Throwable t = e.getTargetException();
75 if (t instanceof RuntimeException) {
76 BugReportExceptionHandler.handleException(t);
77 } else if (t instanceof Exception) {
78 ExceptionUtil.explainException(e);
79 } else {
80 BugReportExceptionHandler.handleException(t);
81 }
82 }
83 }
84 }
85 }
86
87 private RunnableDownloadTask downloadTaskRunnable;
88
89 protected final void setDownloadTask(RunnableDownloadTask downloadTask) {
90 this.downloadTaskRunnable = downloadTask;
91 }
92
93 @Override
94 public final Future<?> download(boolean newLayer, Bounds downloadArea, ProgressMonitor progressMonitor) {
95 return download();
96 }
97
98 /**
99 * Asynchronously launches the changeset download task. This is equivalent to {@code download(false, null, null)}.
100 *
101 * You can wait for the asynchronous download task to finish by synchronizing on the returned
102 * {@link Future}, but make sure not to freeze up JOSM. Example:
103 * <pre>
104 * Future&lt;?&gt; future = task.download();
105 * // DON'T run this on the Swing EDT or JOSM will freeze
106 * future.get(); // waits for the dowload task to complete
107 * </pre>
108 *
109 * The following example uses a pattern which is better suited if a task is launched from the Swing EDT:
110 * <pre>
111 * final Future&lt;?&gt; future = task.download();
112 * Runnable runAfterTask = new Runnable() {
113 * public void run() {
114 * // this is not strictly necessary because of the type of executor service
115 * // Main.worker is initialized with, but it doesn't harm either
116 * //
117 * future.get(); // wait for the download task to complete
118 * doSomethingAfterTheTaskCompleted();
119 * }
120 * }
121 * Main.worker.submit(runAfterTask);
122 * </pre>
123 *
124 * @return the future representing the asynchronous task
125 */
126 public final Future<?> download() {
127 return downloadTaskRunnable != null ? Main.worker.submit(downloadTaskRunnable) : null;
128 }
129
130 @Override
131 public final Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor) {
132 return downloadTaskRunnable != null ? Main.worker.submit(downloadTaskRunnable) : null;
133 }
134
135 @Override
136 public final void cancel() {
137 if (downloadTaskRunnable != null) {
138 downloadTaskRunnable.cancel();
139 }
140 }
141
142 @Override
143 public String getConfirmationMessage(URL url) {
144 return null;
145 }
146}
Note: See TracBrowser for help on using the repository browser.