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