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.Collection;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
013import org.openstreetmap.josm.data.osm.OsmPrimitive;
014import org.openstreetmap.josm.gui.layer.OsmDataLayer;
015import org.openstreetmap.josm.tools.Shortcut;
016
017/**
018 * This action loads the set of primitives referring to the current selection from the OSM server.
019 * @since 1810
020 */
021public class DownloadReferrersAction extends JosmAction {
022
023    /**
024     * Constructs a new {@code DownloadReferrersAction}.
025     */
026    public DownloadReferrersAction() {
027        super(tr("Download parent ways/relations..."), "download",
028                tr("Download objects referring to one of the selected objects"),
029                Shortcut.registerShortcut("file:downloadreferrers",
030                        tr("File: {0}", tr("Download parent ways/relations...")), KeyEvent.VK_D, Shortcut.ALT_CTRL),
031                true, "downloadreferrers", true);
032        putValue("help", ht("/Action/DownloadParentWaysAndRelation"));
033    }
034
035    /**
036     * Downloads the primitives referring to the primitives in <code>primitives</code>
037     * into the target layer <code>targetLayer</code>.
038     * Does nothing if primitives is null or empty.
039     *
040     * @param targetLayer the target layer. Must not be null.
041     * @param children the collection of child primitives.
042     * @throws IllegalArgumentException if targetLayer is null
043     */
044    public static void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) {
045        if (children == null || children.isEmpty())
046            return;
047        Main.worker.submit(new DownloadReferrersTask(targetLayer, children));
048    }
049
050    @Override
051    public void actionPerformed(ActionEvent e) {
052        if (!isEnabled())
053            return;
054        OsmDataLayer layer = Main.main.getEditLayer();
055        if (layer == null)
056            return;
057        Collection<OsmPrimitive> primitives = layer.data.getSelected();
058        downloadReferrers(layer, primitives);
059    }
060
061    @Override
062    protected void updateEnabledState() {
063        if (getCurrentDataSet() == null) {
064            setEnabled(false);
065        } else {
066            updateEnabledState(getCurrentDataSet().getSelected());
067        }
068    }
069
070    @Override
071    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
072        setEnabled(selection != null && !selection.isEmpty());
073    }
074}