001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.downloadtasks; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.GraphicsEnvironment; 007import java.util.ArrayList; 008import java.util.Collection; 009import java.util.LinkedHashSet; 010import java.util.Set; 011import java.util.concurrent.Future; 012 013import javax.swing.JOptionPane; 014import javax.swing.SwingUtilities; 015 016import org.openstreetmap.josm.Main; 017import org.openstreetmap.josm.gui.ExceptionDialogUtil; 018import org.openstreetmap.josm.gui.Notification; 019import org.openstreetmap.josm.tools.ExceptionUtil; 020import org.openstreetmap.josm.tools.Utils; 021 022public class PostDownloadHandler implements Runnable { 023 private final DownloadTask task; 024 private final Future<?> future; 025 026 /** 027 * constructor 028 * @param task the asynchronous download task 029 * @param future the future on which the completion of the download task can be synchronized 030 */ 031 public PostDownloadHandler(DownloadTask task, Future<?> future) { 032 this.task = task; 033 this.future = future; 034 } 035 036 @Override 037 public void run() { 038 // wait for downloads task to finish (by waiting for the future to return a value) 039 // 040 try { 041 future.get(); 042 } catch (Exception e) { 043 Main.error(e); 044 return; 045 } 046 047 // make sure errors are reported only once 048 // 049 Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects()); 050 if (errors.isEmpty()) 051 return; 052 053 // just one error object? 054 // 055 if (errors.size() == 1) { 056 final Object error = errors.iterator().next(); 057 if (!GraphicsEnvironment.isHeadless()) { 058 SwingUtilities.invokeLater(new Runnable() { 059 @Override 060 public void run() { 061 if (error instanceof Exception) { 062 ExceptionDialogUtil.explainException((Exception) error); 063 } else if (tr("No data found in this area.").equals(error)) { 064 new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show(); 065 } else { 066 JOptionPane.showMessageDialog( 067 Main.parent, 068 error.toString(), 069 tr("Error during download"), 070 JOptionPane.ERROR_MESSAGE); 071 } 072 } 073 }); 074 } 075 return; 076 } 077 078 // multiple error object? prepare a HTML list 079 // 080 if (!errors.isEmpty()) { 081 final Collection<String> items = new ArrayList<>(); 082 for (Object error : errors) { 083 if (error instanceof String) { 084 items.add((String) error); 085 } else if (error instanceof Exception) { 086 items.add(ExceptionUtil.explainException((Exception) error)); 087 } 088 } 089 090 if (!GraphicsEnvironment.isHeadless()) { 091 SwingUtilities.invokeLater(new Runnable() { 092 @Override 093 public void run() { 094 JOptionPane.showMessageDialog( 095 Main.parent, 096 "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>", 097 tr("Errors during download"), 098 JOptionPane.ERROR_MESSAGE); 099 } 100 }); 101 } 102 return; 103 } 104 } 105}