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.DownloadNotesTask;
014import org.openstreetmap.josm.actions.downloadtasks.DownloadOsmTask;
015import org.openstreetmap.josm.actions.downloadtasks.PostDownloadHandler;
016import org.openstreetmap.josm.data.Bounds;
017import org.openstreetmap.josm.gui.download.DownloadDialog;
018import org.openstreetmap.josm.tools.Shortcut;
019
020/**
021 * Action that opens a connection to the osm server and downloads map data.
022 *
023 * An dialog is displayed asking the user to specify a rectangle to grab.
024 * The url and account settings from the preferences are used.
025 *
026 * @author imi
027 */
028public class DownloadAction extends JosmAction {
029
030    /**
031     * Constructs a new {@code DownloadAction}.
032     */
033    public DownloadAction() {
034        super(tr("Download from OSM..."), "download", tr("Download map data from the OSM server."),
035                Shortcut.registerShortcut("file:download", tr("File: {0}", tr("Download from OSM...")), KeyEvent.VK_DOWN, Shortcut.CTRL_SHIFT), true);
036        putValue("help", ht("/Action/Download"));
037    }
038
039    @Override
040    public void actionPerformed(ActionEvent e) {
041        DownloadDialog dialog = DownloadDialog.getInstance();
042        dialog.restoreSettings();
043        dialog.setVisible(true);
044        if (! dialog.isCanceled()) {
045            dialog.rememberSettings();
046            Bounds area = dialog.getSelectedDownloadArea();
047            if (dialog.isDownloadOsmData()) {
048                DownloadOsmTask task = new DownloadOsmTask();
049                Future<?> future = task.download(dialog.isNewLayerRequired(), area, null);
050                Main.worker.submit(new PostDownloadHandler(task, future));
051            }
052            if (dialog.isDownloadGpxData()) {
053                DownloadGpsTask task = new DownloadGpsTask();
054                Future<?> future = task.download(dialog.isNewLayerRequired(),area, null);
055                Main.worker.submit(new PostDownloadHandler(task, future));
056            }
057
058            //TODO: This eventually needs to be a checkbox in the UI
059            //For now I'm adding it as a hidden feature since this is still a work in progress
060            if (Main.pref.getBoolean("osm.notes.enableDownload", false)) {
061                DownloadNotesTask task = new DownloadNotesTask();
062                Future<?> future = task.download(false, area, null);
063                Main.worker.submit(new PostDownloadHandler(task, future));
064            }
065        }
066    }
067}