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.util.ArrayList;
|
---|
7 | import java.util.LinkedHashSet;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.concurrent.Future;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
15 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
16 |
|
---|
17 | public class PostDownloadHandler implements Runnable {
|
---|
18 | private DownloadTask task;
|
---|
19 | private List<Future<?>> futures;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * constructor
|
---|
23 | * @param task the asynchronous download task
|
---|
24 | * @param future the future on which the completion of the download task can be synchronized
|
---|
25 | */
|
---|
26 | public PostDownloadHandler(DownloadTask task, Future<?> future) {
|
---|
27 | this.task = task;
|
---|
28 | this.futures = new ArrayList<Future<?>>();
|
---|
29 | if (future != null) {
|
---|
30 | this.futures.add(future);
|
---|
31 | }
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * constructor
|
---|
36 | * @param task the asynchronous download task
|
---|
37 | * @param future the future on which the completion of the download task can be synchronized
|
---|
38 | */
|
---|
39 | public PostDownloadHandler(DownloadTask task, Future<?> ... futures) {
|
---|
40 | this.task = task;
|
---|
41 | this.futures = new ArrayList<Future<?>>();
|
---|
42 | if (futures == null) return;
|
---|
43 | for (Future<?> future: futures) {
|
---|
44 | this.futures.add(future);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * constructor
|
---|
50 | * @param task the asynchronous download task
|
---|
51 | * @param future the future on which the completion of the download task can be synchronized
|
---|
52 | */
|
---|
53 | public PostDownloadHandler(DownloadTask task, List<Future<?>> futures) {
|
---|
54 | this.task = task;
|
---|
55 | this.futures = new ArrayList<Future<?>>();
|
---|
56 | if (futures == null) return;
|
---|
57 | this.futures.addAll(futures);
|
---|
58 | }
|
---|
59 |
|
---|
60 | public void run() {
|
---|
61 | // wait for all downloads task to finish (by waiting for the futures
|
---|
62 | // to return a value)
|
---|
63 | //
|
---|
64 | for (Future<?> future: futures) {
|
---|
65 | try {
|
---|
66 | future.get();
|
---|
67 | } catch(Exception e) {
|
---|
68 | e.printStackTrace();
|
---|
69 | return;
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | // make sure errors are reported only once
|
---|
74 | //
|
---|
75 | LinkedHashSet<Object> errors = new LinkedHashSet<Object>();
|
---|
76 | errors.addAll(task.getErrorObjects());
|
---|
77 | if (errors.isEmpty())
|
---|
78 | return;
|
---|
79 |
|
---|
80 | // just one error object?
|
---|
81 | //
|
---|
82 | if (errors.size() == 1) {
|
---|
83 | Object error = errors.iterator().next();
|
---|
84 | if (error instanceof Exception) {
|
---|
85 | ExceptionDialogUtil.explainException((Exception)error);
|
---|
86 | return;
|
---|
87 | }
|
---|
88 | JOptionPane.showMessageDialog(
|
---|
89 | Main.parent,
|
---|
90 | error.toString(),
|
---|
91 | tr("Errors during Download"),
|
---|
92 | JOptionPane.ERROR_MESSAGE);
|
---|
93 | return;
|
---|
94 |
|
---|
95 | }
|
---|
96 |
|
---|
97 | // multiple error object? prepare a HTML list
|
---|
98 | //
|
---|
99 | if (!errors.isEmpty()) {
|
---|
100 | StringBuffer sb = new StringBuffer();
|
---|
101 | for (Object error:errors) {
|
---|
102 | if (error instanceof String) {
|
---|
103 | sb.append("<li>").append(error).append("</li>").append("<br>");
|
---|
104 | } else if (error instanceof Exception) {
|
---|
105 | sb.append("<li>").append(ExceptionUtil.explainException((Exception)error)).append("</li>").append("<br>");
|
---|
106 | }
|
---|
107 | }
|
---|
108 | sb.insert(0, "<html><ul>");
|
---|
109 | sb.append("</ul></html>");
|
---|
110 |
|
---|
111 | JOptionPane.showMessageDialog(
|
---|
112 | Main.parent,
|
---|
113 | sb.toString(),
|
---|
114 | tr("Errors during download"),
|
---|
115 | JOptionPane.ERROR_MESSAGE);
|
---|
116 | return;
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|