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