Changeset 14828 in josm
- Timestamp:
- 2019-03-03T10:52:10+01:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/preferences/sources/ValidatorPrefHelper.java
r12825 r14828 44 44 /** The preferences for ignored severity other */ 45 45 public static final BooleanProperty PREF_OTHER = new BooleanProperty(PREFIX + ".other", false); 46 47 /** The preferences key for the ignorelist */ 48 public static final String PREF_IGNORELIST = PREFIX + ".ignorelist"; 46 49 47 50 /** -
trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java
r14153 r14828 8 8 import java.io.FileNotFoundException; 9 9 import java.io.IOException; 10 import java.io.PrintWriter;11 10 import java.nio.charset.StandardCharsets; 12 11 import java.nio.file.Files; … … 18 17 import java.util.Collections; 19 18 import java.util.EnumMap; 19 import java.util.Enumeration; 20 20 import java.util.HashMap; 21 import java.util.Iterator; 21 22 import java.util.List; 22 23 import java.util.Map; 24 import java.util.Map.Entry; 23 25 import java.util.SortedMap; 24 26 import java.util.TreeMap; … … 28 30 29 31 import javax.swing.JOptionPane; 32 import javax.swing.JTree; 33 import javax.swing.tree.DefaultMutableTreeNode; 34 import javax.swing.tree.TreeModel; 35 import javax.swing.tree.TreeNode; 30 36 31 37 import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper; … … 89 95 private static double griddetail; 90 96 91 private static final Collection<String> ignoredErrors = new TreeSet<>(); 92 97 private static final SortedMap<String, String> ignoredErrors = new TreeMap<>(); 93 98 /** 94 99 * All registered tests … … 170 175 checkValidatorDir(); 171 176 initializeGridDetail(); 172 loadIgnoredErrors(); //FIXME: load only when needed177 loadIgnoredErrors(); 173 178 } 174 179 … … 205 210 ignoredErrors.clear(); 206 211 if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) { 212 Config.getPref().getListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST).forEach(ignoredErrors::putAll); 207 213 Path path = Paths.get(getValidatorDir()).resolve("ignorederrors"); 208 214 try { 209 215 if (path.toFile().exists()) { 210 216 try { 211 ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8)); 217 TreeSet<String> treeSet = new TreeSet<>(); 218 treeSet.addAll(Files.readAllLines(path, StandardCharsets.UTF_8)); 219 treeSet.forEach(ignore -> ignoredErrors.putIfAbsent(ignore, "")); 220 221 saveIgnoredErrors(); 222 Files.deleteIfExists(path); 223 212 224 } catch (FileNotFoundException e) { 213 225 Logging.debug(Logging.getErrorMessage(e)); … … 229 241 */ 230 242 public static void addIgnoredError(String s) { 231 ignoredErrors.add(s); 243 addIgnoredError(s, ""); 244 } 245 246 /** 247 * Adds an ignored error 248 * @param s The ignore group / sub group name 249 * @param description What the error actually is 250 * @see TestError#getIgnoreGroup() 251 * @see TestError#getIgnoreSubGroup() 252 */ 253 public static void addIgnoredError(String s, String description) { 254 if (description == null) description = ""; 255 ignoredErrors.put(s, description); 256 } 257 258 /** 259 * Make sure that we don't keep single entries for a "group ignore" or 260 * multiple different entries for the single entries that are in the same group. 261 */ 262 private static void cleanupIgnoredErrors() { 263 if (ignoredErrors.size() > 1) { 264 List<String> toRemove = new ArrayList<>(); 265 266 Iterator<Entry<String, String>> iter = ignoredErrors.entrySet().iterator(); 267 Entry<String, String> last = iter.next(); 268 while (iter.hasNext()) { 269 Entry<String, String> entry = iter.next(); 270 if (entry.getKey().startsWith(last.getKey())) { 271 toRemove.add(entry.getKey()); 272 } else { 273 last = entry; 274 } 275 } 276 toRemove.forEach(ignoredErrors::remove); 277 } 278 279 Map<String, String> tmap = buildIgnore(buildJTreeList()); 280 if (tmap != null && !tmap.isEmpty()) { 281 ignoredErrors.clear(); 282 ignoredErrors.putAll(tmap); 283 } 232 284 } 233 285 … … 238 290 */ 239 291 public static boolean hasIgnoredError(String s) { 240 return ignoredErrors.contains(s); 241 } 242 243 /** 244 * Saves the names of the ignored errors to a file 292 return ignoredErrors.containsKey(s); 293 } 294 295 /** 296 * Get the list of all ignored errors 297 * @return The <code>Collection<String></code> of errors that are ignored 298 */ 299 public static SortedMap<String, String> getIgnoredErrors() { 300 return ignoredErrors; 301 } 302 303 /** 304 * Build a JTree with a list 305 * @return <type>list as a {@code JTree} 306 */ 307 public static JTree buildJTreeList() { 308 DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("Ignore list")); 309 for (Entry<String, String> e: ignoredErrors.entrySet()) { 310 String key = e.getKey(); 311 String value = e.getValue(); 312 ArrayList<String> ignoredWayList = new ArrayList<>(); 313 String[] osmobjects = key.split(":(r|w|n)_"); 314 for (int i = 1; i < osmobjects.length; i++) { 315 String osmid = osmobjects[i]; 316 if (osmid.matches("^[0-9]+$")) { 317 osmid = '_' + osmid; 318 int index = key.indexOf(osmid); 319 if (index < key.lastIndexOf(']')) continue; 320 char type = key.charAt(index - 1); 321 ignoredWayList.add(type + osmid); 322 } 323 } 324 for (String osmignore : ignoredWayList) { 325 key = key.replace(':' + osmignore, ""); 326 } 327 328 DefaultMutableTreeNode trunk; 329 DefaultMutableTreeNode branch; 330 331 if (value != null && !value.isEmpty()) { 332 trunk = inTree(root, value); 333 branch = inTree(trunk, key); 334 trunk.add(branch); 335 } else { 336 trunk = inTree(root, key); 337 branch = trunk; 338 } 339 ignoredWayList.forEach(osmignore -> branch.add(new DefaultMutableTreeNode(osmignore))); 340 341 root.add(trunk); 342 } 343 return new JTree(root); 344 } 345 346 private static DefaultMutableTreeNode inTree(DefaultMutableTreeNode root, String name) { 347 @SuppressWarnings("unchecked") 348 Enumeration<TreeNode> trunks = root.children(); 349 while (trunks.hasMoreElements()) { 350 TreeNode ttrunk = trunks.nextElement(); 351 if (ttrunk instanceof DefaultMutableTreeNode) { 352 DefaultMutableTreeNode trunk = (DefaultMutableTreeNode) ttrunk; 353 if (name.equals(trunk.getUserObject())) { 354 return trunk; 355 } 356 } 357 } 358 return new DefaultMutableTreeNode(name); 359 } 360 361 /** 362 * Build a {@code HashMap} from a tree of ignored errors 363 * @param tree The JTree of ignored errors 364 * @return A {@code HashMap} of the ignored errors for comparison 365 */ 366 public static Map<String, String> buildIgnore(JTree tree) { 367 TreeModel model = tree.getModel(); 368 DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); 369 return buildIgnore(model, root); 370 } 371 372 private static Map<String, String> buildIgnore(TreeModel model, DefaultMutableTreeNode node) { 373 HashMap<String, String> rHashMap = new HashMap<>(); 374 375 String osmids = node.getUserObject().toString(); 376 String description = ""; 377 378 if (!model.getRoot().equals(node)) { 379 description = ((DefaultMutableTreeNode) node.getParent()).getUserObject().toString(); 380 } else { 381 description = node.getUserObject().toString(); 382 } 383 if (tr("Ignore list").equals(description)) description = ""; 384 if (!osmids.matches("^[0-9]+(_.*|$)")) { 385 description = osmids; 386 osmids = ""; 387 } 388 389 390 for (int i = 0; i < model.getChildCount(node); i++) { 391 DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(node, i); 392 if (model.getChildCount(child) == 0) { 393 String ignoreName = child.getUserObject().toString(); 394 if (ignoreName.matches("^(r|w|n)_.*")) { 395 osmids += ":" + child.getUserObject().toString(); 396 } else if (ignoreName.matches("^[0-9]+(_.*|)$")) { 397 rHashMap.put(ignoreName, description); 398 } 399 } else { 400 rHashMap.putAll(buildIgnore(model, child)); 401 } 402 } 403 if (!osmids.isEmpty() && osmids.indexOf(':') != 0) rHashMap.put(osmids, description); 404 return rHashMap; 405 } 406 407 /** 408 * Reset the error list by deleting {@code validator.ignorelist} 409 */ 410 public static void resetErrorList() { 411 saveIgnoredErrors(); 412 Config.getPref().putListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST, null); 413 OsmValidator.initialize(); 414 } 415 416 /** 417 * Saves the names of the ignored errors to a preference 245 418 */ 246 419 public static void saveIgnoredErrors() { 247 try (PrintWriter out = new PrintWriter(new File(getValidatorDir(), "ignorederrors"), StandardCharsets.UTF_8.name())) { 248 for (String e : ignoredErrors) { 249 out.println(e); 250 } 251 } catch (IOException e) { 252 Logging.error(e); 253 } 420 List<Map<String, String>> list = new ArrayList<>(); 421 cleanupIgnoredErrors(); 422 list.add(ignoredErrors); 423 int i = 0; 424 while (i < list.size()) { 425 if (list.get(i) == null || list.get(i).isEmpty()) { 426 list.remove(i); 427 continue; 428 } 429 i++; 430 } 431 if (list.isEmpty()) list = null; 432 Config.getPref().putListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST, list); 254 433 } 255 434 -
trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java
r14826 r14828 65 65 import org.openstreetmap.josm.tools.InputMapUtils; 66 66 import org.openstreetmap.josm.tools.JosmRuntimeException; 67 import org.openstreetmap.josm.tools.Pair; 67 68 import org.openstreetmap.josm.tools.Shortcut; 68 69 import org.xml.sax.SAXException; … … 87 88 /** The ignore button */ 88 89 private final SideButton ignoreButton; 90 /** The reset ignorelist button */ 91 private final SideButton ignorelistManagement; 89 92 /** The select button */ 90 93 private final SideButton selectButton; … … 160 163 ignoreButton.setEnabled(false); 161 164 buttons.add(ignoreButton); 165 166 ignorelistManagement = new SideButton(new AbstractAction() { 167 { 168 putValue(NAME, tr("Manage Ignore")); 169 putValue(SHORT_DESCRIPTION, tr("Manage the ignore list")); 170 new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true); 171 } 172 173 @Override 174 public void actionPerformed(ActionEvent e) { 175 new ValidatorListManagementDialog("Ignore"); 176 } 177 }); 178 buttons.add(ignorelistManagement); 162 179 } else { 163 180 ignoreButton = null; 164 } 181 ignorelistManagement = null; 182 } 183 165 184 createLayout(tree, true, buttons); 166 185 } … … 169 188 * The action to lookup the selection in the error tree. 170 189 */ 171 190 class LookupAction extends AbstractAction implements DataSelectionListener { 172 191 173 192 LookupAction() { … … 274 293 if (depth <= 1) { 275 294 if (!(mainNodeInfo instanceof TestError)) { 276 Set< String> state = new HashSet<>();295 Set<Pair<String, String>> state = new HashSet<>(); 277 296 // ask if the whole set should be ignored 278 297 if (asked == JOptionPane.DEFAULT_OPTION) { … … 286 305 err.setIgnored(true); 287 306 changed.set(true); 288 state.add( depth == 1 ? err.getIgnoreSubGroup() : err.getIgnoreGroup());307 state.add(new Pair<>(node.getDepth() == 1 ? err.getIgnoreSubGroup() : err.getIgnoreGroup(), err.getMessage())); 289 308 }, processedNodes); 290 for ( Strings : state) {291 OsmValidator.addIgnoredError(s );309 for (Pair<String, String> s : state) { 310 OsmValidator.addIgnoredError(s.a, s.b); 292 311 } 293 312 continue; … … 300 319 String state = error.getIgnoreState(); 301 320 if (state != null) { 302 OsmValidator.addIgnoredError(state );321 OsmValidator.addIgnoredError(state, error.getMessage()); 303 322 } 304 323 changed.set(true); … … 317 336 * Sets the selection of the map to the current selected items. 318 337 */ 319 @SuppressWarnings("unchecked")320 338 private void setSelectedItems() { 321 339 DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
Note:
See TracChangeset
for help on using the changeset viewer.