source: josm/trunk/src/org/openstreetmap/josm/actions/upload/ValidateUploadHook.java@ 13893

Last change on this file since 13893 was 13893, checked in by Don-vip, 6 years ago

fix #16355 - Ignored validation findings reappear on upload (patch by bhatchl)

  • Property svn:eol-style set to native
File size: 6.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.upload;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Dimension;
7import java.awt.GridBagLayout;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import javax.swing.JPanel;
13import javax.swing.JScrollPane;
14
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.data.APIDataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
19import org.openstreetmap.josm.data.validation.OsmValidator;
20import org.openstreetmap.josm.data.validation.Severity;
21import org.openstreetmap.josm.data.validation.Test;
22import org.openstreetmap.josm.data.validation.TestError;
23import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
24import org.openstreetmap.josm.gui.ExtendedDialog;
25import org.openstreetmap.josm.gui.MainApplication;
26import org.openstreetmap.josm.gui.MapFrame;
27import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
28import org.openstreetmap.josm.gui.layer.OsmDataLayer;
29import org.openstreetmap.josm.gui.layer.ValidatorLayer;
30import org.openstreetmap.josm.gui.widgets.HtmlPanel;
31import org.openstreetmap.josm.tools.GBC;
32
33/**
34 * The action that does the validate thing.
35 * <p>
36 * This action iterates through all active tests and give them the data, so that
37 * each one can test it.
38 *
39 * @author frsantos
40 * @since 3669
41 */
42public class ValidateUploadHook implements UploadHook {
43
44 /**
45 * Validate the modified data before uploading
46 */
47 @Override
48 public boolean checkUpload(APIDataSet apiDataSet) {
49
50 OsmValidator.initializeTests();
51 Collection<Test> tests = OsmValidator.getEnabledTests(true);
52 if (tests.isEmpty())
53 return true;
54
55 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
56 v.visit(apiDataSet.getPrimitivesToAdd());
57 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
58
59 List<TestError> errors = new ArrayList<>(30);
60 for (Test test : tests) {
61 test.setBeforeUpload(true);
62 test.setPartialSelection(true);
63 test.startTest(null);
64 test.visit(selection);
65 test.endTest();
66 if (ValidatorPrefHelper.PREF_OTHER.get() && ValidatorPrefHelper.PREF_OTHER_UPLOAD.get()) {
67 errors.addAll(test.getErrors());
68 } else {
69 for (TestError e : test.getErrors()) {
70 if (e.getSeverity() != Severity.OTHER) {
71 errors.add(e);
72 }
73 }
74 }
75 }
76
77 if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
78 int nume = 0;
79 for (TestError error : errors) {
80 List<String> s = new ArrayList<>();
81 s.add(error.getIgnoreState());
82 s.add(error.getIgnoreGroup());
83 s.add(error.getIgnoreSubGroup());
84 for (String state : s) {
85 if (state != null && OsmValidator.hasIgnoredError(state)) {
86 error.setIgnored(true);
87 }
88 }
89 if (!error.isIgnored()) {
90 ++nume;
91 }
92 }
93 if (nume == 0)
94 return true;
95 }
96
97 OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
98 if (editLayer != null) {
99 editLayer.validationErrors.clear();
100 editLayer.validationErrors.addAll(errors);
101 }
102 MapFrame map = MainApplication.getMap();
103 if (map != null) {
104 map.validatorDialog.tree.setErrors(errors);
105 }
106 if (errors.isEmpty())
107 return true;
108
109 return displayErrorScreen(errors);
110 }
111
112 /**
113 * Displays a screen where the actions that would be taken are displayed and
114 * give the user the possibility to cancel the upload.
115 * @param errors The errors displayed in the screen
116 * @return <code>true</code>, if the upload should continue. <code>false</code>
117 * if the user requested cancel.
118 */
119 private static boolean displayErrorScreen(List<TestError> errors) {
120 JPanel p = new JPanel(new GridBagLayout());
121 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
122 errorPanel.expandAll();
123 HtmlPanel pnlMessage = new HtmlPanel();
124 pnlMessage.setText("<html><body>"
125 + tr("The following are results of automatic validation. Try fixing"
126 + " these, but be careful (don''t destroy valid data)."
127 + " When in doubt ignore them.<br>When you"
128 + " cancel this dialog, you can find the entries in the validator"
129 + " side panel to inspect them.")
130 + "<table align=\"center\">"
131 + "<tr><td align=\"left\"><b>"+tr("Errors")
132 + "&nbsp;</b></td><td align=\"left\">"
133 + tr("Usually this should be fixed.")+"</td></tr>"
134 + "<tr><td align=\"left\"><b>"+tr("Warnings")
135 + "&nbsp;</b></td><td align=\"left\">"
136 + tr("Fix these when possible.")+"</td></tr>"
137 + "<tr><td align=\"left\"><b>"+tr("Other")
138 + "&nbsp;</b></td><td align=\"left\">"
139 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
140 + "</table>"
141 );
142 pnlMessage.setPreferredSize(new Dimension(500, 150));
143 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
144 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
145
146 ExtendedDialog ed = new ExtendedDialog(Main.parent,
147 tr("Suspicious data found. Upload anyway?"),
148 tr("Continue upload"), tr("Cancel"))
149 .setButtonIcons("ok", "cancel")
150 .setContent(p);
151
152 if (ed.showDialog().getValue() != 1) {
153 OsmValidator.initializeTests();
154 OsmValidator.initializeErrorLayer();
155 MainApplication.getMap().validatorDialog.unfurlDialog();
156 MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
157 return false;
158 }
159 return true;
160 }
161}
Note: See TracBrowser for help on using the repository browser.