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.LinkedHashSet;
008import java.util.Set;
009
010import org.openstreetmap.josm.data.osm.IPrimitive;
011import org.openstreetmap.josm.data.osm.IRelation;
012import org.openstreetmap.josm.gui.MainApplication;
013import org.openstreetmap.josm.tools.ImageProvider;
014
015/**
016 * Sets the current selection to the list of relations selected in this dialog
017 * @since 5793
018 */
019public class SelectMembersAction extends AbstractRelationAction {
020
021    private final boolean add;
022
023    /**
024     * Constructs a new <code>SelectMembersAction</code>.
025     * @param add if <code>true</code>, the members will be added to current selection.
026     * If <code>false</code>, the members will replace the current selection.
027     */
028    public SelectMembersAction(boolean add) {
029        putValue(SHORT_DESCRIPTION, add ? tr("Add the members of all selected relations to current selection")
030                : tr("Select the members of all selected relations"));
031        new ImageProvider("selectall").getResource().attachImageIcon(this, true);
032        putValue(NAME, add ? tr("Select members (add)") : tr("Select members"));
033        this.add = add;
034    }
035
036    @Override
037    public void actionPerformed(ActionEvent e) {
038        if (!isEnabled() || relations.isEmpty() || !MainApplication.isDisplayingMapView()) return;
039
040        Set<IPrimitive> members = new LinkedHashSet<>();
041        for (IRelation<?> r: relations) {
042            members.addAll(r.getMemberPrimitivesList());
043        }
044        if (add) {
045            MainApplication.getLayerManager().getActiveData().addSelected(members);
046        } else {
047            MainApplication.getLayerManager().getActiveData().setSelected(members);
048        }
049    }
050}