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