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

Last change on this file since 17436 was 17436, checked in by GerdP, 3 years ago

see #20342: Validator results not cleared

  • make sure that validator tree is rebuild even if all found errors are ignored (regression from r13893)
  • Property svn:eol-style set to native
File size: 6.0 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.data.APIDataSet;
16import org.openstreetmap.josm.data.osm.OsmPrimitive;
17import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
18import org.openstreetmap.josm.data.validation.OsmValidator;
19import org.openstreetmap.josm.data.validation.Severity;
20import org.openstreetmap.josm.data.validation.Test;
21import org.openstreetmap.josm.data.validation.TestError;
22import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
23import org.openstreetmap.josm.gui.ExtendedDialog;
24import org.openstreetmap.josm.gui.MainApplication;
25import org.openstreetmap.josm.gui.MapFrame;
26import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.gui.layer.ValidatorLayer;
29import org.openstreetmap.josm.gui.util.GuiHelper;
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 * @param apiDataSet contains primitives to be uploaded
47 * @return true if upload should continue, else false
48 */
49 @Override
50 public boolean checkUpload(APIDataSet apiDataSet) {
51
52 OsmValidator.initializeTests();
53 Collection<Test> tests = OsmValidator.getEnabledTests(true);
54 if (tests.isEmpty())
55 return true;
56
57 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
58 v.visit(apiDataSet.getPrimitivesToAdd());
59 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
60
61 List<TestError> errors = new ArrayList<>(30);
62 for (Test test : tests) {
63 test.setBeforeUpload(true);
64 test.setPartialSelection(true);
65 test.startTest(null);
66 test.visit(selection);
67 test.endTest();
68 if (ValidatorPrefHelper.PREF_OTHER.get() && ValidatorPrefHelper.PREF_OTHER_UPLOAD.get()) {
69 errors.addAll(test.getErrors());
70 } else {
71 for (TestError e : test.getErrors()) {
72 if (e.getSeverity() != Severity.OTHER) {
73 errors.add(e);
74 }
75 }
76 }
77 test.clear();
78 test.setBeforeUpload(false);
79 }
80
81 if (Boolean.TRUE.equals(ValidatorPrefHelper.PREF_USE_IGNORE.get())) {
82 errors.forEach(TestError::updateIgnored);
83 }
84
85 OsmDataLayer editLayer = MainApplication.getLayerManager().getEditLayer();
86 if (editLayer != null) {
87 editLayer.validationErrors.clear();
88 editLayer.validationErrors.addAll(errors);
89 }
90 MapFrame map = MainApplication.getMap();
91 if (map != null) {
92 map.validatorDialog.tree.setErrors(errors);
93 }
94 if (errors.stream().allMatch(TestError::isIgnored))
95 return true;
96
97 return displayErrorScreen(errors);
98 }
99
100 /**
101 * Displays a screen where the actions that would be taken are displayed and
102 * give the user the possibility to cancel the upload.
103 * @param errors The errors displayed in the screen
104 * @return <code>true</code>, if the upload should continue. <code>false</code>
105 * if the user requested cancel.
106 */
107 private static boolean displayErrorScreen(List<TestError> errors) {
108 JPanel p = new JPanel(new GridBagLayout());
109 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
110 errorPanel.expandAll();
111 HtmlPanel pnlMessage = new HtmlPanel();
112 pnlMessage.setText("<html><body>"
113 + tr("The following are results of automatic validation. Try fixing"
114 + " these, but be careful (don''t destroy valid data)."
115 + " When in doubt ignore them.<br>When you"
116 + " cancel this dialog, you can find the entries in the validator"
117 + " side panel to inspect them.")
118 + "<table align=\"center\">"
119 + "<tr><td align=\"left\"><b>"+tr("Errors")
120 + "&nbsp;</b></td><td align=\"left\">"
121 + tr("Usually this should be fixed.")+"</td></tr>"
122 + "<tr><td align=\"left\"><b>"+tr("Warnings")
123 + "&nbsp;</b></td><td align=\"left\">"
124 + tr("Fix these when possible.")+"</td></tr>"
125 + "<tr><td align=\"left\"><b>"+tr("Other")
126 + "&nbsp;</b></td><td align=\"left\">"
127 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
128 + "</table>"
129 );
130 pnlMessage.setPreferredSize(new Dimension(500, 150));
131 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
132 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
133
134 ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
135 tr("Suspicious data found. Upload anyway?"),
136 tr("Continue upload"), tr("Cancel"))
137 .setButtonIcons("ok", "cancel")
138 .setContent(p);
139 int rc = ed.showDialog().getValue();
140 GuiHelper.destroyComponents(ed, false);
141 ed.dispose();
142 if (rc != 1) {
143 OsmValidator.initializeTests();
144 OsmValidator.initializeErrorLayer();
145 MainApplication.getMap().validatorDialog.unfurlDialog();
146 MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
147 return false;
148 }
149 return true;
150 }
151}
Note: See TracBrowser for help on using the repository browser.