001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.event.ActionEvent;
007import java.awt.event.KeyEvent;
008import java.util.Collection;
009import java.util.Optional;
010
011import org.openstreetmap.josm.data.osm.OsmPrimitive;
012import org.openstreetmap.josm.data.validation.OsmValidator;
013import org.openstreetmap.josm.data.validation.Test;
014import org.openstreetmap.josm.data.validation.ValidationTask;
015import org.openstreetmap.josm.gui.MainApplication;
016import org.openstreetmap.josm.gui.MapFrame;
017import org.openstreetmap.josm.tools.Shortcut;
018
019/**
020 * The action that does the validate thing.
021 * <p>
022 * This action iterates through all active tests and give them the data, so that
023 * each one can test it.
024 *
025 * @author frsantos
026 */
027public class ValidateAction extends JosmAction {
028
029    /** Last selection used to validate */
030    private transient Collection<OsmPrimitive> lastSelection;
031
032    /**
033     * Constructor
034     */
035    public ValidateAction() {
036        super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
037                Shortcut.registerShortcut("tools:validate", tr("Validation"),
038                        KeyEvent.VK_V, Shortcut.SHIFT), true);
039    }
040
041    @Override
042    public void actionPerformed(ActionEvent ev) {
043        doValidate(true);
044    }
045
046    /**
047     * Does the validation.
048     * <p>
049     * If getSelectedItems is true, the selected items (or all items, if no one
050     * is selected) are validated. If it is false, last selected items are revalidated
051     *
052     * @param getSelectedItems If selected or last selected items must be validated
053     */
054    public void doValidate(boolean getSelectedItems) {
055        MapFrame map = MainApplication.getMap();
056        if (map == null || !map.isVisible())
057            return;
058
059        OsmValidator.initializeTests();
060
061        Collection<Test> tests = OsmValidator.getEnabledTests(false);
062        if (tests.isEmpty())
063            return;
064
065        Collection<OsmPrimitive> selection;
066        if (getSelectedItems) {
067            selection = getLayerManager().getActiveDataSet().getAllSelected();
068            if (selection.isEmpty()) {
069                selection = getLayerManager().getActiveDataSet().allNonDeletedPrimitives();
070                lastSelection = null;
071            } else {
072                lastSelection = selection;
073            }
074        } else {
075            selection = Optional.ofNullable(lastSelection).orElseGet(
076                    () -> getLayerManager().getActiveDataSet().allNonDeletedPrimitives());
077        }
078
079        MainApplication.worker.submit(new ValidationTask(tests, selection, lastSelection));
080    }
081
082    @Override
083    public void updateEnabledState() {
084        setEnabled(getLayerManager().getActiveDataSet() != null);
085    }
086
087    @Override
088    public void destroy() {
089        // Hack - this action should stay forever because it could be added to toolbar
090        // Do not call super.destroy() here
091        lastSelection = null;
092    }
093
094}