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

Last change on this file since 12630 was 12630, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 7.0 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.Main;
15import org.openstreetmap.josm.data.osm.OsmPrimitive;
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.preferences.validator.ValidatorPreference;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.io.OsmTransferException;
27import org.openstreetmap.josm.tools.Shortcut;
28import org.xml.sax.SAXException;
29
30/**
31 * The action that does the validate thing.
32 * <p>
33 * This action iterates through all active tests and give them the data, so that
34 * each one can test it.
35 *
36 * @author frsantos
37 */
38public class ValidateAction extends JosmAction {
39
40 /** Last selection used to validate */
41 private transient Collection<OsmPrimitive> lastSelection;
42
43 /**
44 * Constructor
45 */
46 public ValidateAction() {
47 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
48 Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")),
49 KeyEvent.VK_V, Shortcut.SHIFT), true);
50 }
51
52 @Override
53 public void actionPerformed(ActionEvent ev) {
54 doValidate(true);
55 }
56
57 /**
58 * Does the validation.
59 * <p>
60 * If getSelectedItems is true, the selected items (or all items, if no one
61 * is selected) are validated. If it is false, last selected items are revalidated
62 *
63 * @param getSelectedItems If selected or last selected items must be validated
64 */
65 public void doValidate(boolean getSelectedItems) {
66 MapFrame map = MainApplication.getMap();
67 if (map == null || !map.isVisible())
68 return;
69
70 OsmValidator.initializeTests();
71 OsmValidator.initializeErrorLayer();
72
73 Collection<Test> tests = OsmValidator.getEnabledTests(false);
74 if (tests.isEmpty())
75 return;
76
77 Collection<OsmPrimitive> selection;
78 if (getSelectedItems) {
79 selection = Main.getLayerManager().getEditDataSet().getAllSelected();
80 if (selection.isEmpty()) {
81 selection = Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives();
82 lastSelection = null;
83 } else {
84 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
85 selection = v.visit(selection);
86 lastSelection = selection;
87 }
88 } else {
89 selection = Optional.ofNullable(lastSelection).orElseGet(
90 () -> Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives());
91 }
92
93 Main.worker.submit(new ValidationTask(tests, selection, lastSelection));
94 }
95
96 @Override
97 public void updateEnabledState() {
98 setEnabled(getLayerManager().getEditLayer() != null);
99 }
100
101 @Override
102 public void destroy() {
103 // Hack - this action should stay forever because it could be added to toolbar
104 // Do not call super.destroy() here
105 }
106
107 /**
108 * Asynchronous task for running a collection of tests against a collection
109 * of primitives
110 *
111 */
112 static class ValidationTask extends PleaseWaitRunnable {
113 private Collection<Test> tests;
114 private final Collection<OsmPrimitive> validatedPrimitives;
115 private final Collection<OsmPrimitive> formerValidatedPrimitives;
116 private boolean canceled;
117 private List<TestError> errors;
118
119 /**
120 *
121 * @param tests the tests to run
122 * @param validatedPrimitives the collection of primitives to validate.
123 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
124 */
125 ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives,
126 Collection<OsmPrimitive> formerValidatedPrimitives) {
127 super(tr("Validating"), false /*don't ignore exceptions */);
128 this.validatedPrimitives = validatedPrimitives;
129 this.formerValidatedPrimitives = formerValidatedPrimitives;
130 this.tests = tests;
131 }
132
133 @Override
134 protected void cancel() {
135 this.canceled = true;
136 }
137
138 @Override
139 protected void finish() {
140 if (canceled) return;
141
142 // update GUI on Swing EDT
143 //
144 GuiHelper.runInEDT(() -> {
145 MapFrame map = MainApplication.getMap();
146 map.validatorDialog.tree.setErrors(errors);
147 map.validatorDialog.unfurlDialog();
148 //FIXME: nicer way to find / invalidate the corresponding error layer
149 Main.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
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<>(200);
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.setPartialSelection(formerValidatedPrimitives != null);
167 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
168 test.visit(validatedPrimitives);
169 test.endTest();
170 errors.addAll(test.getErrors());
171 }
172 tests = null;
173 if (ValidatorPreference.PREF_USE_IGNORE.get()) {
174 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
175 for (TestError error : errors) {
176 if (canceled) return;
177 List<String> s = new ArrayList<>();
178 s.add(error.getIgnoreState());
179 s.add(error.getIgnoreGroup());
180 s.add(error.getIgnoreSubGroup());
181 for (String state : s) {
182 if (state != null && OsmValidator.hasIgnoredError(state)) {
183 error.setIgnored(true);
184 }
185 }
186 }
187 }
188 }
189 }
190}
Note: See TracBrowser for help on using the repository browser.