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

Last change on this file since 12212 was 12212, checked in by michael2402, 7 years ago

ValidateUploadHook: Invalidate the error layer instead of relying on a selection change to trigger a map update

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