Changeset 9905 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2016-03-01T20:36:36+01:00 (8 years ago)
Author:
Don-vip
Message:

PostDownloadHandler: remove unused code, add unit tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/PostDownloadHandler.java

    r9067 r9905  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.GraphicsEnvironment;
    67import java.util.ArrayList;
    78import java.util.Collection;
    89import java.util.LinkedHashSet;
    9 import java.util.List;
    1010import java.util.Set;
    1111import java.util.concurrent.Future;
     
    2222public class PostDownloadHandler implements Runnable {
    2323    private final DownloadTask task;
    24     private final List<Future<?>> futures;
     24    private final Future<?> future;
    2525
    2626    /**
     
    3131    public PostDownloadHandler(DownloadTask task, Future<?> future) {
    3232        this.task = task;
    33         this.futures = new ArrayList<>();
    34         if (future != null) {
    35             this.futures.add(future);
    36         }
    37     }
    38 
    39     /**
    40      * constructor
    41      * @param task the asynchronous download task
    42      * @param futures the futures on which the completion of the download task can be synchronized
    43      */
    44     public PostDownloadHandler(DownloadTask task, Future<?> ... futures) {
    45         this.task = task;
    46         this.futures = new ArrayList<>();
    47         if (futures == null) return;
    48         for (Future<?> future: futures) {
    49             this.futures.add(future);
    50         }
    51     }
    52 
    53     /**
    54      * constructor
    55      * @param task the asynchronous download task
    56      * @param futures the futures on which the completion of the download task can be synchronized
    57      */
    58     public PostDownloadHandler(DownloadTask task, List<Future<?>> futures) {
    59         this.task = task;
    60         this.futures = new ArrayList<>();
    61         if (futures == null) return;
    62         this.futures.addAll(futures);
     33        this.future = future;
    6334    }
    6435
    6536    @Override
    6637    public void run() {
    67         // wait for all downloads task to finish (by waiting for the futures to return a value)
     38        // wait for downloads task to finish (by waiting for the future to return a value)
    6839        //
    69         for (Future<?> future: futures) {
    70             try {
    71                 future.get();
    72             } catch (Exception e) {
    73                 Main.error(e);
    74                 return;
    75             }
     40        try {
     41            future.get();
     42        } catch (Exception e) {
     43            Main.error(e);
     44            return;
    7645        }
    7746
    7847        // make sure errors are reported only once
    7948        //
    80         Set<Object> errors = new LinkedHashSet<>();
    81         errors.addAll(task.getErrorObjects());
     49        Set<Object> errors = new LinkedHashSet<>(task.getErrorObjects());
    8250        if (errors.isEmpty())
    8351            return;
     
    8755        if (errors.size() == 1) {
    8856            final Object error = errors.iterator().next();
    89             SwingUtilities.invokeLater(new Runnable() {
    90                 @Override
    91                 public void run() {
    92                     if (error instanceof Exception) {
    93                         ExceptionDialogUtil.explainException((Exception) error);
    94                     } else if (tr("No data found in this area.").equals(error)) {
    95                         new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show();
    96                     } else {
    97                         JOptionPane.showMessageDialog(
    98                                 Main.parent,
    99                                 error.toString(),
    100                                 tr("Error during download"),
    101                                 JOptionPane.ERROR_MESSAGE);
     57            if (!GraphicsEnvironment.isHeadless()) {
     58                SwingUtilities.invokeLater(new Runnable() {
     59                    @Override
     60                    public void run() {
     61                        if (error instanceof Exception) {
     62                            ExceptionDialogUtil.explainException((Exception) error);
     63                        } else if (tr("No data found in this area.").equals(error)) {
     64                            new Notification(error.toString()).setIcon(JOptionPane.WARNING_MESSAGE).show();
     65                        } else {
     66                            JOptionPane.showMessageDialog(
     67                                    Main.parent,
     68                                    error.toString(),
     69                                    tr("Error during download"),
     70                                    JOptionPane.ERROR_MESSAGE);
     71                        }
    10272                    }
    103                 }
    104             });
     73                });
     74            }
    10575            return;
    10676        }
     
    11080        if (!errors.isEmpty()) {
    11181            final Collection<String> items = new ArrayList<>();
    112             for (Object error:errors) {
     82            for (Object error : errors) {
    11383                if (error instanceof String) {
    11484                    items.add((String) error);
     
    11888            }
    11989
    120             SwingUtilities.invokeLater(new Runnable() {
    121                 @Override
    122                 public void run() {
    123                     JOptionPane.showMessageDialog(
    124                             Main.parent,
    125                             "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
    126                             tr("Errors during download"),
    127                             JOptionPane.ERROR_MESSAGE);
    128                 }
    129             });
     90            if (!GraphicsEnvironment.isHeadless()) {
     91                SwingUtilities.invokeLater(new Runnable() {
     92                    @Override
     93                    public void run() {
     94                        JOptionPane.showMessageDialog(
     95                                Main.parent,
     96                                "<html>"+Utils.joinAsHtmlUnorderedList(items)+"</html>",
     97                                tr("Errors during download"),
     98                                JOptionPane.ERROR_MESSAGE);
     99                    }
     100                });
     101            }
    130102            return;
    131103        }
Note: See TracChangeset for help on using the changeset viewer.