001 // License: GPL. For details, see LICENSE file.
002 package org.openstreetmap.josm.actions;
003
004 import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005 import static org.openstreetmap.josm.tools.I18n.tr;
006
007 import java.awt.event.ActionEvent;
008 import java.awt.event.KeyEvent;
009 import java.text.MessageFormat;
010 import java.util.Collection;
011 import java.util.Map;
012
013 import org.openstreetmap.josm.Main;
014 import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
015 import org.openstreetmap.josm.data.osm.OsmPrimitive;
016 import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
017 import org.openstreetmap.josm.gui.layer.OsmDataLayer;
018 import org.openstreetmap.josm.tools.CheckParameterUtil;
019 import 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 */
026 public 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 public void actionPerformed(ActionEvent e) {
082 if (!isEnabled() || ! Main.isDisplayingMapView())
083 return;
084 OsmDataLayer layer = Main.map.mapView.getEditLayer();
085 if (layer == null)
086 return;
087 Collection<OsmPrimitive> primitives = layer.data.getSelected();
088 downloadReferrers(layer,primitives);
089 }
090
091 @Override
092 protected void updateEnabledState() {
093 if (getCurrentDataSet() == null) {
094 setEnabled(false);
095 } else {
096 updateEnabledState(getCurrentDataSet().getSelected());
097 }
098 }
099
100 @Override
101 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
102 setEnabled(selection != null && !selection.isEmpty());
103 }
104 }