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

Last change on this file was 17332, checked in by Don-vip, 3 years ago

fix #20131 - fix unit tests and codestyle violations

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