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

Last change on this file since 10809 was 10601, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

  • Property svn:eol-style set to native
File size: 6.8 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;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.validation.OsmValidator;
16import org.openstreetmap.josm.data.validation.Test;
17import org.openstreetmap.josm.data.validation.TestError;
18import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
19import org.openstreetmap.josm.gui.PleaseWaitRunnable;
20import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
21import org.openstreetmap.josm.gui.util.GuiHelper;
22import org.openstreetmap.josm.io.OsmTransferException;
23import org.openstreetmap.josm.tools.Shortcut;
24import org.xml.sax.SAXException;
25
26/**
27 * The action that does the validate thing.
28 * <p>
29 * This action iterates through all active tests and give them the data, so that
30 * each one can test it.
31 *
32 * @author frsantos
33 */
34public class ValidateAction extends JosmAction {
35
36 /** Last selection used to validate */
37 private transient Collection<OsmPrimitive> lastSelection;
38
39 /**
40 * Constructor
41 */
42 public ValidateAction() {
43 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
44 Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")),
45 KeyEvent.VK_V, Shortcut.SHIFT), true);
46 }
47
48 @Override
49 public void actionPerformed(ActionEvent ev) {
50 doValidate(true);
51 }
52
53 /**
54 * Does the validation.
55 * <p>
56 * If getSelectedItems is true, the selected items (or all items, if no one
57 * is selected) are validated. If it is false, last selected items are
58 * 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 if (lastSelection == null) {
86 selection = Main.getLayerManager().getEditDataSet().allNonDeletedPrimitives();
87 } else {
88 selection = lastSelection;
89 }
90 }
91
92 ValidationTask task = new ValidationTask(tests, selection, lastSelection);
93 Main.worker.submit(task);
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 Main.map.validatorDialog.tree.setErrors(errors);
146 Main.map.validatorDialog.unfurlDialog();
147 Main.getLayerManager().getEditDataSet().fireSelectionChanged();
148 });
149 }
150
151 @Override
152 protected void realRun() throws SAXException, IOException,
153 OsmTransferException {
154 if (tests == null || tests.isEmpty())
155 return;
156 errors = new ArrayList<>(200);
157 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
158 int testCounter = 0;
159 for (Test test : tests) {
160 if (canceled)
161 return;
162 testCounter++;
163 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(), test.getName()));
164 test.setPartialSelection(formerValidatedPrimitives != null);
165 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
166 test.visit(validatedPrimitives);
167 test.endTest();
168 errors.addAll(test.getErrors());
169 }
170 tests = null;
171 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
172 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
173 for (TestError error : errors) {
174 if (canceled) return;
175 List<String> s = new ArrayList<>();
176 s.add(error.getIgnoreState());
177 s.add(error.getIgnoreGroup());
178 s.add(error.getIgnoreSubGroup());
179 for (String state : s) {
180 if (state != null && OsmValidator.hasIgnoredError(state)) {
181 error.setIgnored(true);
182 }
183 }
184 }
185 }
186 }
187 }
188}
Note: See TracBrowser for help on using the repository browser.