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

Last change on this file since 3671 was 3671, checked in by bastiK, 13 years ago

adapt coding style (to some degree); there shouldn't be any semantic changes in this commit

  • Property svn:eol-style set to native
File size: 4.4 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.GridBagLayout;
7import java.util.ArrayList;
8import java.util.Collection;
9import java.util.List;
10
11import javax.swing.JOptionPane;
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.AgregatePrimitivesVisitor;
23import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
24import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
25import org.openstreetmap.josm.tools.GBC;
26
27/**
28 * The action that does the validate thing.
29 * <p>
30 * This action iterates through all active tests and give them the data, so that
31 * each one can test it.
32 *
33 * @author frsantos
34 */
35public class ValidateUploadHook implements UploadHook
36{
37 /** Serializable ID */
38 private static final long serialVersionUID = -2304521273582574603L;
39
40 /**
41 * Validate the modified data before uploading
42 */
43 public boolean checkUpload(APIDataSet apiDataSet) {
44
45 Collection<Test> tests = OsmValidator.getEnabledTests(true);
46 if (tests.isEmpty())
47 return true;
48
49 AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor();
50 v.visit(apiDataSet.getPrimitivesToAdd());
51 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
52
53 List<TestError> errors = new ArrayList<TestError>(30);
54 for (Test test : tests) {
55 test.setBeforeUpload(true);
56 test.setPartialSelection(true);
57 test.startTest(null);
58 test.visit(selection);
59 test.endTest();
60 if (Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false)) {
61 errors.addAll( test.getErrors() );
62 }
63 else {
64 for (TestError e : test.getErrors()) {
65 if (e.getSeverity() != Severity.OTHER) {
66 errors.add(e);
67 }
68 }
69 }
70 }
71 tests = null;
72 if (errors == null || errors.isEmpty())
73 return true;
74
75 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
76 int nume = 0;
77 for (TestError error : errors) {
78 List<String> s = new ArrayList<String>();
79 s.add(error.getIgnoreState());
80 s.add(error.getIgnoreGroup());
81 s.add(error.getIgnoreSubGroup());
82 for (String state : s) {
83 if (state != null && OsmValidator.hasIgnoredError(state)) {
84 error.setIgnored(true);
85 }
86 }
87 if (!error.getIgnored()) {
88 ++nume;
89 }
90 }
91 if (nume == 0)
92 return true;
93 }
94 return displayErrorScreen(errors);
95 }
96
97 /**
98 * Displays a screen where the actions that would be taken are displayed and
99 * give the user the possibility to cancel the upload.
100 * @param errors The errors displayed in the screen
101 * @return <code>true</code>, if the upload should continue. <code>false</code>
102 * if the user requested cancel.
103 */
104 private boolean displayErrorScreen(List<TestError> errors) {
105 JPanel p = new JPanel(new GridBagLayout());
106 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
107 errorPanel.expandAll();
108 p.add(new JScrollPane(errorPanel), GBC.eol());
109
110 int res = JOptionPane.showConfirmDialog(Main.parent, p,
111 tr("Data with errors. Upload anyway?"),
112 JOptionPane.YES_NO_OPTION,
113 JOptionPane.QUESTION_MESSAGE);
114 if (res == JOptionPane.NO_OPTION) {
115 OsmValidator.initializeErrorLayer();
116 Main.map.validatorDialog.unfurlDialog();
117 Main.map.validatorDialog.tree.setErrors(errors);
118 Main.main.getCurrentDataSet().fireSelectionChanged();
119 }
120 return res == JOptionPane.YES_OPTION;
121 }
122}
Note: See TracBrowser for help on using the repository browser.