001// License: GPL. See LICENSE file for details.
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.Main;
012import org.openstreetmap.josm.data.osm.PrimitiveId;
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     * Constructs a new {@code DownloadPrimitiveAction}.
027     */
028    public DownloadPrimitiveAction() {
029        super(tr("Download object..."), "downloadprimitive", tr("Download OSM object by ID."),
030                Shortcut.registerShortcut("system:download_primitive", tr("File: {0}", tr("Download object...")), KeyEvent.VK_O, Shortcut.CTRL_SHIFT), true);
031        putValue("help", ht("/Action/DownloadObject"));
032    }
033
034    @Override
035    public void actionPerformed(ActionEvent e) {
036
037        DownloadObjectDialog dialog = new DownloadObjectDialog();
038        if (dialog.showDialog().getValue() != dialog.getContinueButtonIndex()) return;
039
040        processItems(dialog.isNewLayerRequested(), dialog.getOsmIds(), dialog.isReferrersRequested(), dialog.isFullRelationRequested());
041    }
042
043    /**
044     * @param newLayer if the data should be downloaded into a new layer
045     * @param ids List of primitive id to download
046     * @param downloadReferrers if the referrers of the object should be downloaded as well, i.e., parent relations, and for nodes, additionally, parent ways
047     * @param full if the members of a relation should be downloaded as well
048     */
049    public static void processItems(boolean newLayer, final List<PrimitiveId> ids, boolean downloadReferrers, boolean full) {
050        final DownloadPrimitivesWithReferrersTask task =
051                new DownloadPrimitivesWithReferrersTask(newLayer, ids, downloadReferrers, full, null);
052        Main.worker.submit(task);
053        Main.worker.submit(new Runnable() {
054            @Override
055            public void run() {
056                final List<PrimitiveId> downloaded = task.getDownloadedId();
057                if(downloaded != null) {
058                    GuiHelper.runInEDT(new Runnable() {
059                        @Override
060                        public void run() {
061                            Main.main.getCurrentDataSet().setSelected(downloaded);
062                        }
063                    });
064                }
065            }
066        });
067    }
068}