[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.dialogs.relation;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
| 6 | import java.io.IOException;
|
---|
[10212] | 7 | import java.lang.reflect.InvocationTargetException;
|
---|
[2711] | 8 | import java.util.Collection;
|
---|
| 9 |
|
---|
| 10 | import javax.swing.SwingUtilities;
|
---|
| 11 |
|
---|
| 12 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 13 | import org.openstreetmap.josm.data.osm.DataSetMerger;
|
---|
[12663] | 14 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
[2711] | 15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
| 16 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
[12630] | 17 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
[2711] | 18 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
| 19 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
| 20 | import org.openstreetmap.josm.io.OsmServerObjectReader;
|
---|
| 21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
[2847] | 22 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[12620] | 23 | import org.openstreetmap.josm.tools.Logging;
|
---|
[2711] | 24 | import org.xml.sax.SAXException;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * The asynchronous task for fully downloading a collection of relations. Does a full download
|
---|
[5266] | 28 | * for each relations and merges the relation into an {@link OsmDataLayer}
|
---|
[2711] | 29 | *
|
---|
| 30 | */
|
---|
| 31 | public class DownloadRelationTask extends PleaseWaitRunnable {
|
---|
[4310] | 32 | private boolean canceled;
|
---|
[2711] | 33 | private Exception lastException;
|
---|
[9078] | 34 | private final Collection<Relation> relations;
|
---|
| 35 | private final OsmDataLayer layer;
|
---|
[2711] | 36 | private OsmServerObjectReader objectReader;
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Creates the download task
|
---|
| 40 | *
|
---|
| 41 | * @param relations a collection of relations. Must not be null.
|
---|
| 42 | * @param layer the layer which data is to be merged into
|
---|
[8291] | 43 | * @throws IllegalArgumentException if relations is null
|
---|
| 44 | * @throws IllegalArgumentException if layer is null
|
---|
[2711] | 45 | */
|
---|
[8291] | 46 | public DownloadRelationTask(Collection<Relation> relations, OsmDataLayer layer) {
|
---|
[2711] | 47 | super(tr("Download relations"), false /* don't ignore exception */);
|
---|
[2847] | 48 | CheckParameterUtil.ensureParameterNotNull(relations, "relations");
|
---|
| 49 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
[2711] | 50 | this.relations = relations;
|
---|
| 51 | this.layer = layer;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | @Override
|
---|
| 55 | protected void cancel() {
|
---|
[4310] | 56 | canceled = true;
|
---|
[8510] | 57 | synchronized (this) {
|
---|
[2711] | 58 | if (objectReader != null) {
|
---|
| 59 | objectReader.cancel();
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | @Override
|
---|
| 65 | protected void finish() {
|
---|
[4310] | 66 | if (canceled)
|
---|
[2711] | 67 | return;
|
---|
| 68 | if (lastException != null) {
|
---|
| 69 | ExceptionDialogUtil.explainException(lastException);
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | @Override
|
---|
| 74 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
| 75 | try {
|
---|
| 76 | final DataSet allDownloads = new DataSet();
|
---|
[8510] | 77 | int i = 0;
|
---|
[2751] | 78 | getProgressMonitor().setTicksCount(relations.size());
|
---|
[2711] | 79 | for (Relation relation: relations) {
|
---|
[2751] | 80 | i++;
|
---|
[8509] | 81 | getProgressMonitor().setCustomText(tr("({0}/{1}): Downloading relation ''{2}''...", i, relations.size(),
|
---|
| 82 | relation.getDisplayName(DefaultNameFormatter.getInstance())));
|
---|
[2711] | 83 | synchronized (this) {
|
---|
[4310] | 84 | if (canceled) return;
|
---|
[2711] | 85 | objectReader = new OsmServerObjectReader(relation.getPrimitiveId(), true /* full download */);
|
---|
| 86 | }
|
---|
[2751] | 87 | DataSet dataSet = objectReader.parseOsm(
|
---|
| 88 | getProgressMonitor().createSubTaskMonitor(0, false)
|
---|
| 89 | );
|
---|
[2711] | 90 | if (dataSet == null)
|
---|
| 91 | return;
|
---|
| 92 | synchronized (this) {
|
---|
[4310] | 93 | if (canceled) return;
|
---|
[2711] | 94 | objectReader = null;
|
---|
| 95 | }
|
---|
| 96 | DataSetMerger merger = new DataSetMerger(allDownloads, dataSet);
|
---|
| 97 | merger.merge();
|
---|
[2751] | 98 | getProgressMonitor().worked(1);
|
---|
[2711] | 99 | }
|
---|
| 100 |
|
---|
[10611] | 101 | SwingUtilities.invokeAndWait(() -> {
|
---|
| 102 | layer.mergeFrom(allDownloads);
|
---|
| 103 | layer.onPostDownloadFromServer();
|
---|
[12630] | 104 | MainApplication.getMap().repaint();
|
---|
[10611] | 105 | });
|
---|
[10212] | 106 | } catch (OsmTransferException | InvocationTargetException | InterruptedException e) {
|
---|
[4310] | 107 | if (canceled) {
|
---|
[12620] | 108 | Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
[2711] | 109 | return;
|
---|
| 110 | }
|
---|
| 111 | lastException = e;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|