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.text.MessageFormat;
010import java.util.Collection;
011import java.util.Map;
012
013import org.openstreetmap.josm.Main;
014import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
015import org.openstreetmap.josm.data.osm.OsmPrimitive;
016import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
017import org.openstreetmap.josm.gui.layer.OsmDataLayer;
018import org.openstreetmap.josm.tools.CheckParameterUtil;
019import org.openstreetmap.josm.tools.Shortcut;
020
021/**
022 * This action loads the set of primitives referring to the current selection from the OSM server.
023 * @since 1810
024 */
025public class DownloadReferrersAction extends JosmAction {
026
027    /**
028     * Constructs a new {@code DownloadReferrersAction}.
029     */
030    public DownloadReferrersAction() {
031        super(tr("Download parent ways/relations..."), "download",
032                tr("Download objects referring to one of the selected objects"),
033                Shortcut.registerShortcut("file:downloadreferrers",
034                        tr("File: {0}", tr("Download parent ways/relations...")), KeyEvent.VK_D, Shortcut.ALT_CTRL),
035                true, "downloadreferrers", true);
036        putValue("help", ht("/Action/DownloadParentWaysAndRelation"));
037    }
038
039    /**
040     * Downloads the primitives referring to the primitives in <code>primitives</code>
041     * into the target layer <code>targetLayer</code>.
042     * Does nothing if primitives is null or empty.
043     *
044     * @param targetLayer the target layer. Must not be null.
045     * @param children the collection of child primitives.
046     * @throws IllegalArgumentException if targetLayer is null
047     */
048    public static void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) {
049        if (children == null || children.isEmpty()) return;
050        Main.worker.submit(new DownloadReferrersTask(targetLayer, children));
051    }
052
053    /**
054     * Downloads the primitives referring to the primitives in <code>primitives</code>
055     * into the target layer <code>targetLayer</code>.
056     * Does nothing if primitives is null or empty.
057     *
058     * @param targetLayer the target layer. Must not be null.
059     * @param children the collection of primitives, given as map of ids and types
060     * @throws IllegalArgumentException if targetLayer is null
061     */
062    public static void downloadReferrers(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) {
063        if (children == null || children.isEmpty()) return;
064        Main.worker.submit(new DownloadReferrersTask(targetLayer, children));
065    }
066
067    /**
068     * Downloads the primitives referring to the primitive given by <code>id</code> and <code>type</code>.
069     *
070     * @param targetLayer the target layer. Must not be null.
071     * @param id the primitive id. id &gt; 0 required.
072     * @param type the primitive type. type != null required
073     * @throws IllegalArgumentException if targetLayer is null
074     * @throws IllegalArgumentException if id &lt;= 0
075     * @throws IllegalArgumentException if type == null
076     */
077    public static void downloadReferrers(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) {
078        if (id <= 0)
079            throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id));
080        CheckParameterUtil.ensureParameterNotNull(type, "type");
081        Main.worker.submit(new DownloadReferrersTask(targetLayer, id, type));
082    }
083
084    @Override
085    public void actionPerformed(ActionEvent e) {
086        if (!isEnabled())
087            return;
088        OsmDataLayer layer = Main.main.getEditLayer();
089        if (layer == null)
090            return;
091        Collection<OsmPrimitive> primitives = layer.data.getSelected();
092        downloadReferrers(layer, primitives);
093    }
094
095    @Override
096    protected void updateEnabledState() {
097        if (getCurrentDataSet() == null) {
098            setEnabled(false);
099        } else {
100            updateEnabledState(getCurrentDataSet().getSelected());
101        }
102    }
103
104    @Override
105    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
106        setEnabled(selection != null && !selection.isEmpty());
107    }
108}