1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.relation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.HashSet;
|
---|
9 | import java.util.Set;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
13 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
14 | import org.openstreetmap.josm.gui.dialogs.relation.DownloadRelationMemberTask;
|
---|
15 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
16 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Action for downloading incomplete members of selected relations
|
---|
20 | * @since 5793
|
---|
21 | */
|
---|
22 | public class DownloadSelectedIncompleteMembersAction extends AbstractRelationAction {
|
---|
23 |
|
---|
24 | private transient Collection<OsmPrimitive> incompleteMembers;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new <code>DownloadSelectedIncompleteMembersAction</code>.
|
---|
28 | */
|
---|
29 | public DownloadSelectedIncompleteMembersAction() {
|
---|
30 | putValue(SHORT_DESCRIPTION, tr("Download incomplete members of selected relations"));
|
---|
31 | new ImageProvider("dialogs/relation", "downloadincompleteselected").getResource().attachImageIcon(this, true);
|
---|
32 | putValue(NAME, tr("Download incomplete members"));
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the set of incomplete members of the given relations.
|
---|
37 | * @param rels The relations to inspect.
|
---|
38 | * @return The set of incomplete members of the given relations.
|
---|
39 | */
|
---|
40 | public static Set<OsmPrimitive> buildSetOfIncompleteMembers(Collection<Relation> rels) {
|
---|
41 | Set<OsmPrimitive> ret = new HashSet<>();
|
---|
42 | for (Relation r : rels) {
|
---|
43 | ret.addAll(SubclassFilteredCollection.filter(r.getIncompleteMembers(), osm -> !osm.isNew()));
|
---|
44 | }
|
---|
45 | return ret;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public void actionPerformed(ActionEvent e) {
|
---|
50 | if (!isEnabled() || relations.isEmpty() || !MainApplication.isDisplayingMapView()) return;
|
---|
51 | MainApplication.worker.submit(new DownloadRelationMemberTask(
|
---|
52 | relations,
|
---|
53 | incompleteMembers,
|
---|
54 | MainApplication.getLayerManager().getEditLayer()));
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
|
---|
59 | // selected relations with incomplete members
|
---|
60 | this.relations = SubclassFilteredCollection.filter(getRelations(primitives), Relation::hasIncompleteMembers);
|
---|
61 | this.incompleteMembers = buildSetOfIncompleteMembers(relations);
|
---|
62 | updateEnabledState();
|
---|
63 | }
|
---|
64 |
|
---|
65 | @Override
|
---|
66 | protected void updateEnabledState() {
|
---|
67 | setEnabled(!incompleteMembers.isEmpty() && canDownload());
|
---|
68 | }
|
---|
69 | }
|
---|