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