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