1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.upload;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.JPanel;
|
---|
13 | import javax.swing.JScrollPane;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.data.APIDataSet;
|
---|
17 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
18 | import org.openstreetmap.josm.data.validation.OsmValidator;
|
---|
19 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
20 | import org.openstreetmap.josm.data.validation.Test;
|
---|
21 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
22 | import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
|
---|
25 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
|
---|
27 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
28 | import 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 | */
|
---|
39 | public 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() && ValidatorPreference.PREF_OTHER_UPLOAD.get()) {
|
---|
64 | errors.addAll(test.getErrors());
|
---|
65 | } else {
|
---|
66 | for (TestError e : test.getErrors()) {
|
---|
67 | if (e.getSeverity() != Severity.OTHER) {
|
---|
68 | errors.add(e);
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 | OsmDataLayer editLayer = Main.getLayerManager().getEditLayer();
|
---|
74 | if (editLayer != null) {
|
---|
75 | editLayer.validationErrors.clear();
|
---|
76 | editLayer.validationErrors.addAll(errors);
|
---|
77 | }
|
---|
78 | if (Main.map != null) {
|
---|
79 | Main.map.validatorDialog.tree.setErrors(errors);
|
---|
80 | }
|
---|
81 | if (errors.isEmpty())
|
---|
82 | return true;
|
---|
83 |
|
---|
84 | if (ValidatorPreference.PREF_USE_IGNORE.get()) {
|
---|
85 | int nume = 0;
|
---|
86 | for (TestError error : errors) {
|
---|
87 | List<String> s = new ArrayList<>();
|
---|
88 | s.add(error.getIgnoreState());
|
---|
89 | s.add(error.getIgnoreGroup());
|
---|
90 | s.add(error.getIgnoreSubGroup());
|
---|
91 | for (String state : s) {
|
---|
92 | if (state != null && OsmValidator.hasIgnoredError(state)) {
|
---|
93 | error.setIgnored(true);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | if (!error.isIgnored()) {
|
---|
97 | ++nume;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | if (nume == 0)
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | return displayErrorScreen(errors);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /**
|
---|
107 | * Displays a screen where the actions that would be taken are displayed and
|
---|
108 | * give the user the possibility to cancel the upload.
|
---|
109 | * @param errors The errors displayed in the screen
|
---|
110 | * @return <code>true</code>, if the upload should continue. <code>false</code>
|
---|
111 | * if the user requested cancel.
|
---|
112 | */
|
---|
113 | private static boolean displayErrorScreen(List<TestError> errors) {
|
---|
114 | JPanel p = new JPanel(new GridBagLayout());
|
---|
115 | ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
|
---|
116 | errorPanel.expandAll();
|
---|
117 | HtmlPanel pnlMessage = new HtmlPanel();
|
---|
118 | pnlMessage.setText("<html><body>"
|
---|
119 | + tr("The following are results of automatic validation. Try fixing"
|
---|
120 | + " these, but be careful (don''t destroy valid data)."
|
---|
121 | + " When in doubt ignore them.<br>When you"
|
---|
122 | + " cancel this dialog, you can find the entries in the validator"
|
---|
123 | + " side panel to inspect them.")
|
---|
124 | + "<table align=\"center\">"
|
---|
125 | + "<tr><td align=\"left\"><b>"+tr("Errors")
|
---|
126 | + " </b></td><td align=\"left\">"
|
---|
127 | + tr("Usually this should be fixed.")+"</td></tr>"
|
---|
128 | + "<tr><td align=\"left\"><b>"+tr("Warnings")
|
---|
129 | + " </b></td><td align=\"left\">"
|
---|
130 | + tr("Fix these when possible.")+"</td></tr>"
|
---|
131 | + "<tr><td align=\"left\"><b>"+tr("Other")
|
---|
132 | + " </b></td><td align=\"left\">"
|
---|
133 | + tr("Informational warnings, expect many false entries.")+"</td></tr>"
|
---|
134 | + "</table>"
|
---|
135 | );
|
---|
136 | pnlMessage.setPreferredSize(new Dimension(500, 150));
|
---|
137 | p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
138 | p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
|
---|
139 |
|
---|
140 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
141 | tr("Suspicious data found. Upload anyway?"),
|
---|
142 | new String[] {tr("Continue upload"), tr("Cancel")});
|
---|
143 | ed.setButtonIcons(new String[] {"ok.png", "cancel.png"});
|
---|
144 | ed.setContent(p);
|
---|
145 | ed.showDialog();
|
---|
146 |
|
---|
147 | if (ed.getValue() != 1) {
|
---|
148 | OsmValidator.initializeTests();
|
---|
149 | OsmValidator.initializeErrorLayer();
|
---|
150 | Main.map.validatorDialog.unfurlDialog();
|
---|
151 | Main.getLayerManager().getEditDataSet().fireSelectionChanged();
|
---|
152 | return false;
|
---|
153 | }
|
---|
154 | return true;
|
---|
155 | }
|
---|
156 | }
|
---|