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