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     * @exception IllegalArgumentException thrown if targetLayer is null
047     */
048    public static void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) throws IllegalArgumentException {
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     * @exception IllegalArgumentException thrown if targetLayer is null
061     */
062    public static void downloadReferrers(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) throws IllegalArgumentException {
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
069     * <code>type</code>.
070     *
071     * @param targetLayer  the target layer. Must not be null.
072     * @param id the primitive id. id &gt; 0 required.
073     * @param type the primitive type. type != null required
074     * @exception IllegalArgumentException thrown if targetLayer is null
075     * @exception IllegalArgumentException thrown if id &lt;= 0
076     * @exception IllegalArgumentException thrown if type == null
077     */
078    public static void downloadReferrers(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) throws IllegalArgumentException {
079        if (id <= 0)
080            throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id));
081        CheckParameterUtil.ensureParameterNotNull(type, "type");
082        Main.worker.submit(new DownloadReferrersTask(targetLayer, id, type));
083    }
084
085    @Override
086    public void actionPerformed(ActionEvent e) {
087        if (!isEnabled())
088            return;
089        OsmDataLayer layer = Main.main.getEditLayer();
090        if (layer == null)
091            return;
092        Collection<OsmPrimitive> primitives = layer.data.getSelected();
093        downloadReferrers(layer,primitives);
094    }
095
096    @Override
097    protected void updateEnabledState() {
098        if (getCurrentDataSet() == null) {
099            setEnabled(false);
100        } else {
101            updateEnabledState(getCurrentDataSet().getSelected());
102        }
103    }
104
105    @Override
106    protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
107        setEnabled(selection != null && !selection.isEmpty());
108    }
109}