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

Last change on this file since 7509 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 6.0 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 OsmValidator.initializeTests();
48 Collection<Test> tests = OsmValidator.getEnabledTests(true);
49 if (tests.isEmpty())
50 return true;
51
52 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
53 v.visit(apiDataSet.getPrimitivesToAdd());
54 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
55
56 List<TestError> errors = new ArrayList<>(30);
57 for (Test test : tests) {
58 test.setBeforeUpload(true);
59 test.setPartialSelection(true);
60 test.startTest(null);
61 test.visit(selection);
62 test.endTest();
63 if (ValidatorPreference.PREF_OTHER.get() &&
64 Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false)) {
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 tests = null;
75 OsmDataLayer editLayer = Main.main.getEditLayer();
76 editLayer.validationErrors.clear();
77 editLayer.validationErrors.addAll(errors);
78 Main.map.validatorDialog.tree.setErrors(errors);
79 if (errors == null || errors.isEmpty())
80 return true;
81
82 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
83 int nume = 0;
84 for (TestError error : errors) {
85 List<String> s = new ArrayList<>();
86 s.add(error.getIgnoreState());
87 s.add(error.getIgnoreGroup());
88 s.add(error.getIgnoreSubGroup());
89 for (String state : s) {
90 if (state != null && OsmValidator.hasIgnoredError(state)) {
91 error.setIgnored(true);
92 }
93 }
94 if (!error.getIgnored()) {
95 ++nume;
96 }
97 }
98 if (nume == 0)
99 return true;
100 }
101 return displayErrorScreen(errors);
102 }
103
104 /**
105 * Displays a screen where the actions that would be taken are displayed and
106 * give the user the possibility to cancel the upload.
107 * @param errors The errors displayed in the screen
108 * @return <code>true</code>, if the upload should continue. <code>false</code>
109 * if the user requested cancel.
110 */
111 private boolean displayErrorScreen(List<TestError> errors) {
112 JPanel p = new JPanel(new GridBagLayout());
113 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
114 errorPanel.expandAll();
115 HtmlPanel pnlMessage = new HtmlPanel();
116 pnlMessage.setText("<html><body>"
117 + tr("The following are results of automatic validation. Try fixing"
118 + " these, but be careful (don''t destroy valid data)."
119 + " When in doubt ignore them.<br>When you"
120 + " cancel this dialog, you can find the entries in the validator"
121 + " side panel to inspect them.")
122 + "<table align=\"center\">"
123 + "<tr><td align=\"left\"><b>"+tr("Errors")
124 + "&nbsp;</b></td><td align=\"left\">"
125 + tr("Usually this should be fixed.")+"</td></tr>"
126 + "<tr><td align=\"left\"><b>"+tr("Warnings")
127 + "&nbsp;</b></td><td align=\"left\">"
128 + tr("Fix these when possible.")+"</td></tr>"
129 + "<tr><td align=\"left\"><b>"+tr("Other")
130 + "&nbsp;</b></td><td align=\"left\">"
131 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
132 + "</table>"
133 );
134 pnlMessage.setPreferredSize(new Dimension(500, 150));
135 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
136 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
137
138 ExtendedDialog ed = new ExtendedDialog(Main.parent,
139 tr("Suspicious data found. Upload anyway?"),
140 new String[] {tr("Continue upload"), tr("Cancel")});
141 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"});
142 ed.setContent(p);
143 ed.showDialog();
144
145 if (ed.getValue() != 1) {
146 OsmValidator.initializeTests();
147 OsmValidator.initializeErrorLayer();
148 Main.map.validatorDialog.unfurlDialog();
149 Main.main.getCurrentDataSet().fireSelectionChanged();
150 return false;
151 }
152 return true;
153 }
154}
Note: See TracBrowser for help on using the repository browser.