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 | {
|
---|
71 | this.name = name;
|
---|
72 | this.description = description;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Constructor
|
---|
77 | * @param name Name of the test
|
---|
78 | */
|
---|
79 | public Test(String name)
|
---|
80 | {
|
---|
81 | this(name, null);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Initializes any global data used this tester.
|
---|
86 | * @throws Exception When cannot initialize the test
|
---|
87 | */
|
---|
88 | public void initialize() throws Exception {}
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Start the test using a given progress monitor
|
---|
92 | *
|
---|
93 | * @param progressMonitor the progress monitor
|
---|
94 | */
|
---|
95 | public void startTest(ProgressMonitor progressMonitor) {
|
---|
96 | if (progressMonitor == null) {
|
---|
97 | this.progressMonitor = NullProgressMonitor.INSTANCE;
|
---|
98 | } else {
|
---|
99 | this.progressMonitor = progressMonitor;
|
---|
100 | }
|
---|
101 | this.progressMonitor.beginTask(tr("Running test {0}", name));
|
---|
102 | errors = new ArrayList<TestError>(30);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Flag notifying that this test is run over a partial data selection
|
---|
107 | * @param partialSelection Whether the test is on a partial selection data
|
---|
108 | */
|
---|
109 | public void setPartialSelection(boolean partialSelection)
|
---|
110 | {
|
---|
111 | this.partialSelection = partialSelection;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Gets the validation errors accumulated until this moment.
|
---|
116 | * @return The list of errors
|
---|
117 | */
|
---|
118 | public List<TestError> getErrors()
|
---|
119 | {
|
---|
120 | return errors;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Notification of the end of the test. The tester may perform additional
|
---|
125 | * actions and destroy the used structures
|
---|
126 | */
|
---|
127 | public void endTest() {
|
---|
128 | progressMonitor.finishTask();
|
---|
129 | progressMonitor = null;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Visits all primitives to be tested. These primitives are always visited
|
---|
134 | * in the same order: nodes first, then ways.
|
---|
135 | *
|
---|
136 | * @param selection The primitives to be tested
|
---|
137 | */
|
---|
138 | public void visit(Collection<OsmPrimitive> selection)
|
---|
139 | {
|
---|
140 | progressMonitor.setTicksCount(selection.size());
|
---|
141 | for (OsmPrimitive p : selection) {
|
---|
142 | if( p.isUsable() )
|
---|
143 | p.visit(this);
|
---|
144 | progressMonitor.worked(1);
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | public void visit(Node n) {}
|
---|
149 |
|
---|
150 | public void visit(Way w) {}
|
---|
151 |
|
---|
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 | {
|
---|
160 | checkEnabled = new JCheckBox(name, enabled);
|
---|
161 | checkEnabled.setToolTipText(description);
|
---|
162 | testPanel.add(checkEnabled, GBC.std());
|
---|
163 |
|
---|
164 | GBC a = GBC.eol();
|
---|
165 | a.anchor = GridBagConstraints.EAST;
|
---|
166 | checkBeforeUpload = new JCheckBox();
|
---|
167 | checkBeforeUpload.setSelected(testBeforeUpload);
|
---|
168 | testPanel.add(checkBeforeUpload, a);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Called when the used submits the preferences
|
---|
173 | */
|
---|
174 | public boolean ok()
|
---|
175 | {
|
---|
176 | enabled = checkEnabled.isSelected();
|
---|
177 | testBeforeUpload = checkBeforeUpload.isSelected();
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Fixes the error with the appropiate command
|
---|
183 | *
|
---|
184 | * @param testError
|
---|
185 | * @return The command to fix the error
|
---|
186 | */
|
---|
187 | public Command fixError(TestError testError)
|
---|
188 | {
|
---|
189 | return null;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Returns true if the given error can be fixed automatically
|
---|
194 | *
|
---|
195 | * @param testError The error to check if can be fixed
|
---|
196 | * @return true if the error can be fixed
|
---|
197 | */
|
---|
198 | public boolean isFixable(TestError testError)
|
---|
199 | {
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Returns true if this plugin must check the uploaded data before uploading
|
---|
205 | * @return true if this plugin must check the uploaded data before uploading
|
---|
206 | */
|
---|
207 | public boolean testBeforeUpload()
|
---|
208 | {
|
---|
209 | return testBeforeUpload;
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Sets the flag that marks an upload check
|
---|
214 | * @param isUpload if true, the test is before upload
|
---|
215 | */
|
---|
216 | public void setBeforeUpload(boolean isUpload)
|
---|
217 | {
|
---|
218 | this.isBeforeUpload = isUpload;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public String getName() {
|
---|
222 | return name;
|
---|
223 | }
|
---|
224 | }
|
---|