source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/PostDownloadHandler.java@ 11186

Last change on this file since 11186 was 10601, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GraphicsEnvironment;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.LinkedHashSet;
10import java.util.Set;
11import java.util.concurrent.CancellationException;
12import java.util.concurrent.ExecutionException;
13import java.util.concurrent.Future;
14
15import javax.swing.JOptionPane;
16import javax.swing.SwingUtilities;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.Notification;
21import org.openstreetmap.josm.tools.ExceptionUtil;
22import org.openstreetmap.josm.tools.Utils;
23
24public class PostDownloadHandler implements Runnable {
25 private final DownloadTask task;
26 private final Future<?> future;
27
28 /**
29 * constructor
30 * @param task the asynchronous download task
31 * @param future the future on which the completion of the download task can be synchronized
32 */
33 public PostDownloadHandler(DownloadTask task, Future<?> future) {
34 this.task = task;
35 this.future = future;
36 }
37
38 @Override
39 public void run() {
40 // wait for downloads task to finish (by waiting for the future to return a value)
41 //
42 try {
43 future.get();
44 } catch (InterruptedException | ExecutionException | CancellationException e) {
45 Main.error(e);
46 return;
47 }
48
49 // make sure errors are reported only once
50 //
51 Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects());
52 if (errors.isEmpty())
53 return;
54
55 // just one error object?
56 //
57 if (errors.size() == 1) {
58 final Object error = errors.iterator().next();
59 if (!GraphicsEnvironment.isHeadless()) {
60 SwingUtilities.invokeLater(() -> {
61 if (error instanceof Exception) {
62 ExceptionDialogUtil.explainException((Exception) error);
63 } else if (tr("No data found in this area.").equals(error)) {
64 new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show();
65 } else {
66 JOptionPane.showMessageDialog(
67 Main.parent,
68 error.toString(),
69 tr("Error during download"),
70 JOptionPane.ERROR_MESSAGE);
71 }
72 });
73 }
74 return;
75 }
76
77 // multiple error object? prepare a HTML list
78 //
79 if (!errors.isEmpty()) {
80 final Collection<String> items = new ArrayList<>();
81 for (Object error : errors) {
82 if (error instanceof String) {
83 items.add((String) error);
84 } else if (error instanceof Exception) {
85 items.add(ExceptionUtil.explainException((Exception) error));
86 }
87 }
88
89 if (!GraphicsEnvironment.isHeadless()) {
90 SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(
91 Main.parent,
92 "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
93 tr("Errors during download"),
94 JOptionPane.ERROR_MESSAGE));
95 }
96 return;
97 }
98 }
99}
Note: See TracBrowser for help on using the repository browser.