001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009import java.util.concurrent.Future;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.actions.downloadtasks.DownloadGpsTask;
013import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
014import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
015import org.openstreetmap.josm.data.Bounds;
016import org.openstreetmap.josm.gui.download.DownloadDialog;
017import org.openstreetmap.josm.tools.Shortcut;
018
019/**
020 * Action that opens a connection to the osm server and downloads map data.
021 *
022 * An dialog is displayed asking the user to specify a rectangle to grab.
023 * The url and account settings from the preferences are used.
024 *
025 * @author imi
026 */
027public class DownloadAction extends JosmAction {
028    
029    /**
030     * Constructs a new {@code DownloadAction}.
031     */
032    public DownloadAction() {
033        super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."),
034                Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download from OSM...")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT), true);
035        putValue("help", ht("/Action/Download"));
036    }
037
038    @Override
039    public void actionPerformed(ActionEvent e) {
040        DownloadDialog dialog = DownloadDialog.getInstance();
041        dialog.restoreSettings();
042        dialog.setVisible(true);
043        if (! dialog.isCanceled()) {
044            dialog.rememberSettings();
045            Bounds area = dialog.getSelectedDownloadArea();
046            if (dialog.isDownloadOsmData()) {
047                DownloadOsmTask task = new DownloadOsmTask();
048                Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
049                Main.worker.submit(new PostDownloadHandler(task, future));
050            }
051            if (dialog.isDownloadGpxData()) {
052                DownloadGpsTask task = new DownloadGpsTask();
053                Future<?> future = task.download(dialog.isNewLayerRequired(),area, null);
054                Main.worker.submit(new PostDownloadHandler(task, future));
055            }
056        }
057    }
058}