1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
16 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
17 | import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
|
---|
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.AggregatePrimitivesVisitor;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
25 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
26 | import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
|
---|
27 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
28 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
29 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
30 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
31 | import 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 | */
|
---|
42 | public 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 | + " </b></td><td align=\"left\">"
|
---|
121 | + tr("Usually this should be fixed.")+"</td></tr>"
|
---|
122 | + "<tr><td align=\"left\"><b>"+tr("Warnings")
|
---|
123 | + " </b></td><td align=\"left\">"
|
---|
124 | + tr("Fix these when possible.")+"</td></tr>"
|
---|
125 | + "<tr><td align=\"left\"><b>"+tr("Other")
|
---|
126 | + " </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 | }
|
---|