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.Collection;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.List;
|
---|
11 | import java.util.concurrent.atomic.AtomicBoolean;
|
---|
12 |
|
---|
13 | import javax.swing.JPanel;
|
---|
14 | import javax.swing.JScrollPane;
|
---|
15 |
|
---|
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.TestError;
|
---|
20 | import org.openstreetmap.josm.data.validation.ValidationTask;
|
---|
21 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
22 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
23 | import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel;
|
---|
24 | import org.openstreetmap.josm.gui.layer.ValidatorLayer;
|
---|
25 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
26 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
---|
27 | import 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 gives them the data, so that
|
---|
33 | * each one can test it.
|
---|
34 | *
|
---|
35 | * @author frsantos
|
---|
36 | * @since 3669
|
---|
37 | */
|
---|
38 | public class ValidateUploadHook implements UploadHook {
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Validate the modified data before uploading
|
---|
42 | * @param apiDataSet contains primitives to be uploaded
|
---|
43 | * @return {@code true} if upload should continue, else false
|
---|
44 | */
|
---|
45 | @Override
|
---|
46 | public boolean checkUpload(APIDataSet apiDataSet) {
|
---|
47 | AtomicBoolean returnCode = new AtomicBoolean();
|
---|
48 | Collection<OsmPrimitive> toCheck = new HashSet<>();
|
---|
49 | toCheck.addAll(apiDataSet.getPrimitivesToAdd());
|
---|
50 | toCheck.addAll(apiDataSet.getPrimitivesToUpdate());
|
---|
51 | OsmValidator.initializeTests();
|
---|
52 | new ValidationTask(errors -> {
|
---|
53 | if (errors.stream().allMatch(TestError::isIgnored)) {
|
---|
54 | returnCode.set(true);
|
---|
55 | } else {
|
---|
56 | // Unfortunately, the progress monitor is not "finished" until after `finish` is called, so we will
|
---|
57 | // have a ProgressMonitor open behind the error screen. Fortunately, the error screen appears in front
|
---|
58 | // of the progress monitor.
|
---|
59 | GuiHelper.runInEDTAndWait(() -> returnCode.set(displayErrorScreen(errors)));
|
---|
60 | }
|
---|
61 | }, null, OsmValidator.getEnabledTests(true), toCheck, null, true).run();
|
---|
62 |
|
---|
63 | return returnCode.get();
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Displays a screen where the actions that would be taken are displayed and
|
---|
68 | * give the user the possibility to cancel the upload.
|
---|
69 | * @param errors The errors displayed in the screen
|
---|
70 | * @return {@code true}, if the upload should continue.<br>
|
---|
71 | * {@code false}, if the user requested cancel.
|
---|
72 | */
|
---|
73 | private static boolean displayErrorScreen(List<TestError> errors) {
|
---|
74 | JPanel p = new JPanel(new GridBagLayout());
|
---|
75 | ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors);
|
---|
76 | errorPanel.expandAll();
|
---|
77 | HtmlPanel pnlMessage = new HtmlPanel();
|
---|
78 | pnlMessage.setText("<html><body>"
|
---|
79 | + tr("The JOSM data validator partially checked the objects to be"
|
---|
80 | + " uploaded and found some problems. Try fixing them, but do not"
|
---|
81 | + " harm valid data. When in doubt ignore the findings.<br>"
|
---|
82 | + " You can see the findings in the Validator Results panel too."
|
---|
83 | + " Further checks on all data can be started from that panel.")
|
---|
84 | + "<table align=\"center\">"
|
---|
85 | + "<tr><td align=\"left\"><b>"+tr("Errors")
|
---|
86 | + " </b></td><td align=\"left\">"
|
---|
87 | + tr("Usually this should be fixed.")+"</td></tr>"
|
---|
88 | + "<tr><td align=\"left\"><b>"+tr("Warnings")
|
---|
89 | + " </b></td><td align=\"left\">"
|
---|
90 | + tr("Fix these when possible.")+"</td></tr>"
|
---|
91 | + "<tr><td align=\"left\"><b>"+tr("Other")
|
---|
92 | + " </b></td><td align=\"left\">"
|
---|
93 | + tr("Informational hints, expect many false entries.")+"</td></tr>"
|
---|
94 | + "</table>"
|
---|
95 | );
|
---|
96 | pnlMessage.setPreferredSize(new Dimension(500, 150));
|
---|
97 | p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
98 | p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH));
|
---|
99 |
|
---|
100 | ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
|
---|
101 | tr("Suspicious data found. Upload anyway?"),
|
---|
102 | tr("Continue upload"), tr("Cancel"))
|
---|
103 | .setButtonIcons("ok", "cancel")
|
---|
104 | .setContent(p);
|
---|
105 | int rc = ed.showDialog().getValue();
|
---|
106 | GuiHelper.destroyComponents(ed, false);
|
---|
107 | ed.dispose();
|
---|
108 | if (rc != 1) {
|
---|
109 | OsmValidator.initializeTests();
|
---|
110 | OsmValidator.initializeErrorLayer();
|
---|
111 | MainApplication.getMap().validatorDialog.unfurlDialog();
|
---|
112 | MainApplication.getLayerManager().getLayersOfType(ValidatorLayer.class).forEach(ValidatorLayer::invalidate);
|
---|
113 | return false;
|
---|
114 | }
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 | }
|
---|