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

Last change on this file since 5608 was 5013, checked in by akks, 12 years ago

Validation on upload and error reporting windows did not fit in 800x600

  • Property svn:eol-style set to native
File size: 5.9 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.actions.upload;
3
4import java.awt.Dimension;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.GridBagLayout;
8import java.util.ArrayList;
9import java.util.Collection;
10import java.util.List;
11
12import javax.swing.JOptionPane;
13import javax.swing.JPanel;
14import javax.swing.JScrollPane;
15
16import org.openstreetmap.josm.Main;
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.preferences.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 */
38public class ValidateUploadHook implements UploadHook
39{
40 /** Serializable ID */
41 private static final long serialVersionUID = -2304521273582574603L;
42
43 /**
44 * Validate the modified data before uploading
45 */
46 public boolean checkUpload(APIDataSet apiDataSet) {
47
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<TestError>(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 (Main.pref.getBoolean(ValidatorPreference.PREF_OTHER, false) &&
64 Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false))
65 {
66 errors.addAll( test.getErrors() );
67 }
68 else {
69 for (TestError e : test.getErrors()) {
70 if (e.getSeverity() != Severity.OTHER) {
71 errors.add(e);
72 }
73 }
74 }
75 }
76 tests = null;
77 if (errors == null || errors.isEmpty())
78 return true;
79
80 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
81 int nume = 0;
82 for (TestError error : errors) {
83 List<String> s = new ArrayList<String>();
84 s.add(error.getIgnoreState());
85 s.add(error.getIgnoreGroup());
86 s.add(error.getIgnoreSubGroup());
87 for (String state : s) {
88 if (state != null && OsmValidator.hasIgnoredError(state)) {
89 error.setIgnored(true);
90 }
91 }
92 if (!error.getIgnored()) {
93 ++nume;
94 }
95 }
96 if (nume == 0)
97 return true;
98 }
99 return displayErrorScreen(errors);
100 }
101
102 /**
103 * Displays a screen where the actions that would be taken are displayed and
104 * give the user the possibility to cancel the upload.
105 * @param errors The errors displayed in the screen
106 * @return <code>true</code>, if the upload should continue. <code>false</code>
107 * if the user requested cancel.
108 */
109 private boolean displayErrorScreen(List<TestError> errors) {
110 JPanel p = new JPanel(new GridBagLayout());
111 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
112 errorPanel.expandAll();
113 HtmlPanel pnlMessage = new HtmlPanel();
114 pnlMessage.setText("<html><body>"
115 + tr("The following are results of automatic validation. Try fixing"
116 + " these, but be careful (don''t destroy valid data)."
117 + " When in doubt ignore them.<br>When you"
118 + " cancel this dialog, you can find the entries in the validator"
119 + " side panel to inspect them.")
120 + "<table align=\"center\">"
121 + "<tr><td align=\"left\"><b>"+tr("Errors")
122 + "&nbsp;</b></td><td align=\"left\">"
123 + tr("Usually this should be fixed.")+"</td></tr>"
124 + "<tr><td align=\"left\"><b>"+tr("Warnings")
125 + "&nbsp;</b></td><td align=\"left\">"
126 + tr("Fix these when possible.")+"</td></tr>"
127 + "<tr><td align=\"left\"><b>"+tr("Other")
128 + "&nbsp;</b></td><td align=\"left\">"
129 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
130 + "</table>"
131 );
132 pnlMessage.setPreferredSize(new Dimension(500, 150));
133 p.add(pnlMessage, GBC.eol());
134 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
135
136 ExtendedDialog ed = new ExtendedDialog(Main.parent,
137 tr("Suspicious data found. Upload anyway?"),
138 new String[] {tr("Continue upload"), tr("Cancel")});
139 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"});
140 ed.setContent(p);
141 ed.showDialog();
142
143 if(ed.getValue() != 1) {
144 OsmValidator.initializeErrorLayer();
145 Main.map.validatorDialog.unfurlDialog();
146 Main.map.validatorDialog.tree.setErrors(errors);
147 Main.main.getCurrentDataSet().fireSelectionChanged();
148 return false;
149 }
150 return true;
151 }
152}
Note: See TracBrowser for help on using the repository browser.