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

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

Various stuff:

  • see #9414: remove old DeprecatedTags test
  • refactor some classes in gui.preferences package
  • improve javadoc
  • 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.initializeErrorLayer();
71
72 Collection<Test> tests = OsmValidator.getEnabledTests(false);
73 if (tests.isEmpty())
74 return;
75
76 Collection<OsmPrimitive> selection;
77 if (getSelectedItems) {
78 selection = Main.main.getCurrentDataSet().getAllSelected();
79 if (selection.isEmpty()) {
80 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
81 lastSelection = null;
82 } else {
83 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
84 selection = v.visit(selection);
85 lastSelection = selection;
86 }
87 } else {
88 if (lastSelection == null) {
89 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
90 } else {
91 selection = lastSelection;
92 }
93 }
94
95 ValidationTask task = new ValidationTask(tests, selection, lastSelection);
96 Main.worker.submit(task);
97 }
98
99 @Override
100 public void updateEnabledState() {
101 setEnabled(getEditLayer() != null);
102 }
103
104 @Override
105 public void destroy() {
106 // Hack - this action should stay forever because it could be added to toolbar
107 // Do not call super.destroy() here
108 }
109
110 /**
111 * Asynchronous task for running a collection of tests against a collection
112 * of primitives
113 *
114 */
115 static class ValidationTask extends PleaseWaitRunnable {
116 private Collection<Test> tests;
117 private Collection<OsmPrimitive> validatedPrimitives;
118 private Collection<OsmPrimitive> formerValidatedPrimitives;
119 private boolean canceled;
120 private List<TestError> errors;
121
122 /**
123 *
124 * @param tests the tests to run
125 * @param validatedPrimitives the collection of primitives to validate.
126 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
127 */
128 public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) {
129 super(tr("Validating"), false /*don't ignore exceptions */);
130 this.validatedPrimitives = validatedPrimitives;
131 this.formerValidatedPrimitives = formerValidatedPrimitives;
132 this.tests = tests;
133 }
134
135 @Override
136 protected void cancel() {
137 this.canceled = true;
138 }
139
140 @Override
141 protected void finish() {
142 if (canceled) return;
143
144 // update GUI on Swing EDT
145 //
146 GuiHelper.runInEDT(new Runnable() {
147 @Override
148 public void run() {
149 Main.map.validatorDialog.tree.setErrors(errors);
150 Main.map.validatorDialog.unfurlDialog();
151 Main.main.getCurrentDataSet().fireSelectionChanged();
152 }
153 });
154 }
155
156 @Override
157 protected void realRun() throws SAXException, IOException,
158 OsmTransferException {
159 if (tests == null || tests.isEmpty())
160 return;
161 errors = new ArrayList<TestError>(200);
162 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
163 int testCounter = 0;
164 for (Test test : tests) {
165 if (canceled)
166 return;
167 testCounter++;
168 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
169 test.setPartialSelection(formerValidatedPrimitives != null);
170 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
171 test.visit(validatedPrimitives);
172 test.endTest();
173 errors.addAll(test.getErrors());
174 }
175 tests = null;
176 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
177 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
178 for (TestError error : errors) {
179 if (canceled) return;
180 List<String> s = new ArrayList<String>();
181 s.add(error.getIgnoreState());
182 s.add(error.getIgnoreGroup());
183 s.add(error.getIgnoreSubGroup());
184 for (String state : s) {
185 if (state != null && OsmValidator.hasIgnoredError(state)) {
186 error.setIgnored(true);
187 }
188 }
189 }
190 }
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.