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

Last change on this file since 6815 was 6779, checked in by simon04, 10 years ago

Initialize validator tests on demand, separate initialization of validator from presets, print initialization timing information to console

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. See LICENSE file for details.
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 /** Serializable ID */
37 private static final long serialVersionUID = -2304521273582574603L;
38
39 /** Last selection used to validate */
40 private 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("Tool: {0}", tr("Validation")),
48 KeyEvent.VK_V, Shortcut.SHIFT), true);
49 }
50
51 @Override
52 public void actionPerformed(ActionEvent ev) {
53 doValidate(ev, 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
61 * revalidated
62 *
63 * @param ev The event
64 * @param getSelectedItems If selected or last selected items must be validated
65 */
66 public void doValidate(ActionEvent ev, boolean getSelectedItems) {
67 if (Main.map == null || !Main.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.main.getCurrentDataSet().getAllSelected();
80 if (selection.isEmpty()) {
81 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
82 lastSelection = null;
83 } else {
84 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
85 selection = v.visit(selection);
86 lastSelection = selection;
87 }
88 } else {
89 if (lastSelection == null) {
90 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
91 } else {
92 selection = lastSelection;
93 }
94 }
95
96 ValidationTask task = new ValidationTask(tests, selection, lastSelection);
97 Main.worker.submit(task);
98 }
99
100 @Override
101 public void updateEnabledState() {
102 setEnabled(getEditLayer() != null);
103 }
104
105 @Override
106 public void destroy() {
107 // Hack - this action should stay forever because it could be added to toolbar
108 // Do not call super.destroy() here
109 }
110
111 /**
112 * Asynchronous task for running a collection of tests against a collection
113 * of primitives
114 *
115 */
116 static class ValidationTask extends PleaseWaitRunnable {
117 private Collection<Test> tests;
118 private Collection<OsmPrimitive> validatedPrimitives;
119 private Collection<OsmPrimitive> formerValidatedPrimitives;
120 private boolean canceled;
121 private List<TestError> errors;
122
123 /**
124 *
125 * @param tests the tests to run
126 * @param validatedPrimitives the collection of primitives to validate.
127 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
128 */
129 public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) {
130 super(tr("Validating"), false /*don't ignore exceptions */);
131 this.validatedPrimitives = validatedPrimitives;
132 this.formerValidatedPrimitives = formerValidatedPrimitives;
133 this.tests = tests;
134 }
135
136 @Override
137 protected void cancel() {
138 this.canceled = true;
139 }
140
141 @Override
142 protected void finish() {
143 if (canceled) return;
144
145 // update GUI on Swing EDT
146 //
147 GuiHelper.runInEDT(new Runnable() {
148 @Override
149 public void run() {
150 Main.map.validatorDialog.tree.setErrors(errors);
151 Main.map.validatorDialog.unfurlDialog();
152 Main.main.getCurrentDataSet().fireSelectionChanged();
153 }
154 });
155 }
156
157 @Override
158 protected void realRun() throws SAXException, IOException,
159 OsmTransferException {
160 if (tests == null || tests.isEmpty())
161 return;
162 errors = new ArrayList<TestError>(200);
163 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
164 int testCounter = 0;
165 for (Test test : tests) {
166 if (canceled)
167 return;
168 testCounter++;
169 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
170 test.setPartialSelection(formerValidatedPrimitives != null);
171 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
172 test.visit(validatedPrimitives);
173 test.endTest();
174 errors.addAll(test.getErrors());
175 }
176 tests = null;
177 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
178 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
179 for (TestError error : errors) {
180 if (canceled) return;
181 List<String> s = new ArrayList<String>();
182 s.add(error.getIgnoreState());
183 s.add(error.getIgnoreGroup());
184 s.add(error.getIgnoreSubGroup());
185 for (String state : s) {
186 if (state != null && OsmValidator.hasIgnoredError(state)) {
187 error.setIgnored(true);
188 }
189 }
190 }
191 }
192 }
193 }
194}
Note: See TracBrowser for help on using the repository browser.