001 // License: GPL. See LICENSE file for details. 002 package org.openstreetmap.josm.actions; 003 004 import static org.openstreetmap.josm.tools.I18n.tr; 005 006 import java.awt.event.ActionEvent; 007 import java.awt.event.KeyEvent; 008 import java.io.IOException; 009 import java.util.ArrayList; 010 import java.util.Collection; 011 import java.util.List; 012 013 import org.openstreetmap.josm.Main; 014 import org.openstreetmap.josm.data.osm.OsmPrimitive; 015 import org.openstreetmap.josm.data.validation.OsmValidator; 016 import org.openstreetmap.josm.data.validation.Test; 017 import org.openstreetmap.josm.data.validation.TestError; 018 import org.openstreetmap.josm.data.validation.util.AggregatePrimitivesVisitor; 019 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 020 import org.openstreetmap.josm.gui.preferences.ValidatorPreference; 021 import org.openstreetmap.josm.gui.util.GuiHelper; 022 import org.openstreetmap.josm.io.OsmTransferException; 023 import org.openstreetmap.josm.tools.Shortcut; 024 import org.xml.sax.SAXException; 025 026 /** 027 * The action that does the validate thing. 028 * <p> 029 * This action iterates through all active tests and give them the data, so that 030 * each one can test it. 031 * 032 * @author frsantos 033 */ 034 public class ValidateAction extends JosmAction { 035 036 /** Serializable ID */ 037 private static final long serialVersionUID = -2304521273582574603L; 038 039 /** Last selection used to validate */ 040 private Collection<OsmPrimitive> lastSelection; 041 042 /** 043 * Constructor 044 */ 045 public ValidateAction() { 046 super(tr("Validation"), "dialogs/validator", tr("Performs the data validation"), 047 Shortcut.registerShortcut("tools:validate", tr("Tool: {0}", tr("Validation")), 048 KeyEvent.VK_V, Shortcut.SHIFT), true); 049 } 050 051 public void actionPerformed(ActionEvent ev) { 052 doValidate(ev, true); 053 } 054 055 /** 056 * Does the validation. 057 * <p> 058 * If getSelectedItems is true, the selected items (or all items, if no one 059 * is selected) are validated. If it is false, last selected items are 060 * revalidated 061 * 062 * @param ev The event 063 * @param getSelectedItems If selected or last selected items must be validated 064 */ 065 public void doValidate(ActionEvent ev, boolean getSelectedItems) { 066 if (Main.map == null || !Main.map.isVisible()) 067 return; 068 069 OsmValidator.initializeErrorLayer(); 070 071 Collection<Test> tests = OsmValidator.getEnabledTests(false); 072 if (tests.isEmpty()) 073 return; 074 075 Collection<OsmPrimitive> selection; 076 if (getSelectedItems) { 077 selection = Main.main.getCurrentDataSet().getAllSelected(); 078 if (selection.isEmpty()) { 079 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives(); 080 lastSelection = null; 081 } else { 082 AggregatePrimitivesVisitor v = new AggregatePrimitivesVisitor(); 083 selection = v.visit(selection); 084 lastSelection = selection; 085 } 086 } else { 087 if (lastSelection == null) { 088 selection = Main.main.getCurrentDataSet().allNonDeletedPrimitives(); 089 } else { 090 selection = lastSelection; 091 } 092 } 093 094 ValidationTask task = new ValidationTask(tests, selection, lastSelection); 095 Main.worker.submit(task); 096 } 097 098 @Override 099 public void updateEnabledState() { 100 setEnabled(getEditLayer() != null); 101 } 102 103 /** 104 * Asynchronous task for running a collection of tests against a collection 105 * of primitives 106 * 107 */ 108 static class ValidationTask extends PleaseWaitRunnable { 109 private Collection<Test> tests; 110 private Collection<OsmPrimitive> validatedPrimitives; 111 private Collection<OsmPrimitive> formerValidatedPrimitives; 112 private boolean canceled; 113 private List<TestError> errors; 114 115 /** 116 * 117 * @param tests the tests to run 118 * @param validatedPrimitives the collection of primitives to validate. 119 * @param formerValidatedPrimitives the last collection of primitives being validates. May be null. 120 */ 121 public ValidationTask(Collection<Test> tests, Collection<OsmPrimitive> validatedPrimitives, Collection<OsmPrimitive> formerValidatedPrimitives) { 122 super(tr("Validating"), false /*don't ignore exceptions */); 123 this.validatedPrimitives = validatedPrimitives; 124 this.formerValidatedPrimitives = formerValidatedPrimitives; 125 this.tests = tests; 126 } 127 128 @Override 129 protected void cancel() { 130 this.canceled = true; 131 } 132 133 @Override 134 protected void finish() { 135 if (canceled) return; 136 137 // update GUI on Swing EDT 138 // 139 GuiHelper.runInEDT(new Runnable() { 140 @Override 141 public void run() { 142 Main.map.validatorDialog.tree.setErrors(errors); 143 Main.map.validatorDialog.unfurlDialog(); 144 Main.main.getCurrentDataSet().fireSelectionChanged(); 145 } 146 }); 147 } 148 149 @Override 150 protected void realRun() throws SAXException, IOException, 151 OsmTransferException { 152 if (tests == null || tests.isEmpty()) 153 return; 154 errors = new ArrayList<TestError>(200); 155 getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size()); 156 int testCounter = 0; 157 for (Test test : tests) { 158 if (canceled) 159 return; 160 testCounter++; 161 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(),test.getName())); 162 test.setPartialSelection(formerValidatedPrimitives != null); 163 test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false)); 164 test.visit(validatedPrimitives); 165 test.endTest(); 166 errors.addAll(test.getErrors()); 167 } 168 tests = null; 169 if (Main.pref.getBoolean(ValidatorPreference.PREF_USE_IGNORE, true)) { 170 getProgressMonitor().subTask(tr("Updating ignored errors ...")); 171 for (TestError error : errors) { 172 if (canceled) return; 173 List<String> s = new ArrayList<String>(); 174 s.add(error.getIgnoreState()); 175 s.add(error.getIgnoreGroup()); 176 s.add(error.getIgnoreSubGroup()); 177 for (String state : s) { 178 if (state != null && OsmValidator.hasIgnoredError(state)) { 179 error.setIgnored(true); 180 } 181 } 182 } 183 } 184 } 185 } 186 }