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

Last change on this file since 12307 was 12307, checked in by stoecker, 7 years ago

typo

  • Property svn:eol-style set to native
File size: 3.5 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;
14
15import javax.swing.JOptionPane;
16import javax.swing.SwingUtilities;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.gui.ExceptionDialogUtil;
20import org.openstreetmap.josm.gui.Notification;
21import org.openstreetmap.josm.tools.ExceptionUtil;
22import org.openstreetmap.josm.tools.Utils;
23
24/**
25 * The post-download handler notifies user of potential errors that occurred.
26 * @since 2322
27 */
28public 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}
Note: See TracBrowser for help on using the repository browser.