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