1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.SwingUtilities;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
18 | import org.openstreetmap.josm.data.validation.Test;
|
---|
19 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
20 | import org.openstreetmap.josm.data.validation.util.AgregatePrimitivesVisitor;
|
---|
21 | import org.openstreetmap.josm.gui.PleaseWaitRunnable;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
|
---|
23 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
24 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
25 | import 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 | */
|
---|
35 | public class ValidateAction extends JosmAction {
|
---|
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")), KeyEvent.VK_V, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void actionPerformed(ActionEvent ev) {
|
---|
51 | doValidate(ev, true);
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Does the validation.
|
---|
56 | * <p>
|
---|
57 | * If getSelectedItems is true, the selected items (or all items, if no one
|
---|
58 | * is selected) are validated. If it is false, last selected items are
|
---|
59 | * revalidated
|
---|
60 | *
|
---|
61 | * @param ev The event
|
---|
62 | * @param getSelectedItems If selected or last selected items must be validated
|
---|
63 | */
|
---|
64 | public void doValidate(ActionEvent ev, boolean getSelectedItems) {
|
---|
65 | if (Main.main.validator.validateAction == null || Main.map == null || !Main.map.isVisible())
|
---|
66 | return;
|
---|
67 |
|
---|
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().getSelected();
|
---|
77 | if (selection.isEmpty()) {
|
---|
78 | selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives();
|
---|
79 | lastSelection = null;
|
---|
80 | } else {
|
---|
81 | AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor();
|
---|
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 | ValidationTask task = new ValidationTask(tests, selection, lastSelection);
|
---|
93 | Main.worker.submit(task);
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void updateEnabledState() {
|
---|
98 | setEnabled(getEditLayer() != null);
|
---|
99 | }
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Asynchronous task for running a collection of tests against a collection
|
---|
103 | * of primitives
|
---|
104 | *
|
---|
105 | */
|
---|
106 |
|
---|
107 | class ValidationTask extends PleaseWaitRunnable {
|
---|
108 | private Collection<Test> tests;
|
---|
109 | private Collection<OsmPrimitive> validatedPrimitmives;
|
---|
110 | private Collection<OsmPrimitive> formerValidatedPrimitives;
|
---|
111 | private boolean canceled;
|
---|
112 | private List<TestError> errors;
|
---|
113 |
|
---|
114 | /**
|
---|
115 | *
|
---|
116 | * @param tests the tests to run
|
---|
117 | * @param validatedPrimitives the collection of primitives to validate.
|
---|
118 | * @param formerValidatedPrimitives the last collection of primitives being validates. May be null.
|
---|
119 | */
|
---|
120 | public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) {
|
---|
121 | super(tr("Validating"), false /*don't ignore exceptions */);
|
---|
122 | this.validatedPrimitmives = validatedPrimitives;
|
---|
123 | this.formerValidatedPrimitives = formerValidatedPrimitives;
|
---|
124 | this.tests = tests;
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | protected void cancel() {
|
---|
129 | this.canceled = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | @Override
|
---|
133 | protected void finish() {
|
---|
134 | if (canceled) return;
|
---|
135 |
|
---|
136 | // update GUI on Swing EDT
|
---|
137 | //
|
---|
138 | Runnable r = new Runnable() {
|
---|
139 | public void run() {
|
---|
140 | Main.map.validatorDialog.tree.setErrors(errors);
|
---|
141 | Main.map.validatorDialog.unfurlDialog();
|
---|
142 | Main.main.getCurrentDataSet().fireSelectionChanged();
|
---|
143 | }
|
---|
144 | };
|
---|
145 | if (SwingUtilities.isEventDispatchThread()) {
|
---|
146 | r.run();
|
---|
147 | } else {
|
---|
148 | SwingUtilities.invokeLater(r);
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | protected void realRun() throws SAXException, IOException,
|
---|
154 | OsmTransferException {
|
---|
155 | if (tests == null || tests.isEmpty()) return;
|
---|
156 | errors = new ArrayList<TestError>(200);
|
---|
157 | getProgressMonitor().setTicksCount(tests.size() * validatedPrimitmives.size());
|
---|
158 | int testCounter = 0;
|
---|
159 | for (Test test : tests) {
|
---|
160 | if (canceled) return;
|
---|
161 | testCounter++;
|
---|
162 | getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName()));
|
---|
163 | test.setPartialSelection(formerValidatedPrimitives != null);
|
---|
164 | test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitmives.size(), false));
|
---|
165 | test.visit(validatedPrimitmives);
|
---|
166 | test.endTest();
|
---|
167 | errors.addAll(test.getErrors());
|
---|
168 | }
|
---|
169 | tests = null;
|
---|
170 | if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
|
---|
171 | getProgressMonitor().subTask(tr("Updating ignored errors ..."));
|
---|
172 | for (TestError error : errors) {
|
---|
173 | if (canceled) return;
|
---|
174 | List<String> s = new ArrayList<String>();
|
---|
175 | s.add(error.getIgnoreState());
|
---|
176 | s.add(error.getIgnoreGroup());
|
---|
177 | s.add(error.getIgnoreSubGroup());
|
---|
178 | for (String state : s) {
|
---|
179 | if (state != null && OsmValidator.hasIgnoredError(state)) {
|
---|
180 | error.setIgnored(true);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|