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 |
|
---|
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 | AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor();
|
---|
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> validatedPrimitmives;
|
---|
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.validatedPrimitmives = 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() * validatedPrimitmives.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(validatedPrimitmives.size(), false));
|
---|
169 | test.visit(validatedPrimitmives);
|
---|
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 | }
|
---|