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

Last change on this file since 6053 was 5526, checked in by jttt, 12 years ago

Fix #8045 New "Registered toolbar action" bug with the Validator.

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