source: josm/trunk/src/org/openstreetmap/josm/actions/ValidateAction.java@ 17534

Last change on this file since 17534 was 17188, checked in by Klumbumbus, 4 years ago

fix #19851 - Fix shortcut names

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.awt.event.KeyEvent;
8import java.io.IOException;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.List;
12import java.util.Optional;
13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
16import org.openstreetmap.josm.data.validation.OsmValidator;
17import org.openstreetmap.josm.data.validation.Test;
18import org.openstreetmap.josm.data.validation.TestError;
19import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
20import org.openstreetmap.josm.gui.MainApplication;
21import org.openstreetmap.josm.gui.MapFrame;
22import org.openstreetmap.josm.gui.PleaseWaitRunnable;
23import org.openstreetmap.josm.gui.layer.ValidatorLayer;
24import org.openstreetmap.josm.gui.util.GuiHelper;
25import org.openstreetmap.josm.io.OsmTransferException;
26import org.openstreetmap.josm.tools.Shortcut;
27import org.xml.sax.SAXException;
28
29/**
30 * The action that does the validate thing.
31 * <p>
32 * This action iterates through all active tests and give them the data, so that
33 * each one can test it.
34 *
35 * @author frsantos
36 */
37public class ValidateAction extends JosmAction {
38
39 /** Last selection used to validate */
40 private transient Collection<OsmPrimitive> lastSelection;
41
42 /**
43 * Constructor
44 */
45 public ValidateAction() {
46 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
47 Shortcut.registerShortcut("tools:validate", tr("Validation"),
48 KeyEvent.VK_V, Shortcut.SHIFT), true);
49 }
50
51 @Override
52 public void actionPerformed(ActionEvent ev) {
53 doValidate(true);
54 }
55
56 /**
57 * Does the validation.
58 * <p>
59 * If getSelectedItems is true, the selected items (or all items, if no one
60 * is selected) are validated. If it is false, last selected items are revalidated
61 *
62 * @param getSelectedItems If selected or last selected items must be validated
63 */
64 public void doValidate(boolean getSelectedItems) {
65 MapFrame map = MainApplication.getMap();
66 if (map == null || !map.isVisible())
67 return;
68
69 OsmValidator.initializeTests();
70
71 Collection<Test> tests = OsmValidator.getEnabledTests(false);
72 if (tests.isEmpty())
73 return;
74
75 Collection<OsmPrimitive> selection;
76 if (getSelectedItems) {
77 selection = getLayerManager().getActiveDataSet().getAllSelected();
78 if (selection.isEmpty()) {
79 selection = getLayerManager().getActiveDataSet().allNonDeletedPrimitives();
80 lastSelection = null;
81 } else {
82 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
83 selection = v.visit(selection);
84 lastSelection = selection;
85 }
86 } else {
87 selection = Optional.ofNullable(lastSelection).orElseGet(
88 () -> getLayerManager().getActiveDataSet().allNonDeletedPrimitives());
89 }
90
91 MainApplication.worker.submit(new ValidationTask(tests, selection, lastSelection));
92 }
93
94 @Override
95 public void updateEnabledState() {
96 setEnabled(getLayerManager().getActiveDataSet() != null);
97 }
98
99 @Override
100 public void destroy() {
101 // Hack - this action should stay forever because it could be added to toolbar
102 // Do not call super.destroy() here
103 lastSelection = null;
104 }
105
106 /**
107 * Asynchronous task for running a collection of tests against a collection of primitives
108 */
109 static class ValidationTask extends PleaseWaitRunnable {
110 private Collection<Test> tests;
111 private final Collection<OsmPrimitive> validatedPrimitives;
112 private final Collection<OsmPrimitive> formerValidatedPrimitives;
113 private boolean canceled;
114 private List<TestError> errors;
115
116 /**
117 * Constructs a new {@code ValidationTask}
118 * @param tests the tests to run
119 * @param validatedPrimitives the collection of primitives to validate.
120 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
121 */
122 ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives,
123 Collection<OsmPrimitive> formerValidatedPrimitives) {
124 super(tr("Validating"), false /*don't ignore exceptions */);
125 this.validatedPrimitives = validatedPrimitives;
126 this.formerValidatedPrimitives = formerValidatedPrimitives;
127 this.tests = tests;
128 }
129
130 @Override
131 protected void cancel() {
132 this.canceled = true;
133 }
134
135 @Override
136 protected void finish() {
137 if (canceled) return;
138
139 // update GUI on Swing EDT
140 //
141 GuiHelper.runInEDT(() -> {
142 MapFrame map = MainApplication.getMap();
143 map.validatorDialog.unfurlDialog();
144 map.validatorDialog.tree.setErrors(errors);
145 //FIXME: nicer way to find / invalidate the corresponding error layer
146 MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
147 if (!errors.isEmpty()) {
148 OsmValidator.initializeErrorLayer();
149 }
150 });
151 }
152
153 @Override
154 protected void realRun() throws SAXException, IOException,
155 OsmTransferException {
156 if (tests == null || tests.isEmpty())
157 return;
158 errors = new ArrayList<>();
159 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
160 int testCounter = 0;
161 for (Test test : tests) {
162 if (canceled)
163 return;
164 testCounter++;
165 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(), test.getName()));
166 test.setBeforeUpload(false);
167 test.setPartialSelection(formerValidatedPrimitives != null);
168 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
169 test.visit(validatedPrimitives);
170 test.endTest();
171 errors.addAll(test.getErrors());
172 test.clear();
173 }
174 tests = null;
175 if (Boolean.TRUE.equals(ValidatorPrefHelper.PREF_USE_IGNORE.get())) {
176 getProgressMonitor().setCustomText("");
177 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
178 for (TestError error : errors) {
179 if (canceled) return;
180 error.updateIgnored();
181 }
182 }
183 }
184 }
185}
Note: See TracBrowser for help on using the repository browser.