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.HashSet;
|
---|
10 | import java.util.LinkedHashSet;
|
---|
11 | import java.util.Set;
|
---|
12 | import java.util.concurrent.CancellationException;
|
---|
13 | import java.util.concurrent.ExecutionException;
|
---|
14 | import java.util.concurrent.Future;
|
---|
15 | import java.util.function.Consumer;
|
---|
16 |
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.SwingUtilities;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.Notification;
|
---|
23 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
24 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
25 | import org.openstreetmap.josm.tools.Logging;
|
---|
26 | import org.openstreetmap.josm.tools.Utils;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * The post-download handler notifies user of potential errors that occurred.
|
---|
30 | * @since 2322
|
---|
31 | */
|
---|
32 | public class PostDownloadHandler implements Runnable {
|
---|
33 | private final DownloadTask task;
|
---|
34 | private final Future<?> future;
|
---|
35 | private Consumer<Collection<Object>> errorReporter;
|
---|
36 |
|
---|
37 | private static final Set<String> NO_DATA_ERROR_MESSAGES = new HashSet<>();
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Creates a new {@link PostDownloadHandler}
|
---|
41 | * @param task the asynchronous download task
|
---|
42 | * @param future the future on which the completion of the download task can be synchronized
|
---|
43 | */
|
---|
44 | public PostDownloadHandler(DownloadTask task, Future<?> future) {
|
---|
45 | this.task = task;
|
---|
46 | this.future = future;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Creates a new {@link PostDownloadHandler} using a custom error reporter
|
---|
51 | * @param task the asynchronous download task
|
---|
52 | * @param future the future on which the completion of the download task can be synchronized
|
---|
53 | * @param errorReporter a callback to inform about the number errors happened during the download
|
---|
54 | * task
|
---|
55 | */
|
---|
56 | public PostDownloadHandler(DownloadTask task, Future<?> future, Consumer<Collection<Object>> errorReporter) {
|
---|
57 | this(task, future);
|
---|
58 | this.errorReporter = errorReporter;
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Adds a new translated error message indicating that no data has been downloaded.
|
---|
63 | * @param message new translated error message indicating that no data has been downloaded.
|
---|
64 | * @return {@code true} if the message was not already known
|
---|
65 | * @since 15358
|
---|
66 | */
|
---|
67 | public static boolean addNoDataErrorMessage(String message) {
|
---|
68 | return NO_DATA_ERROR_MESSAGES.add(message);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Determines if a translated error message indicates that no data has been downloaded.
|
---|
73 | * @param message translated error message to check
|
---|
74 | * @return {@code true} if the message indicates that no data has been downloaded
|
---|
75 | * @since 15358
|
---|
76 | */
|
---|
77 | public static boolean isNoDataErrorMessage(Object message) {
|
---|
78 | return NO_DATA_ERROR_MESSAGES.contains(message);
|
---|
79 | }
|
---|
80 |
|
---|
81 | @Override
|
---|
82 | public void run() {
|
---|
83 | // wait for downloads task to finish (by waiting for the future to return a value)
|
---|
84 | //
|
---|
85 | try {
|
---|
86 | future.get();
|
---|
87 | } catch (InterruptedException | ExecutionException | CancellationException e) {
|
---|
88 | Logging.error(e);
|
---|
89 | return;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // make sure errors are reported only once
|
---|
93 | //
|
---|
94 | Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects());
|
---|
95 |
|
---|
96 | if (this.errorReporter != null) {
|
---|
97 | GuiHelper.runInEDT(() -> errorReporter.accept(errors));
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (errors.isEmpty()) {
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | // just one error object?
|
---|
105 | //
|
---|
106 | if (errors.size() == 1) {
|
---|
107 | final Object error = errors.iterator().next();
|
---|
108 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
109 | SwingUtilities.invokeLater(() -> {
|
---|
110 | if (error instanceof Exception) {
|
---|
111 | ExceptionDialogUtil.explainException((Exception) error);
|
---|
112 | } else if (isNoDataErrorMessage(error)) {
|
---|
113 | new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show();
|
---|
114 | } else {
|
---|
115 | JOptionPane.showMessageDialog(
|
---|
116 | MainApplication.getMainFrame(),
|
---|
117 | error.toString(),
|
---|
118 | tr("Error during download"),
|
---|
119 | JOptionPane.ERROR_MESSAGE);
|
---|
120 | }
|
---|
121 | });
|
---|
122 | }
|
---|
123 | return;
|
---|
124 | }
|
---|
125 |
|
---|
126 | // multiple error object? prepare a HTML list
|
---|
127 | //
|
---|
128 | if (!errors.isEmpty()) {
|
---|
129 | final Collection<String> items = new ArrayList<>();
|
---|
130 | for (Object error : errors) {
|
---|
131 | if (error instanceof String) {
|
---|
132 | items.add((String) error);
|
---|
133 | } else if (error instanceof Exception) {
|
---|
134 | items.add(ExceptionUtil.explainException((Exception) error));
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
139 | SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(
|
---|
140 | MainApplication.getMainFrame(),
|
---|
141 | "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
|
---|
142 | tr("Errors during download"),
|
---|
143 | JOptionPane.ERROR_MESSAGE));
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | }
|
---|