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.List;
010
011import org.openstreetmap.josm.data.osm.PrimitiveId;
012import org.openstreetmap.josm.gui.MainApplication;
013import org.openstreetmap.josm.gui.download.DownloadObjectDialog;
014import org.openstreetmap.josm.gui.io.DownloadPrimitivesWithReferrersTask;
015import org.openstreetmap.josm.gui.util.GuiHelper;
016import org.openstreetmap.josm.tools.Shortcut;
017
018/**
019 * Download an OsmPrimitive by specifying type and ID
020 *
021 * @author Matthias Julius
022 */
023public class DownloadPrimitiveAction extends JosmAction {
024
025    /**
026     * Action shortcut (ctrl-shift-O by default), made public in order to be used from {@code GettingStarted} page.
027     */
028    public static final Shortcut SHORTCUT = Shortcut.registerShortcut("system:download_primitive", tr("File: {0}", tr("Download object...")),
029            KeyEvent.VK_O, Shortcut.CTRL_SHIFT);
030
031    /**
032     * Constructs a new {@code DownloadPrimitiveAction}.
033     */
034    public DownloadPrimitiveAction() {
035        super(tr("Download object..."), "downloadprimitive", tr("Download OSM object by ID"),
036                SHORTCUT, true);
037        setHelpId(ht("/Action/DownloadObject"));
038    }
039
040    @Override
041    public void actionPerformed(ActionEvent e) {
042        DownloadObjectDialog dialog = new DownloadObjectDialog();
043        if (dialog.showDialog().getValue() != dialog.getContinueButtonIndex()) return;
044
045        processItems(dialog.isNewLayerRequested(), dialog.getOsmIds(), dialog.isReferrersRequested(), dialog.isFullRelationRequested());
046    }
047
048    /**
049     * Submits the download task for the given primitive ids.
050     * @param newLayer if the data should be downloaded into a new layer
051     * @param ids List of primitive id to download
052     * @param downloadReferrers if the referrers of the object should be downloaded as well, i.e., parent relations, and for nodes,
053     * additionally, parent ways
054     * @param full if the members of a relation should be downloaded as well
055     */
056    public static void processItems(boolean newLayer, final List<PrimitiveId> ids, boolean downloadReferrers, boolean full) {
057        final DownloadPrimitivesWithReferrersTask task =
058                new DownloadPrimitivesWithReferrersTask(newLayer, ids, downloadReferrers, full, null, null);
059        MainApplication.worker.submit(task);
060        MainApplication.worker.submit(() -> {
061                final List<PrimitiveId> downloaded = task.getDownloadedId();
062                if (downloaded != null) {
063                    GuiHelper.runInEDT(() -> MainApplication.getLayerManager().getEditDataSet().setSelected(downloaded));
064                }
065        });
066    }
067}