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

Last change on this file since 12186 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
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[3669]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;
[11553]12import java.util.Optional;
[3669]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;
[4075]19import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
[3669]20import org.openstreetmap.josm.gui.PleaseWaitRunnable;
[6529]21import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
[5452]22import org.openstreetmap.josm.gui.util.GuiHelper;
[3669]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 {
[4874]36
[3669]37 /** Last selection used to validate */
[8308]38 private transient Collection<OsmPrimitive> lastSelection;
[3669]39
40 /**
41 * Constructor
42 */
43 public ValidateAction() {
44 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
[5463]45 Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")),
46 KeyEvent.VK_V, Shortcut.SHIFT), true);
[3669]47 }
48
[6084]49 @Override
[3669]50 public void actionPerformed(ActionEvent ev) {
[8855]51 doValidate(true);
[3669]52 }
53
54 /**
55 * Does the validation.
56 * <p>
57 * If getSelectedItems is true, the selected items (or all items, if no one
[11553]58 * is selected) are validated. If it is false, last selected items are revalidated
[3669]59 *
60 * @param getSelectedItems If selected or last selected items must be validated
61 */
[8855]62 public void doValidate(boolean getSelectedItems) {
[5463]63 if (Main.map == null || !Main.map.isVisible())
[3669]64 return;
65
[6779]66 OsmValidator.initializeTests();
[3669]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) {
[10446]75 selection = Main.getLayerManager().getEditDataSet().getAllSelected();
[3669]76 if (selection.isEmpty()) {
[10446]77 selection = Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives();
[3669]78 lastSelection = null;
79 } else {
[4075]80 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
[3669]81 selection = v.visit(selection);
82 lastSelection = selection;
83 }
84 } else {
[11553]85 selection = Optional.ofNullable(lastSelection).orElseGet(
86 () -> Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives());
[3669]87 }
88
[11553]89 Main.worker.submit(new ValidationTask(tests, selection, lastSelection));
[3669]90 }
91
92 @Override
93 public void updateEnabledState() {
[10382]94 setEnabled(getLayerManager().getEditLayer() != null);
[3669]95 }
96
[5526]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
[3669]103 /**
104 * Asynchronous task for running a collection of tests against a collection
105 * of primitives
106 *
107 */
[4874]108 static class ValidationTask extends PleaseWaitRunnable {
[3669]109 private Collection<Test> tests;
[9067]110 private final Collection<OsmPrimitive> validatedPrimitives;
111 private final Collection<OsmPrimitive> formerValidatedPrimitives;
[3669]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 */
[8836]121 ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives,
[8509]122 Collection<OsmPrimitive> formerValidatedPrimitives) {
[3669]123 super(tr("Validating"), false /*don't ignore exceptions */);
[10378]124 this.validatedPrimitives = validatedPrimitives;
[3669]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 //
[10601]140 GuiHelper.runInEDT(() -> {
141 Main.map.validatorDialog.tree.setErrors(errors);
142 Main.map.validatorDialog.unfurlDialog();
143 Main.getLayerManager().getEditDataSet().fireSelectionChanged();
[5452]144 });
[3669]145 }
146
147 @Override
148 protected void realRun() throws SAXException, IOException,
[4874]149 OsmTransferException {
[3671]150 if (tests == null || tests.isEmpty())
151 return;
[7005]152 errors = new ArrayList<>(200);
[4076]153 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
[3669]154 int testCounter = 0;
155 for (Test test : tests) {
[3671]156 if (canceled)
157 return;
[3669]158 testCounter++;
[8510]159 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(), test.getName()));
[3669]160 test.setPartialSelection(formerValidatedPrimitives != null);
[4076]161 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
162 test.visit(validatedPrimitives);
[3669]163 test.endTest();
164 errors.addAll(test.getErrors());
165 }
166 tests = null;
[10880]167 if (ValidatorPreference.PREF_USE_IGNORE.get()) {
[3669]168 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
169 for (TestError error : errors) {
170 if (canceled) return;
[7005]171 List<String> s = new ArrayList<>();
[3669]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.