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

Last change on this file since 7402 was 7117, checked in by Don-vip, 10 years ago

validator - remove old and unused serializable stuff

  • Property svn:eol-style set to native
File size: 6.8 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 /** Last selection used to validate */
37 private 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(ev, 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 ev The event
61 * @param getSelectedItems If selected or last selected items must be validated
62 */
63 public void doValidate(ActionEvent ev, boolean getSelectedItems) {
64 if (Main.map == null || !Main.map.isVisible())
65 return;
66
67 OsmValidator.initializeTests();
68 OsmValidator.initializeErrorLayer();
69
70 Collection<Test> tests = OsmValidator.getEnabledTests(false);
71 if (tests.isEmpty())
72 return;
73
74 Collection<OsmPrimitive> selection;
75 if (getSelectedItems) {
76 selection = Main.main.getCurrentDataSet().getAllSelected();
77 if (selection.isEmpty()) {
78 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
79 lastSelection = null;
80 } else {
81 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
82 selection = v.visit(selection);
83 lastSelection = selection;
84 }
85 } else {
86 if (lastSelection == null) {
87 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
88 } else {
89 selection = lastSelection;
90 }
91 }
92
93 ValidationTask task = new ValidationTask(tests, selection, lastSelection);
94 Main.worker.submit(task);
95 }
96
97 @Override
98 public void updateEnabledState() {
99 setEnabled(getEditLayer() != null);
100 }
101
102 @Override
103 public void destroy() {
104 // Hack - this action should stay forever because it could be added to toolbar
105 // Do not call super.destroy() here
106 }
107
108 /**
109 * Asynchronous task for running a collection of tests against a collection
110 * of primitives
111 *
112 */
113 static class ValidationTask extends PleaseWaitRunnable {
114 private Collection<Test> tests;
115 private Collection<OsmPrimitive> validatedPrimitives;
116 private Collection<OsmPrimitive> formerValidatedPrimitives;
117 private boolean canceled;
118 private List<TestError> errors;
119
120 /**
121 *
122 * @param tests the tests to run
123 * @param validatedPrimitives the collection of primitives to validate.
124 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
125 */
126 public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, 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(new Runnable() {
145 @Override
146 public void run() {
147 Main.map.validatorDialog.tree.setErrors(errors);
148 Main.map.validatorDialog.unfurlDialog();
149 Main.main.getCurrentDataSet().fireSelectionChanged();
150 }
151 });
152 }
153
154 @Override
155 protected void realRun() throws SAXException, IOException,
156 OsmTransferException {
157 if (tests == null || tests.isEmpty())
158 return;
159 errors = new ArrayList<>(200);
160 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
161 int testCounter = 0;
162 for (Test test : tests) {
163 if (canceled)
164 return;
165 testCounter++;
166 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
167 test.setPartialSelection(formerValidatedPrimitives != null);
168 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
169 test.visit(validatedPrimitives);
170 test.endTest();
171 errors.addAll(test.getErrors());
172 }
173 tests = null;
174 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
175 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
176 for (TestError error : errors) {
177 if (canceled) return;
178 List<String> s = new ArrayList<>();
179 s.add(error.getIgnoreState());
180 s.add(error.getIgnoreGroup());
181 s.add(error.getIgnoreSubGroup());
182 for (String state : s) {
183 if (state != null && OsmValidator.hasIgnoredError(state)) {
184 error.setIgnored(true);
185 }
186 }
187 }
188 }
189 }
190 }
191}
Note: See TracBrowser for help on using the repository browser.