Ticket #17268: clear_ignored_errors_v15.patch

File clear_ignored_errors_v15.patch, 10.7 KB (added by GerdP, 7 years ago)
  • no need to sort , fix i18n coding
  • src/org/openstreetmap/josm/data/preferences/sources/ValidatorPrefHelper.java

     
    1010import java.util.Map;
    1111
    1212import org.openstreetmap.josm.data.preferences.BooleanProperty;
     13import org.openstreetmap.josm.data.preferences.ListProperty;
    1314import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    1415
    1516/**
     
    4445    /** The preferences for ignored severity other */
    4546    public static final BooleanProperty PREF_OTHER = new BooleanProperty(PREFIX + ".other", false);
    4647
     48    /** The preferences key for the ignorelist */
     49    public static final ListProperty PREF_IGNORELIST = new ListProperty(PREFIX + ".ignorelist", new ArrayList<String>());
     50
     51    /** The preferences key for the ignorelist backup */
     52    public static final ListProperty PREF_IGNORELIST_BACKUP = new ListProperty(PREFIX + ".ignorelist.bak", new ArrayList<String>());
     53
     54    /** The preferences key for whether or not the ignorelist backup should be cleared on start */
     55    public static final BooleanProperty PREF_IGNORELIST_KEEP_BACKUP = new BooleanProperty(PREFIX + ".ignorelist.bak.keep", false);
    4756    /**
    4857     * The preferences key for enabling the permanent filtering
    4958     * of the displayed errors in the tree regarding the current selection
  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

     
    77import java.io.File;
    88import java.io.FileNotFoundException;
    99import java.io.IOException;
    10 import java.io.PrintWriter;
    1110import java.nio.charset.StandardCharsets;
    1211import java.nio.file.Files;
    1312import java.nio.file.Path;
     
    8988    private static double griddetail;
    9089
    9190    private static final Collection<String> ignoredErrors = new TreeSet<>();
     91    /**
     92     * The preference value for the validator's ignorederrors list
     93     */
     94    public static final String prefIgnoredErrors = "validator.ignorederrors";
    9295
    9396    /**
    9497     * All registered tests
     
    204207    private static void loadIgnoredErrors() {
    205208        ignoredErrors.clear();
    206209        if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
     210            ignoredErrors.addAll(ValidatorPrefHelper.PREF_IGNORELIST.get());
    207211            Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
    208212            try {
    209213                if (path.toFile().exists()) {
    210214                    try {
    211215                        ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
     216                        saveIgnoredErrors();
     217                        Files.deleteIfExists(path);
    212218                    } catch (FileNotFoundException e) {
    213219                        Logging.debug(Logging.getErrorMessage(e));
    214220                    } catch (IOException e) {
     
    241247    }
    242248
    243249    /**
     250     * Get the list of all ignored errors
     251     * @return The <code>Collection&ltString&gt</code> of errors that are ignored
     252     */
     253    public static Collection<String> getIgnoredErrors() {
     254        return ignoredErrors;
     255    }
     256
     257    /**
     258     * Reset the error list by deleting ignorederrors
     259     */
     260    public static void resetErrorList() {
     261        saveIgnoredErrors();
     262        backupErrorList();
     263        ValidatorPrefHelper.PREF_IGNORELIST.put(null);
     264        OsmValidator.initialize();
     265    }
     266
     267    /**
     268     * Restore the error list by copying ignorederrors.bak to ignorederrors
     269     */
     270    public static void restoreErrorList() {
     271        saveIgnoredErrors();
     272        List<String> tlist = ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.get();
     273        backupErrorList();
     274        ValidatorPrefHelper.PREF_IGNORELIST.put(tlist);
     275        OsmValidator.initialize();
     276    }
     277
     278    private static void backupErrorList() {
     279        List<String> tlist = ValidatorPrefHelper.PREF_IGNORELIST.get();
     280        if (tlist.isEmpty()) tlist = null;
     281        ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.put(tlist);
     282    }
     283
     284    /**
    244285     * Saves the names of the ignored errors to a file
    245286     */
    246287    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         }
     288        ValidatorPrefHelper.PREF_IGNORELIST.put(new ArrayList<>(ignoredErrors));
    254289    }
    255290
    256291    /**
  • src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

     
    4646import org.openstreetmap.josm.data.validation.OsmValidator;
    4747import org.openstreetmap.josm.data.validation.TestError;
    4848import org.openstreetmap.josm.data.validation.ValidatorVisitor;
     49import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    4950import org.openstreetmap.josm.gui.MainApplication;
     51import org.openstreetmap.josm.gui.MapFrame;
    5052import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    5153import org.openstreetmap.josm.gui.PopupMenuHandler;
    5254import org.openstreetmap.josm.gui.SideButton;
     
    8587    private final SideButton fixButton;
    8688    /** The ignore button */
    8789    private final SideButton ignoreButton;
     90    /** The reset ignorelist button */
     91    private final SideButton resetignorelistButton;
    8892    /** The select button */
    8993    private final SideButton selectButton;
    9094    /** The lookup button */
     
    174178            });
    175179            ignoreButton.setEnabled(false);
    176180            buttons.add(ignoreButton);
     181
     182            final String clearHideName;
     183            final String clearHideTooltip;
     184            if (!ValidatorPrefHelper.PREF_IGNORELIST_KEEP_BACKUP.get()) {
     185                // Clear the backup ignore list
     186                ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.put(null);
     187                clearHideName = tr("Clear Ignore");
     188                clearHideTooltip = tr("Clear ignore list");
     189            } else {
     190                clearHideName = tr("Hide Ignore");
     191                clearHideTooltip = tr("Hide ignore list");
     192            }
     193            resetignorelistButton = new SideButton(new AbstractAction() {
     194                int reset;
     195                {
     196                    toggle();
     197                }
     198
     199                public void toggle() {
     200                    this.setEnabled(true);
     201                    if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     202                        putValue(NAME, clearHideName);
     203                        putValue(SHORT_DESCRIPTION, clearHideTooltip);
     204                        new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     205                        reset = 1;
     206                    } else {
     207                        List<String> ignoredErrors = ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.get();
     208                        if (!ignoredErrors.isEmpty()) {
     209                            putValue(NAME, tr("Restore Ignore"));
     210                            putValue(SHORT_DESCRIPTION, tr("Restore ignore list"));
     211                            new ImageProvider("copy").getResource().attachImageIcon(this, true);
     212                            reset = 2;
     213                        } else {
     214                            putValue(NAME, tr("Ignore list modification"));
     215                            putValue(SHORT_DESCRIPTION, tr("Hide/Clear/Restore the ignore list, depending upon various conditions"));
     216                            new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     217                            this.setEnabled(false);
     218                        }
     219                    }
     220                }
     221
     222                @Override
     223                public void actionPerformed(ActionEvent e) {
     224                    int doToggle = JOptionPane.NO_OPTION;
     225                    if (e != null) {
     226                        if (reset == 1 || reset == 2) doToggle = rerunValidatorPrompt();
     227                        if (reset == 1 && (doToggle == JOptionPane.YES_OPTION || doToggle == JOptionPane.NO_OPTION)) {
     228                            OsmValidator.resetErrorList();
     229                        } else if (reset == 2 && (doToggle == JOptionPane.YES_OPTION || doToggle == JOptionPane.NO_OPTION)) {
     230                            OsmValidator.restoreErrorList();
     231                        }
     232                    }
     233                    if (doToggle == JOptionPane.YES_OPTION || doToggle == JOptionPane.NO_OPTION) toggle();
     234                }
     235            });
     236            buttons.add(resetignorelistButton);
    177237        } else {
    178238            ignoreButton = null;
     239            resetignorelistButton = null;
    179240        }
     241
    180242        createLayout(tree, true, buttons);
    181243    }
    182244
     
    280342        if (changed.get()) {
    281343            tree.resetErrors();
    282344            OsmValidator.saveIgnoredErrors();
     345            if (resetignorelistButton != null) {
     346                resetignorelistButton.getAction().actionPerformed(null);
     347            }
    283348            invalidateValidatorLayers();
    284349        }
    285350    }
    286351
    287352    /**
     353     * Prompt to rerun the validator when the ignore list changes
     354     * @return {@code JOptionPane.YES_OPTION}, {@code JOptionPane.NO_OPTION},
     355     *  or {@code JOptionPane.CANCEL_OPTION}
     356     */
     357    public int rerunValidatorPrompt() {
     358        MapFrame map = MainApplication.getMap();
     359        List<TestError> errors = map.validatorDialog.tree.getErrors();
     360        if (!validateAction.isEnabled() || errors == null || errors.isEmpty()) return JOptionPane.NO_OPTION;
     361        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     362                "rerun_validation_when_ignorelist_changed",
     363                MainApplication.getMainFrame(),
     364                "<hmtl><h3>" + tr("Should the validation be rerun?") + "</h3></html>",
     365                tr("Ignored error filter changed"),
     366                JOptionPane.YES_NO_CANCEL_OPTION,
     367                JOptionPane.QUESTION_MESSAGE,
     368                null,
     369                null);
     370        if (answer == JOptionPane.YES_OPTION) {
     371            validateAction.doValidate(true);
     372        }
     373        return answer;
     374    }
     375
     376    /**
    288377     * Sets the selection of the map to the current selected items.
    289378     */
    290     @SuppressWarnings("unchecked")
    291379    private void setSelectedItems() {
    292380        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    293381        if (tree == null || ds == null)