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