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

Last change on this file since 4175 was 4076, checked in by framm, 13 years ago

fix typo

  • 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 javax.swing.SwingUtilities;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.validation.OsmValidator;
18import org.openstreetmap.josm.data.validation.Test;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
22import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
23import org.openstreetmap.josm.io.OsmTransferException;
24import org.openstreetmap.josm.tools.Shortcut;
25import org.xml.sax.SAXException;
26
27/**
28 * The action that does the validate thing.
29 * <p>
30 * This action iterates through all active tests and give them the data, so that
31 * each one can test it.
32 *
33 * @author frsantos
34 */
35public class ValidateAction extends JosmAction {
36
37 /** Serializable ID */
38 private static final long serialVersionUID = -2304521273582574603L;
39
40 /** Last selection used to validate */
41 private Collection<OsmPrimitive> lastSelection;
42
43 /**
44 * Constructor
45 */
46 public ValidateAction() {
47 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"),
48 Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")), KeyEvent.VK_V, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), 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.main.validator.validateAction == null || 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().getSelected();
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 /**
104 * Asynchronous task for running a collection of tests against a collection
105 * of primitives
106 *
107 */
108 class ValidationTask extends PleaseWaitRunnable {
109 private Collection<Test> tests;
110 private Collection<OsmPrimitive> validatedPrimitives;
111 private Collection<OsmPrimitive> formerValidatedPrimitives;
112 private boolean canceled;
113 private List<TestError> errors;
114
115 /**
116 *
117 * @param tests the tests to run
118 * @param validatedPrimitives the collection of primitives to validate.
119 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
120 */
121 public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) {
122 super(tr("Validating"), false /*don't ignore exceptions */);
123 this.validatedPrimitives = validatedPrimitives;
124 this.formerValidatedPrimitives = formerValidatedPrimitives;
125 this.tests = tests;
126 }
127
128 @Override
129 protected void cancel() {
130 this.canceled = true;
131 }
132
133 @Override
134 protected void finish() {
135 if (canceled) return;
136
137 // update GUI on Swing EDT
138 //
139 Runnable r = new Runnable() {
140 @Override
141 public void run() {
142 Main.map.validatorDialog.tree.setErrors(errors);
143 Main.map.validatorDialog.unfurlDialog();
144 Main.main.getCurrentDataSet().fireSelectionChanged();
145 }
146 };
147 if (SwingUtilities.isEventDispatchThread()) {
148 r.run();
149 } else {
150 SwingUtilities.invokeLater(r);
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<TestError>(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<String>();
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.