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

Last change on this file since 19000 was 19000, checked in by GerdP, 3 months ago

fix #23519: Don't automatically enlarge "Validation Results" panel

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