001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.actions.upload; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.Dimension; 007import java.awt.GridBagLayout; 008import java.util.Collection; 009import java.util.HashSet; 010import java.util.List; 011import java.util.concurrent.atomic.AtomicBoolean; 012 013import javax.swing.JPanel; 014import javax.swing.JScrollPane; 015 016import org.openstreetmap.josm.data.APIDataSet; 017import org.openstreetmap.josm.data.osm.OsmPrimitive; 018import org.openstreetmap.josm.data.validation.OsmValidator; 019import org.openstreetmap.josm.data.validation.TestError; 020import org.openstreetmap.josm.data.validation.ValidationTask; 021import org.openstreetmap.josm.gui.ExtendedDialog; 022import org.openstreetmap.josm.gui.MainApplication; 023import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel; 024import org.openstreetmap.josm.gui.util.GuiHelper; 025import org.openstreetmap.josm.gui.widgets.HtmlPanel; 026import org.openstreetmap.josm.tools.GBC; 027 028/** 029 * The action that does the validate thing. 030 * <p> 031 * This action iterates through all active tests and gives them the data, so that 032 * each one can test it. 033 * 034 * @author frsantos 035 * @since 3669 036 */ 037public class ValidateUploadHook implements UploadHook { 038 039 /** 040 * Validate the modified data before uploading 041 * @param apiDataSet contains primitives to be uploaded 042 * @return {@code true} if upload should continue, else false 043 */ 044 @Override 045 public boolean checkUpload(APIDataSet apiDataSet) { 046 AtomicBoolean returnCode = new AtomicBoolean(); 047 Collection<OsmPrimitive> toCheck = new HashSet<>(); 048 toCheck.addAll(apiDataSet.getPrimitivesToAdd()); 049 toCheck.addAll(apiDataSet.getPrimitivesToUpdate()); 050 OsmValidator.initializeTests(); 051 new ValidationTask(errors -> { 052 if (errors.stream().allMatch(TestError::isIgnored)) { 053 returnCode.set(true); 054 } else { 055 // Unfortunately, the progress monitor is not "finished" until after `finish` is called, so we will 056 // have a ProgressMonitor open behind the error screen. Fortunately, the error screen appears in front 057 // of the progress monitor. 058 GuiHelper.runInEDTAndWait(() -> returnCode.set(displayErrorScreen(errors))); 059 } 060 }, null, OsmValidator.getEnabledTests(true), toCheck, null, true).run(); 061 062 return returnCode.get(); 063 } 064 065 /** 066 * Displays a screen where the actions that would be taken are displayed and 067 * give the user the possibility to cancel the upload. 068 * @param errors The errors displayed in the screen 069 * @return {@code true}, if the upload should continue.<br> 070 * {@code false}, if the user requested cancel. 071 */ 072 private static boolean displayErrorScreen(List<TestError> errors) { 073 JPanel p = new JPanel(new GridBagLayout()); 074 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors); 075 errorPanel.expandAll(); 076 HtmlPanel pnlMessage = new HtmlPanel(); 077 pnlMessage.setText("<html><body>" 078 + tr("The JOSM data validator partially checked the objects to be" 079 + " uploaded and found some problems. Try fixing them, but do not" 080 + " harm valid data. When in doubt ignore the findings.<br>" 081 + " You can see the findings in the Validator Results panel too." 082 + " Further checks on all data can be started from that panel.") 083 + "<table align=\"center\">" 084 + "<tr><td align=\"left\"><b>"+tr("Errors") 085 + " </b></td><td align=\"left\">" 086 + tr("Usually this should be fixed.")+"</td></tr>" 087 + "<tr><td align=\"left\"><b>"+tr("Warnings") 088 + " </b></td><td align=\"left\">" 089 + tr("Fix these when possible.")+"</td></tr>" 090 + "<tr><td align=\"left\"><b>"+tr("Other") 091 + " </b></td><td align=\"left\">" 092 + tr("Informational hints, expect many false entries.")+"</td></tr>" 093 + "</table>" 094 ); 095 pnlMessage.setPreferredSize(new Dimension(500, 150)); 096 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL)); 097 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH)); 098 099 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}