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.gui.progress.ProgressMonitor;
|
---|
20 | import org.openstreetmap.josm.io.OsmServerObjectReader;
|
---|
21 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
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 | if (relations == null)
|
---|
47 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "relations"));
|
---|
48 | if (layer == null)
|
---|
49 | throw new IllegalArgumentException(tr("Parameter ''{0}'' must not be null", "layer"));
|
---|
50 | this.relations = relations;
|
---|
51 | this.layer = layer;
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | protected void cancel() {
|
---|
56 | cancelled = true;
|
---|
57 | synchronized(this) {
|
---|
58 | if (objectReader != null) {
|
---|
59 | objectReader.cancel();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | protected void finish() {
|
---|
66 | if (cancelled)
|
---|
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();
|
---|
77 | int i=0;
|
---|
78 | for (Relation relation: relations) {
|
---|
79 | progressMonitor.subTask(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(progressMonitor
|
---|
85 | .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
86 | if (dataSet == null)
|
---|
87 | return;
|
---|
88 | synchronized (this) {
|
---|
89 | if (cancelled) return;
|
---|
90 | objectReader = null;
|
---|
91 | }
|
---|
92 | DataSetMerger merger = new DataSetMerger(allDownloads, dataSet);
|
---|
93 | merger.merge();
|
---|
94 | }
|
---|
95 |
|
---|
96 | SwingUtilities.invokeAndWait(
|
---|
97 | new Runnable() {
|
---|
98 | public void run() {
|
---|
99 | layer.mergeFrom(allDownloads);
|
---|
100 | layer.fireDataChange();
|
---|
101 | layer.onPostDownloadFromServer();
|
---|
102 | Main.map.repaint();
|
---|
103 | }
|
---|
104 | }
|
---|
105 | );
|
---|
106 | } catch (Exception e) {
|
---|
107 | if (cancelled) {
|
---|
108 | System.out.println(tr("Warning: ignoring exception because task is cancelled. Exception: {0}", e
|
---|
109 | .toString()));
|
---|
110 | return;
|
---|
111 | }
|
---|
112 | lastException = e;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | }
|
---|