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

Last change on this file since 6636 was 6636, checked in by simon04, 10 years ago

see #9414 - MapCSS validator: skip tests with informational severity if that isn't enabled

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