001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007
008import org.openstreetmap.josm.tools.ImageProvider;
009
010/**
011 * Selects  members in the relation editor which refer to primitives in the current selection of the context layer.
012 * @since 9496
013 */
014public class SelectedMembersForSelectionAction extends AddFromSelectionAction {
015    private static final long serialVersionUID = 1L;
016
017    /**
018     * Constructs a new {@code SelectedMembersForSelectionAction}.
019     * @param editorAccess An interface to access the relation editor contents.
020     */
021    public SelectedMembersForSelectionAction(IRelationEditorActionAccess editorAccess) {
022        super(editorAccess, IRelationEditorUpdateOn.SELECTION_TABLE_CHANGE, IRelationEditorUpdateOn.MEMBER_TABLE_CHANGE);
023        putValue(SHORT_DESCRIPTION, tr("Select relation members which refer to objects in the current selection"));
024        new ImageProvider("dialogs/relation", "selectmembers").getResource().attachImageIcon(this, true);
025        updateEnabledState();
026    }
027
028    @Override
029    protected void updateEnabledState() {
030        boolean enabled = getSelectionTableModel().getRowCount() > 0
031        && !editorAccess.getMemberTableModel().getChildPrimitives(getLayer().data.getSelected()).isEmpty();
032        setEnabled(enabled);
033    }
034
035    @Override
036    public void actionPerformed(ActionEvent e) {
037        editorAccess.getMemberTableModel().selectMembersReferringTo(getLayer().data.getSelected());
038    }
039}