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