| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.awt.event.KeyEvent;
|
|---|
| 8 | import java.util.Collection;
|
|---|
| 9 | import java.util.Optional;
|
|---|
| 10 |
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 12 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
|---|
| 13 | import org.openstreetmap.josm.data.validation.Test;
|
|---|
| 14 | import org.openstreetmap.josm.data.validation.ValidationTask;
|
|---|
| 15 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 16 | import org.openstreetmap.josm.gui.MapFrame;
|
|---|
| 17 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * The action that does the validate thing.
|
|---|
| 21 | * <p>
|
|---|
| 22 | * This action iterates through all active tests and give them the data, so that
|
|---|
| 23 | * each one can test it.
|
|---|
| 24 | *
|
|---|
| 25 | * @author frsantos
|
|---|
| 26 | */
|
|---|
| 27 | public class ValidateAction extends JosmAction {
|
|---|
| 28 |
|
|---|
| 29 | /** Last selection used to validate */
|
|---|
| 30 | private transient Collection<OsmPrimitive> lastSelection;
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructor
|
|---|
| 34 | */
|
|---|
| 35 | public ValidateAction() {
|
|---|
| 36 | super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
|
|---|
| 37 | Shortcut.registerShortcut("tools:validate", tr("Validation"),
|
|---|
| 38 | KeyEvent.VK_V, Shortcut.SHIFT), true);
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | @Override
|
|---|
| 42 | public void actionPerformed(ActionEvent ev) {
|
|---|
| 43 | doValidate(true);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | /**
|
|---|
| 47 | * Does the validation.
|
|---|
| 48 | * <p>
|
|---|
| 49 | * If getSelectedItems is true, the selected items (or all items, if no one
|
|---|
| 50 | * is selected) are validated. If it is false, last selected items are revalidated
|
|---|
| 51 | *
|
|---|
| 52 | * @param getSelectedItems If selected or last selected items must be validated
|
|---|
| 53 | */
|
|---|
| 54 | public void doValidate(boolean getSelectedItems) {
|
|---|
| 55 | MapFrame map = MainApplication.getMap();
|
|---|
| 56 | if (map == null || !map.isVisible())
|
|---|
| 57 | return;
|
|---|
| 58 |
|
|---|
| 59 | OsmValidator.initializeTests();
|
|---|
| 60 |
|
|---|
| 61 | Collection<Test> tests = OsmValidator.getEnabledTests(false);
|
|---|
| 62 | if (tests.isEmpty())
|
|---|
| 63 | return;
|
|---|
| 64 |
|
|---|
| 65 | Collection<OsmPrimitive> selection;
|
|---|
| 66 | if (getSelectedItems) {
|
|---|
| 67 | selection = getLayerManager().getActiveDataSet().getAllSelected();
|
|---|
| 68 | if (selection.isEmpty()) {
|
|---|
| 69 | selection = getLayerManager().getActiveDataSet().allNonDeletedPrimitives();
|
|---|
| 70 | lastSelection = null;
|
|---|
| 71 | } else {
|
|---|
| 72 | lastSelection = selection;
|
|---|
| 73 | }
|
|---|
| 74 | } else {
|
|---|
| 75 | selection = Optional.ofNullable(lastSelection).orElseGet(
|
|---|
| 76 | () -> getLayerManager().getActiveDataSet().allNonDeletedPrimitives());
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | MainApplication.worker.submit(new ValidationTask(tests, selection, lastSelection));
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | @Override
|
|---|
| 83 | public void updateEnabledState() {
|
|---|
| 84 | setEnabled(getLayerManager().getActiveDataSet() != null);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | @Override
|
|---|
| 88 | public void destroy() {
|
|---|
| 89 | // Hack - this action should stay forever because it could be added to toolbar
|
|---|
| 90 | // Do not call super.destroy() here
|
|---|
| 91 | lastSelection = null;
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | }
|
|---|