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