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

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

see #13001 - replace calls to Main.main.getCurrentDataSet() by Main.getLayerManager().getEditDataSet()

  • 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.actions.JosmAction;
17import org.openstreetmap.josm.data.APIDataSet;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
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.dialogs.validator.ValidatorTreePanel;
26import org.openstreetmap.josm.gui.layer.OsmDataLayer;
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() &&
65 Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false)) {
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 OsmDataLayer editLayer = JosmAction.getEditLayer();
76 if (editLayer != null) {
77 editLayer.validationErrors.clear();
78 editLayer.validationErrors.addAll(errors);
79 }
80 if (Main.map != null) {
81 Main.map.validatorDialog.tree.setErrors(errors);
82 }
83 if (errors.isEmpty())
84 return true;
85
86 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
87 int nume = 0;
88 for (TestError error : errors) {
89 List<String> s = new ArrayList<>();
90 s.add(error.getIgnoreState());
91 s.add(error.getIgnoreGroup());
92 s.add(error.getIgnoreSubGroup());
93 for (String state : s) {
94 if (state != null && OsmValidator.hasIgnoredError(state)) {
95 error.setIgnored(true);
96 }
97 }
98 if (!error.isIgnored()) {
99 ++nume;
100 }
101 }
102 if (nume == 0)
103 return true;
104 }
105 return displayErrorScreen(errors);
106 }
107
108 /**
109 * Displays a screen where the actions that would be taken are displayed and
110 * give the user the possibility to cancel the upload.
111 * @param errors The errors displayed in the screen
112 * @return <code>true</code>, if the upload should continue. <code>false</code>
113 * if the user requested cancel.
114 */
115 private static boolean displayErrorScreen(List<TestError> errors) {
116 JPanel p = new JPanel(new GridBagLayout());
117 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
118 errorPanel.expandAll();
119 HtmlPanel pnlMessage = new HtmlPanel();
120 pnlMessage.setText("<html><body>"
121 + tr("The following are results of automatic validation. Try fixing"
122 + " these, but be careful (don''t destroy valid data)."
123 + " When in doubt ignore them.<br>When you"
124 + " cancel this dialog, you can find the entries in the validator"
125 + " side panel to inspect them.")
126 + "<table align=\"center\">"
127 + "<tr><td align=\"left\"><b>"+tr("Errors")
128 + "&nbsp;</b></td><td align=\"left\">"
129 + tr("Usually this should be fixed.")+"</td></tr>"
130 + "<tr><td align=\"left\"><b>"+tr("Warnings")
131 + "&nbsp;</b></td><td align=\"left\">"
132 + tr("Fix these when possible.")+"</td></tr>"
133 + "<tr><td align=\"left\"><b>"+tr("Other")
134 + "&nbsp;</b></td><td align=\"left\">"
135 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
136 + "</table>"
137 );
138 pnlMessage.setPreferredSize(new Dimension(500, 150));
139 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
140 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
141
142 ExtendedDialog ed = new ExtendedDialog(Main.parent,
143 tr("Suspicious data found. Upload anyway?"),
144 new String[] {tr("Continue upload"), tr("Cancel")});
145 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"});
146 ed.setContent(p);
147 ed.showDialog();
148
149 if (ed.getValue() != 1) {
150 OsmValidator.initializeTests();
151 OsmValidator.initializeErrorLayer();
152 Main.map.validatorDialog.unfurlDialog();
153 Main.getLayerManager().getEditDataSet().fireSelectionChanged();
154 return false;
155 }
156 return true;
157 }
158}
Note: See TracBrowser for help on using the repository browser.