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

Last change on this file since 12574 was 12574, checked in by michael2402, 7 years ago

Apply #15057: Improve the over pass turbo dialog

Adds the ability to add favorites and a new wizard dialog with examples.

  • Property svn:eol-style set to native
File size: 4.2 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.tools.ExceptionUtil;
23import org.openstreetmap.josm.tools.Utils;
24
25/**
26 * The post-download handler notifies user of potential errors that occurred.
27 * @since 2322
28 */
29public class PostDownloadHandler implements Runnable {
30 private final DownloadTask task;
31 private final Future<?> future;
32 private Consumer<Collection> errorReporter;
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<?> future) {
40 this.task = task;
41 this.future = future;
42 }
43
44 /**
45 * constructor
46 * @param task the asynchronous download task
47 * @param future the future on which the completion of the download task can be synchronized
48 * @param errorReporter a callback to inform about the number errors happened during the download
49 * task
50 */
51 public PostDownloadHandler(DownloadTask task, Future<?> future, Consumer<Collection> errorReporter) {
52 this(task, future);
53 this.errorReporter = errorReporter;
54 }
55
56 @Override
57 public void run() {
58 // wait for downloads task to finish (by waiting for the future to return a value)
59 //
60 try {
61 future.get();
62 } catch (InterruptedException | ExecutionException | CancellationException e) {
63 Main.error(e);
64 return;
65 }
66
67 // make sure errors are reported only once
68 //
69 Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects());
70 if (this.errorReporter != null) {
71 errorReporter.accept(errors);
72 }
73
74 if (errors.isEmpty()) {
75 return;
76 }
77
78 // just one error object?
79 //
80 if (errors.size() == 1) {
81 final Object error = errors.iterator().next();
82 if (!GraphicsEnvironment.isHeadless()) {
83 SwingUtilities.invokeLater(() -> {
84 if (error instanceof Exception) {
85 ExceptionDialogUtil.explainException((Exception) error);
86 } else if (tr("No data found in this area.").equals(error)) {
87 new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show();
88 } else {
89 JOptionPane.showMessageDialog(
90 Main.parent,
91 error.toString(),
92 tr("Error during download"),
93 JOptionPane.ERROR_MESSAGE);
94 }
95 });
96 }
97 return;
98 }
99
100 // multiple error object? prepare a HTML list
101 //
102 if (!errors.isEmpty()) {
103 final Collection<String> items = new ArrayList<>();
104 for (Object error : errors) {
105 if (error instanceof String) {
106 items.add((String) error);
107 } else if (error instanceof Exception) {
108 items.add(ExceptionUtil.explainException((Exception) error));
109 }
110 }
111
112 if (!GraphicsEnvironment.isHeadless()) {
113 SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(
114 Main.parent,
115 "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
116 tr("Errors during download"),
117 JOptionPane.ERROR_MESSAGE));
118 }
119 return;
120 }
121 }
122}
Note: See TracBrowser for help on using the repository browser.