| 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.text.MessageFormat; |
|---|
| 10 | import java.util.Collection; |
|---|
| 11 | import java.util.Map; |
|---|
| 12 | |
|---|
| 13 | import org.openstreetmap.josm.Main; |
|---|
| 14 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask; |
|---|
| 15 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
|---|
| 16 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType; |
|---|
| 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer; |
|---|
| 18 | import org.openstreetmap.josm.tools.CheckParameterUtil; |
|---|
| 19 | import org.openstreetmap.josm.tools.Shortcut; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * This action loads the set of primitives referring to the current selection from the OSM |
|---|
| 23 | * server. |
|---|
| 24 | * |
|---|
| 25 | */ |
|---|
| 26 | public class DownloadReferrersAction extends JosmAction{ |
|---|
| 27 | |
|---|
| 28 | public DownloadReferrersAction() { |
|---|
| 29 | super(tr("Download parent ways/relations..."), "downloadreferrers", tr("Download objects referring to one of the selected objects"), |
|---|
| 30 | Shortcut.registerShortcut("file:downloadreferrers", tr("File: {0}", tr("Download parent ways/relations...")), KeyEvent.VK_D, Shortcut.ALT_CTRL), true); |
|---|
| 31 | putValue("help", ht("/Action/DownloadParentWaysAndRelation")); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Downloads the primitives referring to the primitives in <code>primitives</code> |
|---|
| 36 | * into the target layer <code>targetLayer</code>. |
|---|
| 37 | * Does nothing if primitives is null or empty. |
|---|
| 38 | * |
|---|
| 39 | * @param targetLayer the target layer. Must not be null. |
|---|
| 40 | * @param children the collection of child primitives. |
|---|
| 41 | * @exception IllegalArgumentException thrown if targetLayer is null |
|---|
| 42 | */ |
|---|
| 43 | static public void downloadReferrers(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) throws IllegalArgumentException { |
|---|
| 44 | if (children == null || children.isEmpty()) return; |
|---|
| 45 | Main.worker.submit(new DownloadReferrersTask(targetLayer, children)); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /** |
|---|
| 49 | * Downloads the primitives referring to the primitives in <code>primitives</code> |
|---|
| 50 | * into the target layer <code>targetLayer</code>. |
|---|
| 51 | * Does nothing if primitives is null or empty. |
|---|
| 52 | * |
|---|
| 53 | * @param targetLayer the target layer. Must not be null. |
|---|
| 54 | * @param children the collection of primitives, given as map of ids and types |
|---|
| 55 | * @exception IllegalArgumentException thrown if targetLayer is null |
|---|
| 56 | */ |
|---|
| 57 | static public void downloadReferrers(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) throws IllegalArgumentException { |
|---|
| 58 | if (children == null || children.isEmpty()) return; |
|---|
| 59 | Main.worker.submit(new DownloadReferrersTask(targetLayer, children)); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Downloads the primitives referring to the primitive given by <code>id</code> and |
|---|
| 64 | * <code>type</code>. |
|---|
| 65 | * |
|---|
| 66 | * |
|---|
| 67 | * @param targetLayer the target layer. Must not be null. |
|---|
| 68 | * @param id the primitive id. id > 0 required. |
|---|
| 69 | * @param type the primitive type. type != null required |
|---|
| 70 | * @exception IllegalArgumentException thrown if targetLayer is null |
|---|
| 71 | * @exception IllegalArgumentException thrown if id <= 0 |
|---|
| 72 | * @exception IllegalArgumentException thrown if type == null |
|---|
| 73 | */ |
|---|
| 74 | static public void downloadReferrers(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) throws IllegalArgumentException { |
|---|
| 75 | if (id <= 0) |
|---|
| 76 | throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id)); |
|---|
| 77 | CheckParameterUtil.ensureParameterNotNull(type, "type"); |
|---|
| 78 | Main.worker.submit(new DownloadReferrersTask(targetLayer, id, type)); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | public void actionPerformed(ActionEvent e) { |
|---|
| 82 | if (!isEnabled() || ! Main.isDisplayingMapView()) |
|---|
| 83 | return; |
|---|
| 84 | OsmDataLayer layer = Main.map.mapView.getEditLayer(); |
|---|
| 85 | if (layer == null) |
|---|
| 86 | return; |
|---|
| 87 | Collection<OsmPrimitive> primitives = layer.data.getSelected(); |
|---|
| 88 | downloadReferrers(layer,primitives); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | @Override |
|---|
| 92 | protected void updateEnabledState() { |
|---|
| 93 | if (getCurrentDataSet() == null) { |
|---|
| 94 | setEnabled(false); |
|---|
| 95 | } else { |
|---|
| 96 | updateEnabledState(getCurrentDataSet().getSelected()); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | @Override |
|---|
| 101 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) { |
|---|
| 102 | setEnabled(selection != null && !selection.isEmpty()); |
|---|
| 103 | } |
|---|
| 104 | } |
|---|