001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.relation; 003 004import java.util.Collection; 005import java.util.Collections; 006 007import javax.swing.AbstractAction; 008 009import org.openstreetmap.josm.actions.OsmPrimitiveAction; 010import org.openstreetmap.josm.data.osm.OsmPrimitive; 011import org.openstreetmap.josm.data.osm.Relation; 012import org.openstreetmap.josm.tools.SubclassFilteredCollection; 013 014/** 015 * Ancestor for all actions that want to work with relation collection and 016 * to be disabled is the collection is empty 017 * @since 5793 018 */ 019public abstract class AbstractRelationAction extends AbstractAction implements OsmPrimitiveAction { 020 protected Collection<Relation> relations = Collections.<Relation>emptySet(); 021 022 protected static final Collection<Relation> getRelations(Collection<? extends OsmPrimitive> primitives) { 023 if (primitives == null || primitives.isEmpty()) { 024 return Collections.<Relation>emptySet(); 025 } else { 026 return new SubclassFilteredCollection<OsmPrimitive, Relation>( 027 primitives, OsmPrimitive.relationPredicate); 028 } 029 } 030 031 /* (non-Javadoc) 032 * @see org.openstreetmap.josm.actions.relation.RelationAction#setPrimitives 033 */ 034 @Override 035 public void setPrimitives(Collection<? extends OsmPrimitive> primitives) { 036 this.relations = getRelations(primitives); 037 updateEnabledState(); 038 } 039 040 protected void updateEnabledState() { 041 setEnabled(!relations.isEmpty()); 042 } 043}