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; 008import java.util.Set; 009 010import org.openstreetmap.josm.Main; 011import org.openstreetmap.josm.data.osm.OsmPrimitive; 012import org.openstreetmap.josm.data.osm.Relation; 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 putValue(SMALL_ICON, ImageProvider.get("selectall")); 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() || !Main.isDisplayingMapView()) return; 039 040 Set<OsmPrimitive> members = new HashSet<>(); 041 for (Relation r: relations) { 042 members.addAll(r.getMemberPrimitives()); 043 } 044 if (add) { 045 Main.getLayerManager().getEditLayer().data.addSelected(members); 046 } else { 047 Main.getLayerManager().getEditLayer().data.setSelected(members); 048 } 049 } 050}