source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/DownloadRelationTask.java@ 13751

Last change on this file since 13751 was 13486, checked in by Don-vip, 6 years ago

fix #8039, see #10456 - fix bugs with non-downloadable layers

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