| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.util.ArrayList;
|
|---|
| 8 | import java.util.Collections;
|
|---|
| 9 | import java.util.HashSet;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 | import java.util.Set;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.AbstractAction;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 19 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
|---|
| 20 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationTask;
|
|---|
| 21 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 22 |
|
|---|
| 23 | import relcontext.ChosenRelation;
|
|---|
| 24 | import relcontext.ChosenRelationListener;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * Downloads parent relations for this relation and all parent objects for its members.
|
|---|
| 28 | *
|
|---|
| 29 | * @author Zverik
|
|---|
| 30 | */
|
|---|
| 31 | public class DownloadParentsAction extends AbstractAction implements ChosenRelationListener {
|
|---|
| 32 | private ChosenRelation rel;
|
|---|
| 33 |
|
|---|
| 34 | public DownloadParentsAction(ChosenRelation rel) {
|
|---|
| 35 | super(tr("Download referrers"));
|
|---|
| 36 | putValue(SMALL_ICON, ImageProvider.get("download"));
|
|---|
| 37 | putValue(SHORT_DESCRIPTION, tr("Download referrers for the chosen relation and its members."));
|
|---|
| 38 | this.rel = rel;
|
|---|
| 39 | rel.addChosenRelationListener(this);
|
|---|
| 40 | setEnabled(rel.get() != null && Main.main.getEditLayer() != null);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | @Override
|
|---|
| 44 | public void actionPerformed(ActionEvent e) {
|
|---|
| 45 | Relation relation = rel.get();
|
|---|
| 46 | if (relation == null ) return;
|
|---|
| 47 | List<OsmPrimitive> objects = new ArrayList<>();
|
|---|
| 48 | objects.add(relation);
|
|---|
| 49 | objects.addAll(relation.getMemberPrimitives());
|
|---|
| 50 | Main.worker.submit(new DownloadReferrersTask(Main.main.getEditLayer(), objects));
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | @Override
|
|---|
| 54 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
|---|
| 55 | setEnabled(newRelation != null && Main.main.getEditLayer() != null);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | protected void downloadMembers(Relation rel) {
|
|---|
| 59 | if (!rel.isNew()) {
|
|---|
| 60 | Main.worker.submit(new DownloadRelationTask(Collections.singletonList(rel), Main.main.getEditLayer()));
|
|---|
| 61 | }
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | protected void downloadIncomplete(Relation rel) {
|
|---|
| 65 | if (rel.isNew() ) return;
|
|---|
| 66 | Set<OsmPrimitive> ret = new HashSet<>();
|
|---|
| 67 | ret.addAll(rel.getIncompleteMembers());
|
|---|
| 68 | if (ret.isEmpty() ) return;
|
|---|
| 69 | Main.worker.submit(new DownloadRelationMemberTask(Collections.singletonList(rel), ret, Main.main.getEditLayer()));
|
|---|
| 70 | }
|
|---|
| 71 | }
|
|---|