Changeset 14828 in josm


Ignore:
Timestamp:
2019-03-03T10:52:10+01:00 (5 years ago)
Author:
GerdP
Message:

fix #17268: There should be a method to clear ignored errors

patch clear_ignored_errors_v32.patch by taylor.smock adapted to r14827

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  
    4444    /** The preferences for ignored severity other */
    4545    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";
    4649
    4750    /**
  • trunk/src/org/openstreetmap/josm/data/validation/OsmValidator.java

    r14153 r14828  
    88import java.io.FileNotFoundException;
    99import java.io.IOException;
    10 import java.io.PrintWriter;
    1110import java.nio.charset.StandardCharsets;
    1211import java.nio.file.Files;
     
    1817import java.util.Collections;
    1918import java.util.EnumMap;
     19import java.util.Enumeration;
    2020import java.util.HashMap;
     21import java.util.Iterator;
    2122import java.util.List;
    2223import java.util.Map;
     24import java.util.Map.Entry;
    2325import java.util.SortedMap;
    2426import java.util.TreeMap;
     
    2830
    2931import javax.swing.JOptionPane;
     32import javax.swing.JTree;
     33import javax.swing.tree.DefaultMutableTreeNode;
     34import javax.swing.tree.TreeModel;
     35import javax.swing.tree.TreeNode;
    3036
    3137import org.openstreetmap.josm.data.preferences.sources.ValidatorPrefHelper;
     
    8995    private static double griddetail;
    9096
    91     private static final Collection<String> ignoredErrors = new TreeSet<>();
    92 
     97    private static final SortedMap<String, String> ignoredErrors = new TreeMap<>();
    9398    /**
    9499     * All registered tests
     
    170175        checkValidatorDir();
    171176        initializeGridDetail();
    172         loadIgnoredErrors(); //FIXME: load only when needed
     177        loadIgnoredErrors();
    173178    }
    174179
     
    205210        ignoredErrors.clear();
    206211        if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
     212            Config.getPref().getListOfMaps(ValidatorPrefHelper.PREF_IGNORELIST).forEach(ignoredErrors::putAll);
    207213            Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
    208214            try {
    209215                if (path.toFile().exists()) {
    210216                    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
    212224                    } catch (FileNotFoundException e) {
    213225                        Logging.debug(Logging.getErrorMessage(e));
     
    229241     */
    230242    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        }
    232284    }
    233285
     
    238290     */
    239291    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&ltString&gt</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 &lttype&gtlist 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
    245418     */
    246419    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);
    254433    }
    255434
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

    r14826 r14828  
    6565import org.openstreetmap.josm.tools.InputMapUtils;
    6666import org.openstreetmap.josm.tools.JosmRuntimeException;
     67import org.openstreetmap.josm.tools.Pair;
    6768import org.openstreetmap.josm.tools.Shortcut;
    6869import org.xml.sax.SAXException;
     
    8788    /** The ignore button */
    8889    private final SideButton ignoreButton;
     90    /** The reset ignorelist button */
     91    private final SideButton ignorelistManagement;
    8992    /** The select button */
    9093    private final SideButton selectButton;
     
    160163            ignoreButton.setEnabled(false);
    161164            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);
    162179        } else {
    163180            ignoreButton = null;
    164         }
     181            ignorelistManagement = null;
     182        }
     183
    165184        createLayout(tree, true, buttons);
    166185    }
     
    169188     * The action to lookup the selection in the error tree.
    170189     */
    171      class LookupAction extends AbstractAction implements DataSelectionListener {
     190    class LookupAction extends AbstractAction implements DataSelectionListener {
    172191
    173192        LookupAction() {
     
    274293            if (depth <= 1) {
    275294                if (!(mainNodeInfo instanceof TestError)) {
    276                     Set<String> state = new HashSet<>();
     295                    Set<Pair<String, String>> state = new HashSet<>();
    277296                    // ask if the whole set should be ignored
    278297                    if (asked == JOptionPane.DEFAULT_OPTION) {
     
    286305                            err.setIgnored(true);
    287306                            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()));
    289308                        }, processedNodes);
    290                         for (String s : state) {
    291                             OsmValidator.addIgnoredError(s);
     309                        for (Pair<String, String> s : state) {
     310                            OsmValidator.addIgnoredError(s.a, s.b);
    292311                        }
    293312                        continue;
     
    300319                    String state = error.getIgnoreState();
    301320                    if (state != null) {
    302                         OsmValidator.addIgnoredError(state);
     321                        OsmValidator.addIgnoredError(state, error.getMessage());
    303322                    }
    304323                    changed.set(true);
     
    317336     * Sets the selection of the map to the current selected items.
    318337     */
    319     @SuppressWarnings("unchecked")
    320338    private void setSelectedItems() {
    321339        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
Note: See TracChangeset for help on using the changeset viewer.