1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.relation;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 |
|
---|
7 | import javax.swing.AbstractAction;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.actions.IPrimitiveAction;
|
---|
10 | import org.openstreetmap.josm.data.osm.DownloadPolicy;
|
---|
11 | import org.openstreetmap.josm.data.osm.IPrimitive;
|
---|
12 | import org.openstreetmap.josm.data.osm.IRelation;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmData;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.io.NetworkManager;
|
---|
17 | import org.openstreetmap.josm.io.OnlineResource;
|
---|
18 | import org.openstreetmap.josm.tools.Destroyable;
|
---|
19 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
20 | import org.openstreetmap.josm.tools.Utils;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Ancestor for all actions that want to work with relation collection and
|
---|
24 | * to be disabled if the collection is empty
|
---|
25 | * @since 5793
|
---|
26 | * @since 13957 (signature)
|
---|
27 | */
|
---|
28 | public abstract class AbstractRelationAction extends AbstractAction implements IPrimitiveAction, Destroyable {
|
---|
29 | /** relation collection */
|
---|
30 | protected transient Collection<IRelation<?>> relations = Collections.<IRelation<?>>emptySet();
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Returns the relations contained in the given collection.
|
---|
34 | * @param primitives collection of primitives
|
---|
35 | * @return the relation contained in {@code primitives}
|
---|
36 | */
|
---|
37 | protected static final Collection<IRelation<?>> getRelations(Collection<? extends IPrimitive> primitives) {
|
---|
38 | if (primitives == null || primitives.isEmpty()) {
|
---|
39 | return Collections.<IRelation<?>>emptySet();
|
---|
40 | } else {
|
---|
41 | return new SubclassFilteredCollection<>(primitives, IRelation.class::isInstance);
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | @Override
|
---|
46 | public void setPrimitives(Collection<? extends IPrimitive> primitives) {
|
---|
47 | this.relations = getRelations(primitives);
|
---|
48 | updateEnabledState();
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Override in subclasses to update the enabled state of the action when something changes.
|
---|
53 | */
|
---|
54 | protected void updateEnabledState() {
|
---|
55 | setEnabled(!relations.isEmpty());
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected final boolean canModify() {
|
---|
59 | SubclassFilteredCollection<IRelation<?>, Relation> filteredRelations = Utils.filteredCollection(relations, Relation.class);
|
---|
60 | return OsmUtils.isOsmCollectionEditable(filteredRelations) && filteredRelations.parallelStream().anyMatch(r -> !r.isDeleted());
|
---|
61 | }
|
---|
62 |
|
---|
63 | protected final boolean canDownload() {
|
---|
64 | if (relations.isEmpty()) {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 | OsmData<?, ?, ?, ?> ds = relations.iterator().next().getDataSet();
|
---|
68 | return !NetworkManager.isOffline(OnlineResource.OSM_API)
|
---|
69 | && ds != null && !ds.isLocked() && DownloadPolicy.BLOCKED != ds.getDownloadPolicy();
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected void setHelpId(String helpId) {
|
---|
73 | putValue("help", helpId);
|
---|
74 | }
|
---|
75 |
|
---|
76 | @Override
|
---|
77 | public void destroy() {
|
---|
78 | relations = null;
|
---|
79 | }
|
---|
80 | }
|
---|