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.actions.downloadtasks.DownloadReferrersTask;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
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 && MainApplication.getLayerManager().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 | MainApplication.worker.submit(
|
---|
51 | new DownloadReferrersTask(MainApplication.getLayerManager().getEditLayer(), objects));
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void chosenRelationChanged(Relation oldRelation, Relation newRelation) {
|
---|
56 | setEnabled(newRelation != null && MainApplication.getLayerManager().getEditLayer() != null);
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void downloadMembers(Relation rel) {
|
---|
60 | if (!rel.isNew()) {
|
---|
61 | MainApplication.worker.submit(
|
---|
62 | new DownloadRelationTask(Collections.singletonList(rel), MainApplication.getLayerManager().getEditLayer()));
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | protected void downloadIncomplete(Relation rel) {
|
---|
67 | if (rel.isNew()) return;
|
---|
68 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
69 | ret.addAll(rel.getIncompleteMembers());
|
---|
70 | if (ret.isEmpty()) return;
|
---|
71 | MainApplication.worker.submit(
|
---|
72 | new DownloadRelationMemberTask(Collections.singletonList(rel), ret, MainApplication.getLayerManager().getEditLayer()));
|
---|
73 | }
|
---|
74 | }
|
---|