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

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

refactor handling of null values - use Java 8 Optional where possible

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