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

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

see #15182 - code refactoring to avoid dependence on GUI packages from Preferences

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