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