source: josm/trunk/src/org/openstreetmap/josm/gui/dialogs/relation/DownloadRelationMemberTask.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: 5.5 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;
5import static org.openstreetmap.josm.tools.I18n.trn;
6
7import java.awt.Dialog;
8import java.io.IOException;
9import java.util.Collection;
10import java.util.HashSet;
11import java.util.Set;
12
13import javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.data.osm.DataSet;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.osm.Relation;
18import org.openstreetmap.josm.gui.DefaultNameFormatter;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.layer.OsmDataLayer;
23import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
25import org.openstreetmap.josm.io.MultiFetchServerObjectReader;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.openstreetmap.josm.tools.Logging;
28import org.xml.sax.SAXException;
29
30/**
31 * The asynchronous task for downloading relation members.
32 *
33 */
34public 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}
Note: See TracBrowser for help on using the repository browser.