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