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