| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions.downloadtasks;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.Component;
|
|---|
| 5 | import java.lang.reflect.InvocationTargetException;
|
|---|
| 6 | import java.net.URL;
|
|---|
| 7 | import java.util.HashSet;
|
|---|
| 8 | import java.util.Set;
|
|---|
| 9 | import java.util.concurrent.Future;
|
|---|
| 10 |
|
|---|
| 11 | import javax.swing.SwingUtilities;
|
|---|
| 12 |
|
|---|
| 13 | import org.openstreetmap.josm.Main;
|
|---|
| 14 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 16 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
|---|
| 17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 18 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
|---|
| 19 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
|---|
| 20 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
|---|
| 21 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 22 | import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
|
|---|
| 23 |
|
|---|
| 24 | /**
|
|---|
| 25 | * Common abstract implementation of other changeset download tasks.
|
|---|
| 26 | * @since 10124
|
|---|
| 27 | */
|
|---|
| 28 | public 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<?> 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<?> 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 | }
|
|---|