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

Last change on this file since 12630 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 4.2 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.Relation;
15import org.openstreetmap.josm.gui.DefaultNameFormatter;
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 *
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 }
53
54 @Override
55 protected void cancel() {
56 canceled = true;
57 synchronized (this) {
58 if (objectReader != null) {
59 objectReader.cancel();
60 }
61 }
62 }
63
64 @Override
65 protected void finish() {
66 if (canceled)
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 getProgressMonitor().setTicksCount(relations.size());
79 for (Relation relation: relations) {
80 i++;
81 getProgressMonitor().setCustomText(tr("({0}/{1}): Downloading relation ''{2}''...", i, relations.size(),
82 relation.getDisplayName(DefaultNameFormatter.getInstance())));
83 synchronized (this) {
84 if (canceled) return;
85 objectReader = new OsmServerObjectReader(relation.getPrimitiveId(), true /* full download */);
86 }
87 DataSet dataSet = objectReader.parseOsm(
88 getProgressMonitor().createSubTaskMonitor(0, false)
89 );
90 if (dataSet == null)
91 return;
92 synchronized (this) {
93 if (canceled) return;
94 objectReader = null;
95 }
96 DataSetMerger merger = new DataSetMerger(allDownloads, dataSet);
97 merger.merge();
98 getProgressMonitor().worked(1);
99 }
100
101 SwingUtilities.invokeAndWait(() -> {
102 layer.mergeFrom(allDownloads);
103 layer.onPostDownloadFromServer();
104 MainApplication.getMap().repaint();
105 });
106 } catch (OsmTransferException | InvocationTargetException | InterruptedException e) {
107 if (canceled) {
108 Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
109 return;
110 }
111 lastException = e;
112 }
113 }
114}
Note: See TracBrowser for help on using the repository browser.