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

Last change on this file since 18960 was 18960, checked in by GerdP, 4 months ago

fix #23397: Improve the results of partial validations

  • pass also parent ways and relations of uploaded/selected objects to the testers, child objects are already added, this allows to find e.g. overlapping polygons problems with tags in members of relations
  • let CrossingWays find a problem if at least one of the crossing ways is in the partial selection.
  • let DuplicatWays find a problem if at least one of the duplicated ways is in the partial selection
  • add code to filter the detected issues so that those issues which are clearly not related to the original list of objects are removed. A few issues from mapcss tests may remain.
  • add new preference validator.partial.add.parents to disable the addition of parent objects, default is enabled
  • add new preference validator.partial.removeIrrelevant to disable the filtering of irrelevant errors, default is enabled
  • duplicated code to call AggregatePrimitivesVisitor was moved to ValidationTask
  • Property svn:eol-style set to native
File size: 5.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.Collection;
9import java.util.HashSet;
10import java.util.List;
11import java.util.concurrent.atomic.AtomicBoolean;
12
13import javax.swing.JPanel;
14import javax.swing.JScrollPane;
15
16import org.openstreetmap.josm.data.APIDataSet;
17import org.openstreetmap.josm.data.osm.OsmPrimitive;
18import org.openstreetmap.josm.data.validation.OsmValidator;
19import org.openstreetmap.josm.data.validation.TestError;
20import org.openstreetmap.josm.data.validation.ValidationTask;
21import org.openstreetmap.josm.gui.ExtendedDialog;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
24import org.openstreetmap.josm.gui.layer.ValidatorLayer;
25import org.openstreetmap.josm.gui.util.GuiHelper;
26import org.openstreetmap.josm.gui.widgets.HtmlPanel;
27import org.openstreetmap.josm.tools.GBC;
28
29/**
30 * The action that does the validate thing.
31 * <p>
32 * This action iterates through all active tests and gives them the data, so that
33 * each one can test it.
34 *
35 * @author frsantos
36 * @since 3669
37 */
38public class ValidateUploadHook implements UploadHook {
39
40 /**
41 * Validate the modified data before uploading
42 * @param apiDataSet contains primitives to be uploaded
43 * @return {@code true} if upload should continue, else false
44 */
45 @Override
46 public boolean checkUpload(APIDataSet apiDataSet) {
47 AtomicBoolean returnCode = new AtomicBoolean();
48 Collection<OsmPrimitive> toCheck = new HashSet<>();
49 toCheck.addAll(apiDataSet.getPrimitivesToAdd());
50 toCheck.addAll(apiDataSet.getPrimitivesToUpdate());
51 OsmValidator.initializeTests();
52 new ValidationTask(errors -> {
53 if (errors.stream().allMatch(TestError::isIgnored)) {
54 returnCode.set(true);
55 } else {
56 // Unfortunately, the progress monitor is not "finished" until after `finish` is called, so we will
57 // have a ProgressMonitor open behind the error screen. Fortunately, the error screen appears in front
58 // of the progress monitor.
59 GuiHelper.runInEDTAndWait(() -> returnCode.set(displayErrorScreen(errors)));
60 }
61 }, null, OsmValidator.getEnabledTests(true), toCheck, null, true).run();
62
63 return returnCode.get();
64 }
65
66 /**
67 * Displays a screen where the actions that would be taken are displayed and
68 * give the user the possibility to cancel the upload.
69 * @param errors The errors displayed in the screen
70 * @return {@code true}, if the upload should continue.<br>
71 * {@code false}, if the user requested cancel.
72 */
73 private static boolean displayErrorScreen(List<TestError> errors) {
74 JPanel p = new JPanel(new GridBagLayout());
75 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
76 errorPanel.expandAll();
77 HtmlPanel pnlMessage = new HtmlPanel();
78 pnlMessage.setText("<html><body>"
79 + tr("The JOSM data validator partially checked the objects to be"
80 + " uploaded and found some problems. Try fixing them, but do not"
81 + " harm valid data. When in doubt ignore the findings.<br>"
82 + " You can see the findings in the Validator Results panel too."
83 + " Further checks on all data can be started from that panel.")
84 + "<table align=\"center\">"
85 + "<tr><td align=\"left\"><b>"+tr("Errors")
86 + "&nbsp;</b></td><td align=\"left\">"
87 + tr("Usually this should be fixed.")+"</td></tr>"
88 + "<tr><td align=\"left\"><b>"+tr("Warnings")
89 + "&nbsp;</b></td><td align=\"left\">"
90 + tr("Fix these when possible.")+"</td></tr>"
91 + "<tr><td align=\"left\"><b>"+tr("Other")
92 + "&nbsp;</b></td><td align=\"left\">"
93 + tr("Informational hints, expect many false entries.")+"</td></tr>"
94 + "</table>"
95 );
96 pnlMessage.setPreferredSize(new Dimension(500, 150));
97 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
98 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
99
100 ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
101 tr("Suspicious data found. Upload anyway?"),
102 tr("Continue upload"), tr("Cancel"))
103 .setButtonIcons("ok", "cancel")
104 .setContent(p);
105 int rc = ed.showDialog().getValue();
106 GuiHelper.destroyComponents(ed, false);
107 ed.dispose();
108 if (rc != 1) {
109 OsmValidator.initializeTests();
110 OsmValidator.initializeErrorLayer();
111 MainApplication.getMap().validatorDialog.unfurlDialog();
112 MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
113 return false;
114 }
115 return true;
116 }
117}
Note: See TracBrowser for help on using the repository browser.