001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
005import static org.openstreetmap.josm.tools.I18n.tr;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009
010import org.openstreetmap.josm.data.osm.OsmData;
011import org.openstreetmap.josm.tools.Shortcut;
012
013/**
014 * User action to invert the selection in the current dataset.
015 */
016public class InvertSelectionAction extends JosmAction {
017
018    /**
019     * Constructs a new {@code SelectAllAction}.
020     */
021    public InvertSelectionAction() {
022        super(tr("Invert Selection"), "invert_selection", tr("Invert Selection"),
023                Shortcut.registerShortcut("selection:invertselection",
024                tr("Selection: {0}", tr("Invert Selection")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), true);
025                setHelpId(ht("/Action/InvertSelection"));
026    }
027
028    @Override
029    public void actionPerformed(ActionEvent e) {
030        if (!isEnabled())
031            return;
032        OsmData<?, ?, ?, ?> ds = getLayerManager().getActiveData();
033        ds.setSelected(ds.getPrimitives(t -> !t.isSelected()));
034    }
035
036    @Override
037    protected boolean listenToSelectionChange() {
038        return false;
039    }
040
041    @Override
042    protected void updateEnabledState() {
043        setEnabled(getLayerManager().getActiveData() != null);
044    }
045}