1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import javax.swing.JOptionPane;
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
19 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
20 | import org.openstreetmap.josm.data.validation.Test;
|
---|
21 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
22 | import org.openstreetmap.josm.data.validation.util.AgregatePrimitivesVisitor;
|
---|
23 | import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
|
---|
25 | import org.openstreetmap.josm.tools.GBC;
|
---|
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 ValidateUploadHook implements UploadHook
|
---|
36 | {
|
---|
37 | /** Serializable ID */
|
---|
38 | private static final long serialVersionUID = -2304521273582574603L;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Validate the modified data before uploading
|
---|
42 | */
|
---|
43 | public boolean checkUpload(APIDataSet apiDataSet) {
|
---|
44 |
|
---|
45 | Collection<Test> tests = OsmValidator.getEnabledTests(true);
|
---|
46 | if (tests.isEmpty())
|
---|
47 | return true;
|
---|
48 |
|
---|
49 | AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor();
|
---|
50 | v.visit(apiDataSet.getPrimitivesToAdd());
|
---|
51 | Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
|
---|
52 |
|
---|
53 | List<TestError> errors = new ArrayList<TestError>(30);
|
---|
54 | for (Test test : tests) {
|
---|
55 | test.setBeforeUpload(true);
|
---|
56 | test.setPartialSelection(true);
|
---|
57 | test.startTest(null);
|
---|
58 | test.visit(selection);
|
---|
59 | test.endTest();
|
---|
60 | if (Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false)) {
|
---|
61 | errors.addAll( test.getErrors() );
|
---|
62 | }
|
---|
63 | else {
|
---|
64 | for (TestError e : test.getErrors()) {
|
---|
65 | if (e.getSeverity() != Severity.OTHER) {
|
---|
66 | errors.add(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|
71 | tests = null;
|
---|
72 | if (errors == null || errors.isEmpty())
|
---|
73 | return true;
|
---|
74 |
|
---|
75 | if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
|
---|
76 | int nume = 0;
|
---|
77 | for (TestError error : errors) {
|
---|
78 | List<String> s = new ArrayList<String>();
|
---|
79 | s.add(error.getIgnoreState());
|
---|
80 | s.add(error.getIgnoreGroup());
|
---|
81 | s.add(error.getIgnoreSubGroup());
|
---|
82 | for (String state : s) {
|
---|
83 | if (state != null && OsmValidator.hasIgnoredError(state)) {
|
---|
84 | error.setIgnored(true);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if (!error.getIgnored()) {
|
---|
88 | ++nume;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | if (nume == 0)
|
---|
92 | return true;
|
---|
93 | }
|
---|
94 | return displayErrorScreen(errors);
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Displays a screen where the actions that would be taken are displayed and
|
---|
99 | * give the user the possibility to cancel the upload.
|
---|
100 | * @param errors The errors displayed in the screen
|
---|
101 | * @return <code>true</code>, if the upload should continue. <code>false</code>
|
---|
102 | * if the user requested cancel.
|
---|
103 | */
|
---|
104 | private boolean displayErrorScreen(List<TestError> errors) {
|
---|
105 | JPanel p = new JPanel(new GridBagLayout());
|
---|
106 | ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
|
---|
107 | errorPanel.expandAll();
|
---|
108 | p.add(new JScrollPane(errorPanel), GBC.eol());
|
---|
109 |
|
---|
110 | int res = JOptionPane.showConfirmDialog(Main.parent, p,
|
---|
111 | tr("Data with errors. Upload anyway?"),
|
---|
112 | JOptionPane.YES_NO_OPTION,
|
---|
113 | JOptionPane.QUESTION_MESSAGE);
|
---|
114 | if (res == JOptionPane.NO_OPTION) {
|
---|
115 | OsmValidator.initializeErrorLayer();
|
---|
116 | Main.map.validatorDialog.unfurlDialog();
|
---|
117 | Main.map.validatorDialog.tree.setErrors(errors);
|
---|
118 | Main.main.getCurrentDataSet().fireSelectionChanged();
|
---|
119 | }
|
---|
120 | return res == JOptionPane.YES_OPTION;
|
---|
121 | }
|
---|
122 | }
|
---|