Ignore:
Timestamp:
2016-04-09T16:08:55+02:00 (8 years ago)
Author:
Don-vip
Message:

refactor and move changeset download tasks to actions.downloadtasks package

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/AbstractChangesetDownloadTask.java

    r10113 r10124  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.gui.dialogs.changeset;
     2package org.openstreetmap.josm.actions.downloadtasks;
    33
     4import java.awt.Component;
     5import java.net.URL;
     6import java.util.HashSet;
    47import java.util.Set;
     8import java.util.concurrent.Future;
    59
     10import org.openstreetmap.josm.Main;
     11import org.openstreetmap.josm.data.Bounds;
    612import 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;
    716
    8 public interface ChangesetDownloadTask extends Runnable {
    9     Set<Changeset> getDownloadedChangesets();
     17/**
     18 * Common abstract implementation of other changeset download tasks.
     19 * @since 10124
     20 */
     21public abstract class AbstractChangesetDownloadTask extends AbstractDownloadTask<Set<Changeset>> {
    1022
    11     boolean isCanceled();
     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;
    1230
    13     boolean isFailed();
     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    }
    14108}
Note: See TracChangeset for help on using the changeset viewer.