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