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.ArrayList; 009import java.util.Collection; 010import java.util.List; 011 012import javax.swing.JPanel; 013import javax.swing.JScrollPane; 014 015import org.openstreetmap.josm.Main; 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.Severity; 020import org.openstreetmap.josm.data.validation.Test; 021import org.openstreetmap.josm.data.validation.TestError; 022import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor; 023import org.openstreetmap.josm.gui.ExtendedDialog; 024import org.openstreetmap.josm.gui.dialogs.validator.ValidatorTreePanel; 025import org.openstreetmap.josm.gui.layer.OsmDataLayer; 026import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference; 027import org.openstreetmap.josm.gui.widgets.HtmlPanel; 028import org.openstreetmap.josm.tools.GBC; 029 030/** 031 * The action that does the validate thing. 032 * <p> 033 * This action iterates through all active tests and give them the data, so that 034 * each one can test it. 035 * 036 * @author frsantos 037 * @since 3669 038 */ 039public class ValidateUploadHook implements UploadHook { 040 041 /** 042 * Validate the modified data before uploading 043 */ 044 @Override 045 public boolean checkUpload(APIDataSet apiDataSet) { 046 047 OsmValidator.initializeTests(); 048 Collection<Test> tests = OsmValidator.getEnabledTests(true); 049 if (tests.isEmpty()) 050 return true; 051 052 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor(); 053 v.visit(apiDataSet.getPrimitivesToAdd()); 054 Collection<OsmPrimitive> selection = v.visit(apiDataSet.getPrimitivesToUpdate()); 055 056 List<TestError> errors = new ArrayList<>(30); 057 for (Test test : tests) { 058 test.setBeforeUpload(true); 059 test.setPartialSelection(true); 060 test.startTest(null); 061 test.visit(selection); 062 test.endTest(); 063 if (ValidatorPreference.PREF_OTHER.get() && 064 Main.pref.getBoolean(ValidatorPreference.PREF_OTHER_UPLOAD, false)) { 065 errors.addAll(test.getErrors()); 066 } else { 067 for (TestError e : test.getErrors()) { 068 if (e.getSeverity() != Severity.OTHER) { 069 errors.add(e); 070 } 071 } 072 } 073 } 074 tests = null; 075 OsmDataLayer editLayer = Main.main.getEditLayer(); 076 editLayer.validationErrors.clear(); 077 editLayer.validationErrors.addAll(errors); 078 Main.map.validatorDialog.tree.setErrors(errors); 079 if (errors == null || errors.isEmpty()) 080 return true; 081 082 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) { 083 int nume = 0; 084 for (TestError error : errors) { 085 List<String> s = new ArrayList<>(); 086 s.add(error.getIgnoreState()); 087 s.add(error.getIgnoreGroup()); 088 s.add(error.getIgnoreSubGroup()); 089 for (String state : s) { 090 if (state != null && OsmValidator.hasIgnoredError(state)) { 091 error.setIgnored(true); 092 } 093 } 094 if (!error.isIgnored()) { 095 ++nume; 096 } 097 } 098 if (nume == 0) 099 return true; 100 } 101 return displayErrorScreen(errors); 102 } 103 104 /** 105 * Displays a screen where the actions that would be taken are displayed and 106 * give the user the possibility to cancel the upload. 107 * @param errors The errors displayed in the screen 108 * @return <code>true</code>, if the upload should continue. <code>false</code> 109 * if the user requested cancel. 110 */ 111 private static boolean displayErrorScreen(List<TestError> errors) { 112 JPanel p = new JPanel(new GridBagLayout()); 113 ValidatorTreePanel errorPanel = new ValidatorTreePanel(errors); 114 errorPanel.expandAll(); 115 HtmlPanel pnlMessage = new HtmlPanel(); 116 pnlMessage.setText("<html><body>" 117 + tr("The following are results of automatic validation. Try fixing" 118 + " these, but be careful (don''t destroy valid data)." 119 + " When in doubt ignore them.<br>When you" 120 + " cancel this dialog, you can find the entries in the validator" 121 + " side panel to inspect them.") 122 + "<table align=\"center\">" 123 + "<tr><td align=\"left\"><b>"+tr("Errors") 124 + " </b></td><td align=\"left\">" 125 + tr("Usually this should be fixed.")+"</td></tr>" 126 + "<tr><td align=\"left\"><b>"+tr("Warnings") 127 + " </b></td><td align=\"left\">" 128 + tr("Fix these when possible.")+"</td></tr>" 129 + "<tr><td align=\"left\"><b>"+tr("Other") 130 + " </b></td><td align=\"left\">" 131 + tr("Informational warnings, expect many false entries.")+"</td></tr>" 132 + "</table>" 133 ); 134 pnlMessage.setPreferredSize(new Dimension(500, 150)); 135 p.add(pnlMessage, GBC.eol().fill(GBC.HORIZONTAL)); 136 p.add(new JScrollPane(errorPanel), GBC.eol().fill(GBC.BOTH)); 137 138 ExtendedDialog ed = new ExtendedDialog(Main.parent, 139 tr("Suspicious data found. Upload anyway?"), 140 new String[] {tr("Continue upload"), tr("Cancel")}); 141 ed.setButtonIcons(new String[] {"ok.png", "cancel.png"}); 142 ed.setContent(p); 143 ed.showDialog(); 144 145 if (ed.getValue() != 1) { 146 OsmValidator.initializeTests(); 147 OsmValidator.initializeErrorLayer(); 148 Main.map.validatorDialog.unfurlDialog(); 149 Main.main.getCurrentDataSet().fireSelectionChanged(); 150 return false; 151 } 152 return true; 153 } 154}