[2512] | 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 |
|
---|
[9905] | 6 | import java.awt.GraphicsEnvironment;
|
---|
[2512] | 7 | import java.util.ArrayList;
|
---|
[6524] | 8 | import java.util.Collection;
|
---|
[2512] | 9 | import java.util.LinkedHashSet;
|
---|
[8338] | 10 | import java.util.Set;
|
---|
[10212] | 11 | import java.util.concurrent.CancellationException;
|
---|
| 12 | import java.util.concurrent.ExecutionException;
|
---|
[2512] | 13 | import java.util.concurrent.Future;
|
---|
[12574] | 14 | import java.util.function.Consumer;
|
---|
[2512] | 15 |
|
---|
| 16 | import javax.swing.JOptionPane;
|
---|
[5149] | 17 | import javax.swing.SwingUtilities;
|
---|
[2512] | 18 |
|
---|
| 19 | import org.openstreetmap.josm.Main;
|
---|
| 20 | import org.openstreetmap.josm.gui.ExceptionDialogUtil;
|
---|
[8472] | 21 | import org.openstreetmap.josm.gui.Notification;
|
---|
[2512] | 22 | import org.openstreetmap.josm.tools.ExceptionUtil;
|
---|
[6524] | 23 | import org.openstreetmap.josm.tools.Utils;
|
---|
[2512] | 24 |
|
---|
[12284] | 25 | /**
|
---|
[12307] | 26 | * The post-download handler notifies user of potential errors that occurred.
|
---|
[12284] | 27 | * @since 2322
|
---|
| 28 | */
|
---|
[2512] | 29 | public class PostDownloadHandler implements Runnable {
|
---|
[9067] | 30 | private final DownloadTask task;
|
---|
[9905] | 31 | private final Future<?> future;
|
---|
[12574] | 32 | private Consumer<Collection> errorReporter;
|
---|
[2512] | 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;
|
---|
[9905] | 41 | this.future = future;
|
---|
[2512] | 42 | }
|
---|
| 43 |
|
---|
[12574] | 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 |
|
---|
[6084] | 56 | @Override
|
---|
[2512] | 57 | public void run() {
|
---|
[9905] | 58 | // wait for downloads task to finish (by waiting for the future to return a value)
|
---|
[2512] | 59 | //
|
---|
[9905] | 60 | try {
|
---|
| 61 | future.get();
|
---|
[10212] | 62 | } catch (InterruptedException | ExecutionException | CancellationException e) {
|
---|
[9905] | 63 | Main.error(e);
|
---|
| 64 | return;
|
---|
[2512] | 65 | }
|
---|
| 66 |
|
---|
| 67 | // make sure errors are reported only once
|
---|
| 68 | //
|
---|
[9905] | 69 | Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects());
|
---|
[12574] | 70 | if (this.errorReporter != null) {
|
---|
| 71 | errorReporter.accept(errors);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | if (errors.isEmpty()) {
|
---|
[2512] | 75 | return;
|
---|
[12574] | 76 | }
|
---|
[2512] | 77 |
|
---|
| 78 | // just one error object?
|
---|
| 79 | //
|
---|
| 80 | if (errors.size() == 1) {
|
---|
[5149] | 81 | final Object error = errors.iterator().next();
|
---|
[9905] | 82 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
[10601] | 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);
|
---|
[5149] | 94 | }
|
---|
[9905] | 95 | });
|
---|
| 96 | }
|
---|
[2512] | 97 | return;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | // multiple error object? prepare a HTML list
|
---|
| 101 | //
|
---|
| 102 | if (!errors.isEmpty()) {
|
---|
[7005] | 103 | final Collection<String> items = new ArrayList<>();
|
---|
[9905] | 104 | for (Object error : errors) {
|
---|
[2512] | 105 | if (error instanceof String) {
|
---|
[6524] | 106 | items.add((String) error);
|
---|
[2512] | 107 | } else if (error instanceof Exception) {
|
---|
[8510] | 108 | items.add(ExceptionUtil.explainException((Exception) error));
|
---|
[2512] | 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[9905] | 112 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
[10601] | 113 | SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(
|
---|
| 114 | Main.parent,
|
---|
| 115 | "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
|
---|
| 116 | tr("Errors during download"),
|
---|
| 117 | JOptionPane.ERROR_MESSAGE));
|
---|
[9905] | 118 | }
|
---|
[2512] | 119 | return;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 | }
|
---|