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

Last change on this file since 14153 was 14153, checked in by Don-vip, 6 years ago

see #15229 - deprecate Main.parent and Main itself

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