| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.gui.dialogs.changeset;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.io.IOException;
|
|---|
| 7 | import java.util.Collection;
|
|---|
| 8 | import java.util.HashSet;
|
|---|
| 9 | import java.util.List;
|
|---|
| 10 | import java.util.Set;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.SwingUtilities;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.Changeset;
|
|---|
| 15 | import org.openstreetmap.josm.data.osm.ChangesetCache;
|
|---|
| 16 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
|---|
| 17 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
|---|
| 18 | import org.openstreetmap.josm.io.OsmServerChangesetReader;
|
|---|
| 19 | import org.openstreetmap.josm.io.OsmTransferException;
|
|---|
| 20 | import org.xml.sax.SAXException;
|
|---|
| 21 |
|
|---|
| 22 | public class DownloadChangesetsTask extends PleaseWaitRunnable{
|
|---|
| 23 |
|
|---|
| 24 | private Set<Integer> idsToDownload;
|
|---|
| 25 | private OsmServerChangesetReader reader;
|
|---|
| 26 | private boolean cancelled;
|
|---|
| 27 | private Exception lastException;
|
|---|
| 28 | private List<Changeset> downloadedChangesets;
|
|---|
| 29 |
|
|---|
| 30 | public DownloadChangesetsTask(Collection<Integer> ids) {
|
|---|
| 31 | super(tr("Download changesets"));
|
|---|
| 32 | idsToDownload = new HashSet<Integer>();
|
|---|
| 33 | if (ids == null || ids.isEmpty())
|
|---|
| 34 | return;
|
|---|
| 35 | for (int id: ids) {
|
|---|
| 36 | if (id <= 0) {
|
|---|
| 37 | continue;
|
|---|
| 38 | }
|
|---|
| 39 | idsToDownload.add(id);
|
|---|
| 40 | }
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override
|
|---|
| 44 | protected void cancel() {
|
|---|
| 45 | cancelled = true;
|
|---|
| 46 | synchronized (this) {
|
|---|
| 47 | if (reader != null) {
|
|---|
| 48 | reader.cancel();
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | protected void finish() {
|
|---|
| 55 | if (cancelled)
|
|---|
| 56 | return;
|
|---|
| 57 | if (lastException != null) {
|
|---|
| 58 | ExceptionDialogUtil.explainException(lastException);
|
|---|
| 59 | }
|
|---|
| 60 | Runnable r = new Runnable() {
|
|---|
| 61 | public void run() {
|
|---|
| 62 | ChangesetCache.getInstance().update(downloadedChangesets);
|
|---|
| 63 | }
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 66 | if (SwingUtilities.isEventDispatchThread()) {
|
|---|
| 67 | r.run();
|
|---|
| 68 | } else {
|
|---|
| 69 | SwingUtilities.invokeLater(r);
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @Override
|
|---|
| 74 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
|---|
| 75 | try {
|
|---|
| 76 | synchronized (this) {
|
|---|
| 77 | reader = new OsmServerChangesetReader();
|
|---|
| 78 | }
|
|---|
| 79 | downloadedChangesets = reader.readChangesets(idsToDownload, getProgressMonitor().createSubTaskMonitor(0, false));
|
|---|
| 80 | } catch(Exception e) {
|
|---|
| 81 | if (cancelled)
|
|---|
| 82 | // ignore exception if cancelled
|
|---|
| 83 | return;
|
|---|
| 84 | if (e instanceof RuntimeException)
|
|---|
| 85 | throw (RuntimeException)e;
|
|---|
| 86 | lastException = e;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | }
|
|---|