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 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
6 |
|
---|
7 | import java.awt.Dialog;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import javax.swing.SwingUtilities;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.DefaultNameFormatter;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
19 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
20 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
21 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
22 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
23 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
24 | import org.openstreetmap.josm.gui.progress.swing.PleaseWaitProgressMonitor;
|
---|
25 | import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
|
---|
26 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
27 | import org.openstreetmap.josm.tools.Logging;
|
---|
28 | import org.xml.sax.SAXException;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * The asynchronous task for downloading relation members.
|
---|
32 | *
|
---|
33 | */
|
---|
34 | public class DownloadRelationMemberTask extends PleaseWaitRunnable {
|
---|
35 | private boolean canceled;
|
---|
36 | private Exception lastException;
|
---|
37 | private final Set<Relation> parents = new HashSet<>();
|
---|
38 | private final Collection<OsmPrimitive> children;
|
---|
39 | private final OsmDataLayer curLayer;
|
---|
40 | private MultiFetchServerObjectReader objectReader;
|
---|
41 |
|
---|
42 | public DownloadRelationMemberTask(Relation parent, Collection<OsmPrimitive> children, OsmDataLayer curLayer, Dialog dialog) {
|
---|
43 | super(tr("Download relation members"), new PleaseWaitProgressMonitor(dialog), false /* don't ignore exception */);
|
---|
44 | if (parent != null)
|
---|
45 | this.parents.add(parent);
|
---|
46 | this.children = children;
|
---|
47 | this.curLayer = curLayer;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public DownloadRelationMemberTask(Relation parent, Collection<OsmPrimitive> children, OsmDataLayer curLayer) {
|
---|
51 | super(tr("Download relation members"), false /* don't ignore exception */);
|
---|
52 | if (parent != null)
|
---|
53 | this.parents.add(parent);
|
---|
54 | this.children = children;
|
---|
55 | this.curLayer = curLayer;
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Creates a download task for downloading the child primitives {@code children} for all parent
|
---|
60 | * relations in {@code parents}.
|
---|
61 | *
|
---|
62 | * @param parents the collection of parent relations
|
---|
63 | * @param children the collection of child primitives to download
|
---|
64 | * @param curLayer the current OSM layer
|
---|
65 | */
|
---|
66 | public DownloadRelationMemberTask(Collection<Relation> parents, Collection<OsmPrimitive> children, OsmDataLayer curLayer) {
|
---|
67 | super(tr("Download relation members"), false /* don't ignore exception */);
|
---|
68 | this.parents.addAll(parents);
|
---|
69 | this.children = children;
|
---|
70 | this.curLayer = curLayer;
|
---|
71 | }
|
---|
72 |
|
---|
73 | @Override
|
---|
74 | protected void cancel() {
|
---|
75 | canceled = true;
|
---|
76 | synchronized (this) {
|
---|
77 | if (objectReader != null) {
|
---|
78 | objectReader.cancel();
|
---|
79 | }
|
---|
80 | }
|
---|
81 | }
|
---|
82 |
|
---|
83 | @Override
|
---|
84 | protected void finish() {
|
---|
85 | MainApplication.getMap().repaint();
|
---|
86 | if (canceled)
|
---|
87 | return;
|
---|
88 | if (lastException != null) {
|
---|
89 | ExceptionDialogUtil.explainException(lastException);
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | protected String buildDownloadFeedbackMessage() {
|
---|
94 | if (parents.isEmpty()) {
|
---|
95 | return trn("Downloading {0} incomplete object",
|
---|
96 | "Downloading {0} incomplete objects",
|
---|
97 | children.size(),
|
---|
98 | children.size());
|
---|
99 | } else if (parents.size() == 1) {
|
---|
100 | Relation parent = parents.iterator().next();
|
---|
101 | return trn("Downloading {0} incomplete child of relation ''{1}''",
|
---|
102 | "Downloading {0} incomplete children of relation ''{1}''",
|
---|
103 | children.size(),
|
---|
104 | children.size(),
|
---|
105 | parent.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
106 | } else {
|
---|
107 | return trn("Downloading {0} incomplete child of {1} parent relations",
|
---|
108 | "Downloading {0} incomplete children of {1} parent relations",
|
---|
109 | children.size(),
|
---|
110 | children.size(),
|
---|
111 | parents.size());
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | @Override
|
---|
116 | protected void realRun() throws SAXException, IOException, OsmTransferException {
|
---|
117 | try {
|
---|
118 | synchronized (this) {
|
---|
119 | if (canceled) return;
|
---|
120 | objectReader = MultiFetchServerObjectReader.create();
|
---|
121 | }
|
---|
122 | objectReader.append(children);
|
---|
123 | progressMonitor.indeterminateSubTask(
|
---|
124 | buildDownloadFeedbackMessage()
|
---|
125 | );
|
---|
126 | final DataSet dataSet = objectReader.parseOsm(progressMonitor
|
---|
127 | .createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
|
---|
128 | if (dataSet == null)
|
---|
129 | return;
|
---|
130 | dataSet.deleteInvisible();
|
---|
131 | synchronized (this) {
|
---|
132 | if (canceled) return;
|
---|
133 | objectReader = null;
|
---|
134 | }
|
---|
135 |
|
---|
136 | SwingUtilities.invokeLater(() -> {
|
---|
137 | curLayer.mergeFrom(dataSet);
|
---|
138 | curLayer.onPostDownloadFromServer();
|
---|
139 | });
|
---|
140 | } catch (OsmTransferException e) {
|
---|
141 | if (canceled) {
|
---|
142 | Logging.warn(tr("Ignoring exception because task was canceled. Exception: {0}", e.toString()));
|
---|
143 | return;
|
---|
144 | }
|
---|
145 | lastException = e;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|