| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
|---|
| 13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 14 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 15 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 17 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * This action loads the set of primitives referring to the current selection from the OSM server.
|
|---|
| 21 | * @since 1810
|
|---|
| 22 | */
|
|---|
| 23 | public class DownloadReferrersAction extends JosmAction {
|
|---|
| 24 |
|
|---|
| 25 | /**
|
|---|
| 26 | * Constructs a new {@code DownloadReferrersAction}.
|
|---|
| 27 | */
|
|---|
| 28 | public DownloadReferrersAction() {
|
|---|
| 29 | super(tr("Download parent ways/relations..."), "download",
|
|---|
| 30 | tr("Download objects referring to one of the selected objects"),
|
|---|
| 31 | Shortcut.registerShortcut("file:downloadreferrers",
|
|---|
| 32 | tr("File: {0}", tr("Download parent ways/relations...")), KeyEvent.VK_D, Shortcut.ALT_CTRL),
|
|---|
| 33 | true, "downloadreferrers", true);
|
|---|
| 34 | setHelpId(ht("/Action/DownloadParentWaysAndRelation"));
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Downloads the primitives referring to the primitives in <code>primitives</code>
|
|---|
| 39 | * into the target layer <code>targetLayer</code>.
|
|---|
| 40 | * Does nothing if primitives is null or empty.
|
|---|
| 41 | *
|
|---|
| 42 | * @param targetLayer the target layer. Must not be null.
|
|---|
| 43 | * @param children the collection of child primitives.
|
|---|
| 44 | * @throws IllegalArgumentException if targetLayer is null
|
|---|
| 45 | */
|
|---|
| 46 | public static void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) {
|
|---|
| 47 | if (Utils.isEmpty(children))
|
|---|
| 48 | return;
|
|---|
| 49 | MainApplication.worker.submit(new DownloadReferrersTask(targetLayer, children));
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | @Override
|
|---|
| 53 | public void actionPerformed(ActionEvent e) {
|
|---|
| 54 | if (!isEnabled())
|
|---|
| 55 | return;
|
|---|
| 56 | OsmDataLayer layer = getLayerManager().getEditLayer();
|
|---|
| 57 | if (layer == null)
|
|---|
| 58 | return;
|
|---|
| 59 | Collection<OsmPrimitive> primitives = layer.data.getSelected();
|
|---|
| 60 | downloadReferrers(layer, primitives);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | @Override
|
|---|
| 64 | protected void updateEnabledState() {
|
|---|
| 65 | updateEnabledStateOnCurrentSelection();
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | @Override
|
|---|
| 69 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 70 | updateEnabledStateOnModifiableSelection(selection);
|
|---|
| 71 | if (isEnabled() && !Utils.isEmpty(selection)
|
|---|
| 72 | && DownloadPolicy.BLOCKED.equals(selection.iterator().next().getDataSet().getDownloadPolicy())) {
|
|---|
| 73 | setEnabled(false);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 | }
|
|---|