source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTaskList.java@ 16438

Last change on this file since 16438 was 16438, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 13.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.EventQueue;
9import java.awt.geom.Area;
10import java.awt.geom.Rectangle2D;
11import java.util.ArrayList;
12import java.util.Collection;
13import java.util.LinkedHashSet;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Objects;
17import java.util.Set;
18import java.util.concurrent.CancellationException;
19import java.util.concurrent.ExecutionException;
20import java.util.concurrent.Future;
21import java.util.stream.Collectors;
22
23import javax.swing.JOptionPane;
24
25import org.openstreetmap.josm.actions.UpdateSelectionAction;
26import org.openstreetmap.josm.data.Bounds;
27import org.openstreetmap.josm.data.osm.DataSet;
28import org.openstreetmap.josm.data.osm.OsmPrimitive;
29import org.openstreetmap.josm.gui.HelpAwareOptionPane;
30import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
31import org.openstreetmap.josm.gui.MainApplication;
32import org.openstreetmap.josm.gui.Notification;
33import org.openstreetmap.josm.gui.layer.Layer;
34import org.openstreetmap.josm.gui.layer.OsmDataLayer;
35import org.openstreetmap.josm.gui.progress.ProgressMonitor;
36import org.openstreetmap.josm.gui.util.GuiHelper;
37import org.openstreetmap.josm.tools.ExceptionUtil;
38import org.openstreetmap.josm.tools.ImageProvider;
39import org.openstreetmap.josm.tools.Logging;
40import org.openstreetmap.josm.tools.Utils;
41
42/**
43 * This class encapsulates the downloading of several bounding boxes that would otherwise be too
44 * large to download in one go. Error messages will be collected for all downloads and displayed as
45 * a list in the end.
46 * @author xeen
47 * @since 6053
48 */
49public class DownloadTaskList {
50 private final List<DownloadTask> tasks = new LinkedList<>();
51 private final List<Future<?>> taskFutures = new LinkedList<>();
52 private final boolean zoomAfterDownload;
53 private ProgressMonitor progressMonitor;
54
55 /**
56 * Constructs a new {@code DownloadTaskList}. Zooms to each download area.
57 */
58 public DownloadTaskList() {
59 this(true);
60 }
61
62 /**
63 * Constructs a new {@code DownloadTaskList}.
64 * @param zoomAfterDownload whether to zoom to each download area
65 * @since 15205
66 */
67 public DownloadTaskList(boolean zoomAfterDownload) {
68 this.zoomAfterDownload = zoomAfterDownload;
69 }
70
71 private void addDownloadTask(ProgressMonitor progressMonitor, DownloadTask dt, Rectangle2D td, int i, int n) {
72 ProgressMonitor childProgress = progressMonitor.createSubTaskMonitor(1, false);
73 childProgress.setCustomText(tr("Download {0} of {1} ({2} left)", i, n, n - i));
74 dt.setZoomAfterDownload(zoomAfterDownload);
75 Future<?> future = dt.download(new DownloadParams(), new Bounds(td), childProgress);
76 taskFutures.add(future);
77 tasks.add(dt);
78 }
79
80 /**
81 * Downloads a list of areas from the OSM Server
82 * @param newLayer Set to true if all areas should be put into a single new layer
83 * @param rects The List of Rectangle2D to download
84 * @param osmData Set to true if OSM data should be downloaded
85 * @param gpxData Set to true if GPX data should be downloaded
86 * @param progressMonitor The progress monitor
87 * @return The Future representing the asynchronous download task
88 */
89 public Future<?> download(boolean newLayer, List<Rectangle2D> rects, boolean osmData, boolean gpxData, ProgressMonitor progressMonitor) {
90 this.progressMonitor = progressMonitor;
91 if (newLayer) {
92 Layer l = new OsmDataLayer(new DataSet(), OsmDataLayer.createNewName(), null);
93 MainApplication.getLayerManager().addLayer(l);
94 MainApplication.getLayerManager().setActiveLayer(l);
95 }
96
97 int n = (osmData && gpxData ? 2 : 1)*rects.size();
98 progressMonitor.beginTask(null, n);
99 int i = 0;
100 for (Rectangle2D td : rects) {
101 i++;
102 if (osmData) {
103 addDownloadTask(progressMonitor, new DownloadOsmTask(), td, i, n);
104 }
105 if (gpxData) {
106 addDownloadTask(progressMonitor, new DownloadGpsTask(), td, i, n);
107 }
108 }
109 progressMonitor.addCancelListener(() -> {
110 for (DownloadTask dt : tasks) {
111 dt.cancel();
112 }
113 });
114 return MainApplication.worker.submit(new PostDownloadProcessor(osmData));
115 }
116
117 /**
118 * Downloads a list of areas from the OSM Server
119 * @param newLayer Set to true if all areas should be put into a single new layer
120 * @param areas The Collection of Areas to download
121 * @param osmData Set to true if OSM data should be downloaded
122 * @param gpxData Set to true if GPX data should be downloaded
123 * @param progressMonitor The progress monitor
124 * @return The Future representing the asynchronous download task
125 */
126 public Future<?> download(boolean newLayer, Collection<Area> areas, boolean osmData, boolean gpxData, ProgressMonitor progressMonitor) {
127 progressMonitor.beginTask(tr("Updating data"));
128 try {
129 List<Rectangle2D> rects = areas.stream().map(Area::getBounds2D).collect(Collectors.toList());
130 return download(newLayer, rects, osmData, gpxData, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
131 } finally {
132 progressMonitor.finishTask();
133 }
134 }
135
136 /**
137 * Replies the set of ids of all complete, non-new primitives (i.e. those with !primitive.incomplete)
138 * @param ds data set
139 *
140 * @return the set of ids of all complete, non-new primitives
141 */
142 protected Set<OsmPrimitive> getCompletePrimitives(DataSet ds) {
143 return ds.allPrimitives().stream().filter(p -> !p.isIncomplete() && !p.isNew()).collect(Collectors.toSet());
144 }
145
146 /**
147 * Updates the local state of a set of primitives (given by a set of primitive ids) with the
148 * state currently held on the server.
149 *
150 * @param potentiallyDeleted a set of ids to check update from the server
151 */
152 protected void updatePotentiallyDeletedPrimitives(Set<OsmPrimitive> potentiallyDeleted) {
153 final List<OsmPrimitive> toSelect = potentiallyDeleted.stream().filter(Objects::nonNull).collect(Collectors.toList());
154 EventQueue.invokeLater(() -> UpdateSelectionAction.updatePrimitives(toSelect));
155 }
156
157 /**
158 * Processes a set of primitives (given by a set of their ids) which might be deleted on the
159 * server. First prompts the user whether he wants to check the current state on the server. If
160 * yes, retrieves the current state on the server and checks whether the primitives are indeed
161 * deleted on the server.
162 *
163 * @param potentiallyDeleted a set of primitives (given by their ids)
164 */
165 protected void handlePotentiallyDeletedPrimitives(Set<OsmPrimitive> potentiallyDeleted) {
166 ButtonSpec[] options = {
167 new ButtonSpec(
168 tr("Check on the server"),
169 new ImageProvider("ok"),
170 tr("Click to check whether objects in your local dataset are deleted on the server"),
171 null /* no specific help topic */),
172 new ButtonSpec(
173 tr("Ignore"),
174 new ImageProvider("cancel"),
175 tr("Click to abort and to resume editing"),
176 null /* no specific help topic */),
177 };
178
179 String message = "<html>" + trn(
180 "There is {0} object in your local dataset which "
181 + "might be deleted on the server.<br>If you later try to delete or "
182 + "update this the server is likely to report a conflict.",
183 "There are {0} objects in your local dataset which "
184 + "might be deleted on the server.<br>If you later try to delete or "
185 + "update them the server is likely to report a conflict.",
186 potentiallyDeleted.size(), potentiallyDeleted.size())
187 + "<br>"
188 + trn("Click <strong>{0}</strong> to check the state of this object on the server.",
189 "Click <strong>{0}</strong> to check the state of these objects on the server.",
190 potentiallyDeleted.size(),
191 options[0].text) + "<br>"
192 + tr("Click <strong>{0}</strong> to ignore." + "</html>", options[1].text);
193
194 int ret = HelpAwareOptionPane.showOptionDialog(
195 MainApplication.getMainFrame(),
196 message,
197 tr("Deleted or moved objects"),
198 JOptionPane.WARNING_MESSAGE,
199 null,
200 options,
201 options[0],
202 ht("/Action/UpdateData#SyncPotentiallyDeletedObjects")
203 );
204 if (ret != 0 /* OK */)
205 return;
206
207 updatePotentiallyDeletedPrimitives(potentiallyDeleted);
208 }
209
210 /**
211 * Replies the set of primitive ids which have been downloaded by this task list
212 *
213 * @return the set of primitive ids which have been downloaded by this task list
214 */
215 public Set<OsmPrimitive> getDownloadedPrimitives() {
216 return tasks.stream()
217 .filter(t -> t instanceof DownloadOsmTask)
218 .map(t -> ((DownloadOsmTask) t).getDownloadedData())
219 .filter(Objects::nonNull)
220 .flatMap(ds -> ds.allPrimitives().stream())
221 .collect(Collectors.toSet());
222 }
223
224 class PostDownloadProcessor implements Runnable {
225
226 private final boolean osmData;
227
228 PostDownloadProcessor(boolean osmData) {
229 this.osmData = osmData;
230 }
231
232 /**
233 * Grabs and displays the error messages after all download threads have finished.
234 */
235 @Override
236 public void run() {
237 progressMonitor.finishTask();
238
239 // wait for all download tasks to finish
240 //
241 for (Future<?> future : taskFutures) {
242 try {
243 future.get();
244 } catch (InterruptedException | ExecutionException | CancellationException e) {
245 Logging.error(e);
246 return;
247 }
248 }
249 Set<Object> errors = tasks.stream().flatMap(t -> t.getErrorObjects().stream()).collect(Collectors.toSet());
250 if (!errors.isEmpty()) {
251 final Collection<String> items = new ArrayList<>();
252 for (Object error : errors) {
253 if (error instanceof String) {
254 items.add((String) error);
255 } else if (error instanceof Exception) {
256 items.add(ExceptionUtil.explainException((Exception) error));
257 }
258 }
259
260 GuiHelper.runInEDT(() -> {
261 if (items.size() == 1 && PostDownloadHandler.isNoDataErrorMessage(items.iterator().next())) {
262 new Notification(items.iterator().next()).setIcon(JOptionPane.WARNING_MESSAGE).show();
263 } else {
264 JOptionPane.showMessageDialog(MainApplication.getMainFrame(), "<html>"
265 + tr("The following errors occurred during mass download: {0}",
266 Utils.joinAsHtmlUnorderedList(items)) + "</html>",
267 tr("Errors during download"), JOptionPane.ERROR_MESSAGE);
268 }
269 });
270
271 return;
272 }
273
274 // FIXME: this is a hack. We assume that the user canceled the whole download if at
275 // least one task was canceled or if it failed
276 if (Utils.filteredCollection(tasks, AbstractDownloadTask.class).stream()
277 .anyMatch(absTask -> absTask.isCanceled() || absTask.isFailed())) {
278 return;
279 }
280 final DataSet editDataSet = MainApplication.getLayerManager().getEditDataSet();
281 if (editDataSet != null && osmData) {
282 final List<DownloadOsmTask> osmTasks = tasks.stream()
283 .filter(t -> t instanceof DownloadOsmTask).map(t -> (DownloadOsmTask) t)
284 .filter(t -> t.getDownloadedData() != null)
285 .collect(Collectors.toList());
286 final Set<Bounds> tasksBounds = osmTasks.stream()
287 .flatMap(t -> t.getDownloadedData().getDataSourceBounds().stream())
288 .collect(Collectors.toSet());
289 final Set<Bounds> layerBounds = new LinkedHashSet<>(editDataSet.getDataSourceBounds());
290 final Set<OsmPrimitive> myPrimitives = new LinkedHashSet<>();
291 if (layerBounds.equals(tasksBounds)) {
292 // the full edit layer is updated (we have downloaded again all its current bounds)
293 myPrimitives.addAll(getCompletePrimitives(editDataSet));
294 for (DownloadOsmTask task : osmTasks) {
295 // myPrimitives.removeAll(ds.allPrimitives()) will do the same job but much slower
296 task.getDownloadedData().allPrimitives().forEach(myPrimitives::remove);
297 }
298 } else {
299 // partial update, only check what has been downloaded
300 for (DownloadOsmTask task : osmTasks) {
301 myPrimitives.addAll(task.searchPotentiallyDeletedPrimitives(editDataSet));
302 }
303 }
304 if (!myPrimitives.isEmpty()) {
305 GuiHelper.runInEDT(() -> handlePotentiallyDeletedPrimitives(myPrimitives));
306 }
307 }
308 }
309 }
310}
Note: See TracBrowser for help on using the repository browser.