1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagConstraints;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.JCheckBox;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.command.Command;
|
---|
15 | import org.openstreetmap.josm.data.osm.Node;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
18 | import org.openstreetmap.josm.data.osm.Way;
|
---|
19 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
20 | import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
|
---|
21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
22 | import org.openstreetmap.josm.tools.GBC;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Parent class for all validation tests.
|
---|
26 | * <p>
|
---|
27 | * A test is a primitive visitor, so that it can access to all data to be
|
---|
28 | * validated. These primitives are always visited in the same order: nodes
|
---|
29 | * first, then ways.
|
---|
30 | *
|
---|
31 | * @author frsantos
|
---|
32 | */
|
---|
33 | public class Test extends AbstractVisitor
|
---|
34 | {
|
---|
35 | /** Name of the test */
|
---|
36 | protected final String name;
|
---|
37 |
|
---|
38 | /** Description of the test */
|
---|
39 | protected final String description;
|
---|
40 |
|
---|
41 | /** Whether this test is enabled. Enabled by default */
|
---|
42 | public boolean enabled = true;
|
---|
43 |
|
---|
44 | /** The preferences check for validation */
|
---|
45 | protected JCheckBox checkEnabled;
|
---|
46 |
|
---|
47 | /** The preferences check for validation on upload */
|
---|
48 | protected JCheckBox checkBeforeUpload;
|
---|
49 |
|
---|
50 | /** Whether this test must check before upload. Enabled by default */
|
---|
51 | public boolean testBeforeUpload = true;
|
---|
52 |
|
---|
53 | /** Whether this test is performing just before an upload */
|
---|
54 | protected boolean isBeforeUpload;
|
---|
55 |
|
---|
56 | /** The list of errors */
|
---|
57 | protected List<TestError> errors = new ArrayList<TestError>(30);
|
---|
58 |
|
---|
59 | /** Whether the test is run on a partial selection data */
|
---|
60 | protected boolean partialSelection;
|
---|
61 |
|
---|
62 | /** the progress monitor to use */
|
---|
63 | protected ProgressMonitor progressMonitor;
|
---|
64 | /**
|
---|
65 | * Constructor
|
---|
66 | * @param name Name of the test
|
---|
67 | * @param description Description of the test
|
---|
68 | */
|
---|
69 | public Test(String name, String description) {
|
---|
70 | this.name = name;
|
---|
71 | this.description = description;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * Constructor
|
---|
76 | * @param name Name of the test
|
---|
77 | */
|
---|
78 | public Test(String name) {
|
---|
79 | this(name, null);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Initializes any global data used this tester.
|
---|
84 | * @throws Exception When cannot initialize the test
|
---|
85 | */
|
---|
86 | public void initialize() throws Exception {
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Start the test using a given progress monitor
|
---|
91 | *
|
---|
92 | * @param progressMonitor the progress monitor
|
---|
93 | */
|
---|
94 | public void startTest(ProgressMonitor progressMonitor) {
|
---|
95 | if (progressMonitor == null) {
|
---|
96 | this.progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
97 | } else {
|
---|
98 | this.progressMonitor = progressMonitor;
|
---|
99 | }
|
---|
100 | this.progressMonitor.beginTask(tr("Running test {0}", name));
|
---|
101 | errors = new ArrayList<TestError>(30);
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Flag notifying that this test is run over a partial data selection
|
---|
106 | * @param partialSelection Whether the test is on a partial selection data
|
---|
107 | */
|
---|
108 | public void setPartialSelection(boolean partialSelection) {
|
---|
109 | this.partialSelection = partialSelection;
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Gets the validation errors accumulated until this moment.
|
---|
114 | * @return The list of errors
|
---|
115 | */
|
---|
116 | public List<TestError> getErrors() {
|
---|
117 | return errors;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Notification of the end of the test. The tester may perform additional
|
---|
122 | * actions and destroy the used structures
|
---|
123 | */
|
---|
124 | public void endTest() {
|
---|
125 | progressMonitor.finishTask();
|
---|
126 | progressMonitor = null;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Visits all primitives to be tested. These primitives are always visited
|
---|
131 | * in the same order: nodes first, then ways.
|
---|
132 | *
|
---|
133 | * @param selection The primitives to be tested
|
---|
134 | */
|
---|
135 | public void visit(Collection<OsmPrimitive> selection) {
|
---|
136 | progressMonitor.setTicksCount(selection.size());
|
---|
137 | for (OsmPrimitive p : selection) {
|
---|
138 | if (p.isUsable()) {
|
---|
139 | p.visit(this);
|
---|
140 | }
|
---|
141 | progressMonitor.worked(1);
|
---|
142 | }
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public void visit(Node n) {}
|
---|
147 |
|
---|
148 | @Override
|
---|
149 | public void visit(Way w) {}
|
---|
150 |
|
---|
151 | @Override
|
---|
152 | public void visit(Relation r) {}
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Allow the tester to manage its own preferences
|
---|
156 | * @param testPanel The panel to add any preferences component
|
---|
157 | */
|
---|
158 | public void addGui(JPanel testPanel) {
|
---|
159 | checkEnabled = new JCheckBox(name, enabled);
|
---|
160 | checkEnabled.setToolTipText(description);
|
---|
161 | testPanel.add(checkEnabled, GBC.std());
|
---|
162 |
|
---|
163 | GBC a = GBC.eol();
|
---|
164 | a.anchor = GridBagConstraints.EAST;
|
---|
165 | checkBeforeUpload = new JCheckBox();
|
---|
166 | checkBeforeUpload.setSelected(testBeforeUpload);
|
---|
167 | testPanel.add(checkBeforeUpload, a);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Called when the used submits the preferences
|
---|
172 | */
|
---|
173 | public boolean ok() {
|
---|
174 | enabled = checkEnabled.isSelected();
|
---|
175 | testBeforeUpload = checkBeforeUpload.isSelected();
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Fixes the error with the appropriate command
|
---|
181 | *
|
---|
182 | * @param testError
|
---|
183 | * @return The command to fix the error
|
---|
184 | */
|
---|
185 | public Command fixError(TestError testError) {
|
---|
186 | return null;
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Returns true if the given error can be fixed automatically
|
---|
191 | *
|
---|
192 | * @param testError The error to check if can be fixed
|
---|
193 | * @return true if the error can be fixed
|
---|
194 | */
|
---|
195 | public boolean isFixable(TestError testError) {
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Returns true if this plugin must check the uploaded data before uploading
|
---|
201 | * @return true if this plugin must check the uploaded data before uploading
|
---|
202 | */
|
---|
203 | public boolean testBeforeUpload() {
|
---|
204 | return testBeforeUpload;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * Sets the flag that marks an upload check
|
---|
209 | * @param isUpload if true, the test is before upload
|
---|
210 | */
|
---|
211 | public void setBeforeUpload(boolean isUpload) {
|
---|
212 | this.isBeforeUpload = isUpload;
|
---|
213 | }
|
---|
214 |
|
---|
215 | public String getName() {
|
---|
216 | return name;
|
---|
217 | }
|
---|
218 |
|
---|
219 | public boolean isCanceled() {
|
---|
220 | return progressMonitor.isCanceled();
|
---|
221 | }
|
---|
222 | }
|
---|