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

Last change on this file since 4310 was 4310, checked in by stoecker, 13 years ago

fix #6680, fix #6677 - i18n issues

  • Property svn:eol-style set to native
File size: 5.8 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.AggregatePrimitivesVisitor;
23import org.openstreetmap.josm.gui.ExtendedDialog;
24import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
25import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
26import org.openstreetmap.josm.gui.widgets.HtmlPanel;
27import org.openstreetmap.josm.tools.GBC;
28
29/**
30 * The action that does the validate thing.
31 * <p>
32 * This action iterates through all active tests and give them the data, so that
33 * each one can test it.
34 *
35 * @author frsantos
36 */
37public class ValidateUploadHook implements UploadHook
38{
39 /** Serializable ID */
40 private static final long serialVersionUID = -2304521273582574603L;
41
42 /**
43 * Validate the modified data before uploading
44 */
45 public boolean checkUpload(APIDataSet apiDataSet) {
46
47 Collection<Test> tests = OsmValidator.getEnabledTests(true);
48 if (tests.isEmpty())
49 return true;
50
51 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor();
52 v.visit(apiDataSet.getPrimitivesToAdd());
53 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate());
54
55 List<TestError> errors = new ArrayList<TestError>(30);
56 for (Test test : tests) {
57 test.setBeforeUpload(true);
58 test.setPartialSelection(true);
59 test.startTest(null);
60 test.visit(selection);
61 test.endTest();
62 if (Main.pref.getBoolean(ValidatorPreference.PREF_OTHER, false) &&
63 Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false))
64 {
65 errors.addAll( test.getErrors() );
66 }
67 else {
68 for (TestError e : test.getErrors()) {
69 if (e.getSeverity() != Severity.OTHER) {
70 errors.add(e);
71 }
72 }
73 }
74 }
75 tests = null;
76 if (errors == null || errors.isEmpty())
77 return true;
78
79 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) {
80 int nume = 0;
81 for (TestError error : errors) {
82 List<String> s = new ArrayList<String>();
83 s.add(error.getIgnoreState());
84 s.add(error.getIgnoreGroup());
85 s.add(error.getIgnoreSubGroup());
86 for (String state : s) {
87 if (state != null && OsmValidator.hasIgnoredError(state)) {
88 error.setIgnored(true);
89 }
90 }
91 if (!error.getIgnored()) {
92 ++nume;
93 }
94 }
95 if (nume == 0)
96 return true;
97 }
98 return displayErrorScreen(errors);
99 }
100
101 /**
102 * Displays a screen where the actions that would be taken are displayed and
103 * give the user the possibility to cancel the upload.
104 * @param errors The errors displayed in the screen
105 * @return <code>true</code>, if the upload should continue. <code>false</code>
106 * if the user requested cancel.
107 */
108 private boolean displayErrorScreen(List<TestError> errors) {
109 JPanel p = new JPanel(new GridBagLayout());
110 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
111 errorPanel.expandAll();
112 HtmlPanel pnlMessage = new HtmlPanel();
113 pnlMessage.setText("<html><body>"
114 + tr("The following are results of automatic validation. Try fixing"
115 + " these, but be careful (don''t destroy valid data)."
116 + " When in doubt ignore them.<br>When you"
117 + " cancel this dialog, you can find the entries in the validator"
118 + " side panel to inspect them.")
119 + "<table align=\"center\">"
120 + "<tr><td align=\"left\"><b>"+tr("Errors")
121 + "&nbsp;</b></td><td align=\"left\">"
122 + tr("Usually this should be fixed.")+"</td></tr>"
123 + "<tr><td align=\"left\"><b>"+tr("Warnings")
124 + "&nbsp;</b></td><td align=\"left\">"
125 + tr("Fix these when possible.")+"</td></tr>"
126 + "<tr><td align=\"left\"><b>"+tr("Other")
127 + "&nbsp;</b></td><td align=\"left\">"
128 + tr("Informational warnings, expect many false entries.")+"</td></tr>"
129 + "</table>"
130 );
131 p.add(pnlMessage, GBC.eol());
132 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
133
134 ExtendedDialog ed = new ExtendedDialog(Main.parent,
135 tr("Suspicious data found. Upload anyway?"),
136 new String[] {tr("Continue upload"), tr("Cancel")});
137 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"});
138 ed.setContent(p);
139 ed.showDialog();
140
141 if(ed.getValue() != 1) {
142 OsmValidator.initializeErrorLayer();
143 Main.map.validatorDialog.unfurlDialog();
144 Main.map.validatorDialog.tree.setErrors(errors);
145 Main.main.getCurrentDataSet().fireSelectionChanged();
146 return false;
147 }
148 return true;
149 }
150}
Note: See TracBrowser for help on using the repository browser.