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.OsmPrimitiveAction;
|
---|
10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
11 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
12 | import org.openstreetmap.josm.tools.SubclassFilteredCollection;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Ancestor for all actions that want to work with relation collection and
|
---|
16 | * to be disabled if the collection is empty
|
---|
17 | * @since 5793
|
---|
18 | */
|
---|
19 | public abstract class AbstractRelationAction extends AbstractAction implements OsmPrimitiveAction {
|
---|
20 | /** relation collection */
|
---|
21 | protected transient Collection<Relation> relations = Collections.<Relation>emptySet();
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Returns the relations contained in the given collection.
|
---|
25 | * @param primitives collection of primitives
|
---|
26 | * @return the relation contained in {@code primitives}
|
---|
27 | */
|
---|
28 | protected static final Collection<Relation> getRelations(Collection<? extends OsmPrimitive> primitives) {
|
---|
29 | if (primitives == null || primitives.isEmpty()) {
|
---|
30 | return Collections.<Relation>emptySet();
|
---|
31 | } else {
|
---|
32 | return new SubclassFilteredCollection<>(primitives, Relation.class::isInstance);
|
---|
33 | }
|
---|
34 | }
|
---|
35 |
|
---|
36 | @Override
|
---|
37 | public void setPrimitives(Collection<? extends OsmPrimitive> primitives) {
|
---|
38 | this.relations = getRelations(primitives);
|
---|
39 | updateEnabledState();
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Override in subclasses to update the enabled state of the action when something changes.
|
---|
44 | */
|
---|
45 | protected void updateEnabledState() {
|
---|
46 | setEnabled(!relations.isEmpty());
|
---|
47 | }
|
---|
48 | }
|
---|