Ignore:
Timestamp:
2009-10-27T01:21:32+01:00 (15 years ago)
Author:
Gubaer
Message:

Cleanup in download logic (less global, more encapsulation)

Location:
trunk/src/org/openstreetmap/josm/actions
Files:
6 edited

Legend:

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

    r2323 r2327  
    22package org.openstreetmap.josm.actions;
    33
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    45import static org.openstreetmap.josm.tools.I18n.tr;
    5 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    66
    77import java.awt.BorderLayout;
     
    1515
    1616import org.openstreetmap.josm.Main;
    17 import org.openstreetmap.josm.actions.downloadtasks.DownloadTask;
     17import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
     18import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
    1819import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
    1920import org.openstreetmap.josm.gui.ExtendedDialog;
     
    3132 */
    3233public class DownloadAction extends JosmAction {
     34   
    3335
    3436    public DownloadDialog dialog;
     37    private ExtendedDialog downloadDialog;
    3538
    3639    public DownloadAction() {
     
    4548     */
    4649    protected ExtendedDialog createUploadDialog() {
    47         dialog = new DownloadDialog();
     50        if (dialog == null)
     51            dialog = new DownloadDialog();
     52        dialog.restoreSettings();
    4853        JPanel downPanel = new JPanel(new BorderLayout());
    4954        downPanel.add(dialog, BorderLayout.CENTER);
     
    5358                new Dimension(1000,600));
    5459
    55         ExtendedDialog dialog = new ExtendedDialog(Main.parent,
     60        if (downloadDialog == null) {
     61            downloadDialog= new ExtendedDialog(Main.parent,
    5662                tr("Download"),
    5763                new String[] {tr("OK"), tr("Cancel")});
    58         dialog.setContent(downPanel, false /* don't use a scroll pane inside the dialog */);
    59         dialog.setButtonIcons(new String[] {"ok", "cancel"});
    60         dialog.setRememberWindowGeometry(prefName, wg);
    61         return dialog;
     64            downloadDialog.setContent(downPanel, false /* don't use a scroll pane inside the dialog */);
     65            downloadDialog.setButtonIcons(new String[] {"ok", "cancel"});
     66            downloadDialog.setRememberWindowGeometry(prefName, wg);
     67        }
     68        return downloadDialog;
    6269    }
    6370
     
    6572        ExtendedDialog dlg = createUploadDialog();
    6673        boolean finish = false;
    67         while (!finish) {
     74        while (!finish) {           
    6875            dlg.showDialog();
    69             Main.pref.put("download.newlayer", dialog.newLayer.isSelected());
    7076            if (dlg.getValue() == 1 /* OK */) {
    71                 Main.pref.put("download.tab", Integer.toString(dialog.getSelectedTab()));
    72                 for (DownloadTask task : dialog.downloadTasks) {
    73                     Main.pref.put("download."+task.getPreferencesSuffix(), task.getCheckBox().isSelected());
    74                     if (task.getCheckBox().isSelected()) {
    75                         // asynchronously launch the download task ...
    76                         Future<?> future = task.download(this, dialog.minlat, dialog.minlon, dialog.maxlat, dialog.maxlon, null);
    77                         // ... and the continuation when the download task is finished
    78                         Main.worker.submit(new PostDownloadHandler(task, future));
    79                         finish = true;
    80                     }
     77                dialog.rememberSettings();
     78                if (dialog.isDownloadOsmData()) {
     79                    DownloadOsmTask task = new DownloadOsmTask();
     80                    Future<?> future = task.download(this, dialog.getSelectedDownloadArea(), null);
     81                    Main.worker.submit(new PostDownloadHandler(task, future));
     82                    finish = true;
     83                }
     84                if (dialog.isDownloadGpxData()) {
     85                    DownloadGpsTask task = new DownloadGpsTask();
     86                    Future<?> future = task.download(this,dialog.getSelectedDownloadArea(), null);
     87                    Main.worker.submit(new PostDownloadHandler(task, future));
     88                    finish = true;
    8189                }
    8290            } else {
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadGpsTask.java

    r2322 r2327  
    77import java.util.concurrent.Future;
    88
    9 import javax.swing.JCheckBox;
    10 
    119import org.openstreetmap.josm.Main;
    1210import org.openstreetmap.josm.actions.DownloadAction;
     11import org.openstreetmap.josm.data.Bounds;
    1312import org.openstreetmap.josm.data.gpx.GpxData;
    1413import org.openstreetmap.josm.gui.PleaseWaitRunnable;
     
    2221public class DownloadGpsTask extends AbstractDownloadTask {
    2322
    24     private JCheckBox checkBox = new JCheckBox(tr("Raw GPS data"));
    2523    private DownloadTask downloadTask;
    2624
    27     public Future<?> download(DownloadAction action, double minlat, double minlon,
    28             double maxlat, double maxlon, ProgressMonitor progressMonitor) {
    29         downloadTask = new DownloadTask(action.dialog.newLayer.isSelected(),
    30                 new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon), progressMonitor);
     25    public Future<?> download(DownloadAction action, Bounds downloadArea, ProgressMonitor progressMonitor) {
     26        downloadTask = new DownloadTask(action.dialog.isNewLayerRequired(),
     27                new BoundingBoxDownloader(downloadArea), progressMonitor);
    3128        // We need submit instead of execute so we can wait for it to finish and get the error
    3229        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
    3330        return Main.worker.submit(downloadTask);
    3431    }
    35 
    36     public JCheckBox getCheckBox() {
    37         return checkBox;
    38     }
    39 
    40     public String getPreferencesSuffix() {
    41         return "gps";
    42     }
    43 
     32   
    4433    public Future<?> loadUrl(boolean a,java.lang.String b,  ProgressMonitor progressMonitor) {
    4534        return null;
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java

    r2322 r2327  
    88import java.util.concurrent.Future;
    99import java.util.logging.Logger;
    10 
    11 import javax.swing.JCheckBox;
    1210
    1311import org.openstreetmap.josm.Main;
     
    3836    private DownloadTask downloadTask;
    3937
    40     private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true);
    41 
    4238    private void rememberDownloadedData(DataSet ds) {
    4339        this.downloadedData = ds;
     
    4844    }
    4945
    50     public Future<?> download(DownloadAction action, double minlat, double minlon,
    51             double maxlat, double maxlon, ProgressMonitor progressMonitor) {
    52         // Swap min and max if user has specified them the wrong way round
    53         // (easy to do if you are crossing 0, for example)
    54         // FIXME should perhaps be done in download dialog?
    55         if (minlat > maxlat) {
    56             double t = minlat; minlat = maxlat; maxlat = t;
    57         }
    58         if (minlon > maxlon) {
    59             double t = minlon; minlon = maxlon; maxlon = t;
    60         }
    61 
     46    public Future<?> download(DownloadAction action, Bounds downloadArea, ProgressMonitor progressMonitor) {
     47       
    6248        boolean newLayer = action != null
    63         && (action.dialog == null || action.dialog.newLayer.isSelected());
     49        && (action.dialog == null || action.dialog.isNewLayerRequired());
    6450
    6551        downloadTask = new DownloadTask(newLayer,
    66                 new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon), progressMonitor);
    67         currentBounds = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
     52                new BoundingBoxDownloader(downloadArea), progressMonitor);
     53        currentBounds = new Bounds(downloadArea);
    6854        // We need submit instead of execute so we can wait for it to finish and get the error
    6955        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
     
    8268        currentBounds = new Bounds(new LatLon(0,0), new LatLon(0,0));
    8369        return Main.worker.submit(downloadTask);
    84     }
    85 
    86     public JCheckBox getCheckBox() {
    87         return checkBox;
    88     }
    89 
    90     public String getPreferencesSuffix() {
    91         return "osm";
    9270    }
    9371
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTaskList.java

    r2325 r2327  
    2121import org.openstreetmap.josm.Main;
    2222import org.openstreetmap.josm.actions.UpdateSelectionAction;
     23import org.openstreetmap.josm.data.Bounds;
    2324import org.openstreetmap.josm.data.osm.DataSet;
    2425import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    6566            childProgress.setSilent(true);
    6667            childProgress.setCustomText(tr("Download {0} of {1} ({2} left)", i, rects.size(), rects.size() - i));
    67             Future<?> future = dt.download(null, td.getMinY(), td.getMinX(), td.getMaxY(), td.getMaxX(), childProgress);
     68            Future<?> future = dt.download(null, new Bounds(td), childProgress);
    6869            osmTaskFutures.add(future);
    6970            osmTasks.add(dt);
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadTask.java

    r2322 r2327  
    55import java.util.concurrent.Future;
    66
    7 import javax.swing.JCheckBox;
    8 
    97import org.openstreetmap.josm.actions.DownloadAction;
     8import org.openstreetmap.josm.data.Bounds;
    109import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1110
     
    1615     * if no error messages should be popped up.
    1716     */
    18     Future<?> download(DownloadAction action, double minlat, double minlon,
    19             double maxlat, double maxlon, ProgressMonitor progressMonitor);
     17    Future<?> download(DownloadAction action, Bounds downloadArea, ProgressMonitor progressMonitor);
    2018
    2119    /**
     
    2523     */
    2624    Future<?> loadUrl(boolean newLayer, String url, ProgressMonitor progressMonitor);
    27 
    28     /**
    29      * @return The checkbox presented to the user
    30      */
    31     JCheckBox getCheckBox();
    32 
    33     /**
    34      * @return The name of the preferences suffix to use for storing the
    35      * selection state.
    36      */
    37     String getPreferencesSuffix();
    3825
    3926    /**
  • trunk/src/org/openstreetmap/josm/actions/downloadtasks/PostDownloadHandler.java

    r2322 r2327  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.util.ArrayList;
    67import java.util.LinkedHashSet;
     8import java.util.List;
    79import java.util.concurrent.Future;
    810
     
    1618public class PostDownloadHandler implements Runnable {
    1719    private DownloadTask task;
    18     private Future<?> future;
     20    private List<Future<?>> futures;
    1921
    2022    /**
     
    2527    public PostDownloadHandler(DownloadTask task, Future<?> future) {
    2628        this.task = task;
    27         this.future = future;
     29        this.futures = new ArrayList<Future<?>>();
     30        if (future != null) {
     31            this.futures.add(future);
     32        }
    2833    }
    2934
     35    /**
     36     * constructor
     37     * @param task the asynchronous download task
     38     * @param future the future on which the completion of the download task can be synchronized
     39     */
     40    public PostDownloadHandler(DownloadTask task, Future<?> ... futures) {
     41        this.task = task;
     42        this.futures = new ArrayList<Future<?>>();
     43        if (futures == null) return;
     44        for (Future<?> future: futures) {
     45            this.futures.add(future);
     46        }
     47    }
     48   
     49
     50    /**
     51     * constructor
     52     * @param task the asynchronous download task
     53     * @param future the future on which the completion of the download task can be synchronized
     54     */
     55    public PostDownloadHandler(DownloadTask task, List<Future<?>> futures) {
     56        this.task = task;
     57        this.futures = new ArrayList<Future<?>>();
     58        if (futures == null) return;
     59        this.futures.addAll(futures);
     60    }
     61   
    3062    public void run() {
    31         // wait for the download task to complete
     63        // wait for all downloads task to finish (by waiting for the futures
     64        // to return a value)
    3265        //
    33         try {
    34             future.get();
    35         } catch(Exception e) {
    36             e.printStackTrace();
    37             return;
     66        for (Future<?> future: futures) {
     67            try {
     68                future.get();
     69            } catch(Exception e) {
     70                e.printStackTrace();
     71                return;
     72            }
    3873        }
    3974
Note: See TracChangeset for help on using the changeset viewer.