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