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

Last change on this file since 13664 was 12620, checked in by Don-vip, 7 years ago

see #15182 - deprecate all Main logging methods and introduce suitable replacements in Logging for most of them

  • Property svn:eol-style set to native
File size: 4.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.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.Main;
20import org.openstreetmap.josm.gui.ExceptionDialogUtil;
21import org.openstreetmap.josm.gui.Notification;
22import org.openstreetmap.josm.gui.util.GuiHelper;
23import org.openstreetmap.josm.tools.ExceptionUtil;
24import org.openstreetmap.josm.tools.Logging;
25import org.openstreetmap.josm.tools.Utils;
26
27/**
28 * The post-download handler notifies user of potential errors that occurred.
29 * @since 2322
30 */
31public 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}
Note: See TracBrowser for help on using the repository browser.