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

Last change on this file since 15358 was 15358, checked in by Don-vip, 5 years ago

fix #18105 - Show a notification if no notes can be downloaded

  • Property svn:eol-style set to native
File size: 5.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.HashSet;
10import java.util.LinkedHashSet;
11import java.util.Set;
12import java.util.concurrent.CancellationException;
13import java.util.concurrent.ExecutionException;
14import java.util.concurrent.Future;
15import java.util.function.Consumer;
16
17import javax.swing.JOptionPane;
18import javax.swing.SwingUtilities;
19
20import org.openstreetmap.josm.gui.ExceptionDialogUtil;
21import org.openstreetmap.josm.gui.MainApplication;
22import org.openstreetmap.josm.gui.Notification;
23import org.openstreetmap.josm.gui.util.GuiHelper;
24import org.openstreetmap.josm.tools.ExceptionUtil;
25import org.openstreetmap.josm.tools.Logging;
26import org.openstreetmap.josm.tools.Utils;
27
28/**
29 * The post-download handler notifies user of potential errors that occurred.
30 * @since 2322
31 */
32public 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}
Note: See TracBrowser for help on using the repository browser.