001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.relation;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.util.HashSet;
008
009import org.openstreetmap.josm.Main;
010import org.openstreetmap.josm.data.osm.OsmPrimitive;
011import org.openstreetmap.josm.data.osm.Relation;
012import org.openstreetmap.josm.tools.ImageProvider;
013
014/**
015 * Sets the current selection to the list of relations selected in this dialog
016 * @since 5793
017 */
018public class SelectMembersAction extends AbstractRelationAction {
019    
020    private final boolean add;
021
022    /**
023     * Constructs a new <code>SelectMembersAction</code>.
024     * @param add if <code>true</code>, the members will be added to current selection. If <code>false</code>, the members will replace the current selection.
025     */
026    public SelectMembersAction(boolean add) {
027        putValue(SHORT_DESCRIPTION,add ? tr("Add the members of all selected relations to current selection")
028                : tr("Select the members of all selected relations"));
029        putValue(SMALL_ICON, ImageProvider.get("selectall"));
030        putValue(NAME, add ? tr("Select members (add)") : tr("Select members"));
031        this.add = add;
032    }
033
034    @Override
035    public void actionPerformed(ActionEvent e) {
036        if (!isEnabled() || relations.isEmpty() || !Main.isDisplayingMapView()) return;
037        
038        HashSet<OsmPrimitive> members = new HashSet<OsmPrimitive>();
039        for (Relation r: relations) {
040            members.addAll(r.getMemberPrimitives());
041        }
042        if (add) {
043            Main.main.getEditLayer().data.addSelected(members);
044        } else {
045            Main.main.getEditLayer().data.setSelected(members);
046        }
047    }
048}