Ticket #17268: clear_ignored_errors_v10.patch

File clear_ignored_errors_v10.patch, 8.7 KB (added by taylor.smock, 5 years ago)

Add a check to see if the validator has been run (does not detect when the error list has been cleared, and assumes that the validator has not been run in that case).

  • 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(Config.getPref().getList(prefIgnoredErrors));
    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        Config.getPref().putList(prefIgnoredErrors, 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 = Config.getPref().getList(prefIgnoredErrors + ".bak");
     273        backupErrorList();
     274        Config.getPref().putList(prefIgnoredErrors, tlist);
     275        OsmValidator.initialize();
     276    }
     277
     278    private static void backupErrorList() {
     279        List<String> tlist = Config.getPref().getList(prefIgnoredErrors);
     280        if (tlist.isEmpty()) tlist = null;
     281        Config.getPref().putList(prefIgnoredErrors + ".bak", 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        List<String> list = new ArrayList<>(ignoredErrors);
     289        Collections.sort(list);
     290        Config.getPref().putList(prefIgnoredErrors, list);
    254291    }
    255292
    256293    /**
  • 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            resetignorelistButton = new SideButton(new AbstractAction() {
     182                /* Java integers initialize to "0" by default */
     183                int reset;
     184                {
     185                    toggle();
     186                }
     187
     188                public void toggle() {
     189                    this.setEnabled(true);
     190                    if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     191                        putValue(NAME, tr("Clear Ignore"));
     192                        putValue(SHORT_DESCRIPTION, tr("Clear ignore list"));
     193                        new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     194                        reset = 1;
     195                    } else {
     196                        List<String> ignoredErrors = Config.getPref().getList(OsmValidator.prefIgnoredErrors + ".bak");
     197                        if (!ignoredErrors.isEmpty()) {
     198                            putValue(NAME, tr("Restore Ignore"));
     199                            putValue(SHORT_DESCRIPTION, tr("Restore ignore list"));
     200                            new ImageProvider("copy").getResource().attachImageIcon(this, true);
     201                            reset = 2;
     202                        } else if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     203                            putValue(NAME, tr("Save Ignore"));
     204                            putValue(SHORT_DESCRIPTION, tr("Save ignore list"));
     205                            new ImageProvider("save").getResource().attachImageIcon(this, true);
     206                            reset = 3;
     207                        } else {
     208                            putValue(NAME, tr("Ignore list modification"));
     209                            putValue(SHORT_DESCRIPTION, tr("Clear/Restore/Save the ignore list, depending upon various conditions"));
     210                            new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     211                            //this.setEnabled(false); // TODO enable when I figure out how to call from ignore
     212                        }
     213                    }
     214                }
     215
     216                @Override
     217                public void actionPerformed(ActionEvent e) {
     218                    if (reset == 1) {
     219                        OsmValidator.resetErrorList();
     220                    } else if (reset == 2) {
     221                        OsmValidator.restoreErrorList();
     222                    } else if (reset == 3 && !OsmValidator.getIgnoredErrors().isEmpty()) {
     223                        OsmValidator.saveIgnoredErrors();
     224                    } else if (reset == 0) {
     225                        // Do nothing
     226                    }
     227                    if (reset == 1 || reset == 2) rerunValidatorPrompt();
     228                    toggle();
     229                }
     230            });
     231            buttons.add(resetignorelistButton);
    177232        } else {
    178233            ignoreButton = null;
     234            resetignorelistButton = null;
    179235        }
     236
    180237        createLayout(tree, true, buttons);
    181238    }
    182239
     
    285342    }
    286343
    287344    /**
     345     * Prompt to rerun the validator when the ignore list changes
     346     */
     347    public void rerunValidatorPrompt() {
     348        MapFrame map = MainApplication.getMap();
     349        List<TestError> errors = map.validatorDialog.tree.getErrors();
     350        if (!validateAction.isEnabled() || errors == null || errors.isEmpty()) return;
     351        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     352                "rerun_validation_when_ignorelist_changed",
     353                MainApplication.getMainFrame(),
     354                "<hmtl><h3>" + tr("Should the validation be rerun?") + "</h3></html>",
     355                tr("Ignored error filter changed"),
     356                JOptionPane.YES_NO_CANCEL_OPTION,
     357                JOptionPane.QUESTION_MESSAGE,
     358                null,
     359                null);
     360        if (answer == JOptionPane.YES_OPTION) {
     361            validateAction.doValidate(true);
     362        }
     363    }
     364
     365    /**
    288366     * Sets the selection of the map to the current selected items.
    289367     */
    290     @SuppressWarnings("unchecked")
    291368    private void setSelectedItems() {
    292369        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    293370        if (tree == null || ds == null)