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