Ticket #17268: clear_ignored_errors_v8.patch

File clear_ignored_errors_v8.patch, 7.8 KB (added by taylor.smock, 7 years ago)

Most File operations moved to OsmValidator.java and the clear/restore/save ignore list button no longer shows up when the ValidatorPrefHelper.PREF_USE_IGNORE is set (under the same conditions as the Ignore button).

  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

     
    1212import java.nio.file.Files;
    1313import java.nio.file.Path;
    1414import java.nio.file.Paths;
     15import java.nio.file.StandardCopyOption;
    1516import java.util.ArrayList;
    1617import java.util.Arrays;
    1718import java.util.Collection;
     
    241242    }
    242243
    243244    /**
     245     * Get the list of all ignored errors
     246     * @return The <code>Collection&ltString&gt</code> of errors that are ignored
     247     */
     248    public static Collection<String> getIgnoredErrors() {
     249        return ignoredErrors;
     250    }
     251
     252    /**
     253     * Reset the error list by deleting ignorederrors
     254     */
     255    public static void resetErrorList() {
     256        saveIgnoredErrors();
     257        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     258        if (!ignoredErrors.isFile()) return;
     259        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     260        try {
     261            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     262        } catch (IOException e) {
     263            ignoredErrors.delete();
     264        }
     265        OsmValidator.initialize();
     266    }
     267
     268    /**
     269     * Restore the error list by copying ignorederrors.bak to ignorederrors
     270     */
     271    public static void restoreErrorList() {
     272        saveIgnoredErrors();
     273        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     274        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     275        if (!ignoredErrors.isFile() || !ignoredErrorsBak.isFile()) return;
     276        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     277        try {
     278            Files.move(ignoredErrors.toPath(),
     279                    ignoredErrorsBak2.toPath(),
     280                    StandardCopyOption.REPLACE_EXISTING);
     281            if (ignoredErrorsBak.isFile()) {
     282                Files.move(ignoredErrorsBak.toPath(),
     283                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     284            }
     285            Files.move(ignoredErrorsBak2.toPath(),
     286                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     287        } catch (IOException e) {
     288            Logging.debug(e);
     289        }
     290        OsmValidator.initialize();
     291    }
     292
     293    /**
    244294     * Saves the names of the ignored errors to a file
    245295     */
    246296    public static void saveIgnoredErrors() {
  • src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

     
    66import java.awt.event.ActionEvent;
    77import java.awt.event.KeyEvent;
    88import java.awt.event.MouseEvent;
     9import java.io.File;
    910import java.io.IOException;
    1011import java.lang.reflect.InvocationTargetException;
    1112import java.util.ArrayList;
     
    4647import org.openstreetmap.josm.data.validation.OsmValidator;
    4748import org.openstreetmap.josm.data.validation.TestError;
    4849import org.openstreetmap.josm.data.validation.ValidatorVisitor;
     50import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    4951import org.openstreetmap.josm.gui.MainApplication;
    5052import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    5153import org.openstreetmap.josm.gui.PopupMenuHandler;
     
    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                int reset = 0;
     183                {
     184                    toggle();
     185                }
     186
     187                public void toggle() {
     188                    this.setEnabled(true);
     189                    if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     190                        putValue(NAME, tr("Clear Ignore"));
     191                        putValue(SHORT_DESCRIPTION, tr("Clear ignore list"));
     192                        new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     193                        reset = 1;
     194                    } else {
     195                        File ignoredErrors = new File(OsmValidator.getValidatorDir());
     196                        ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors.bak");
     197                        if (ignoredErrors.isFile()) {
     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        if (!validateAction.isEnabled()) return;
     349        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     350                "rerun_validation_when_ignorelist_changed",
     351                MainApplication.getMainFrame(),
     352                "<hmtl><h3>" + tr("Should the validation be rerun?") + "</h3></html>",
     353                tr("Ignored error filter changed"),
     354                JOptionPane.YES_NO_CANCEL_OPTION,
     355                JOptionPane.QUESTION_MESSAGE,
     356                null,
     357                null);
     358        if (answer == JOptionPane.YES_OPTION) {
     359            validateAction.doValidate(true);
     360        }
     361    }
     362
     363    /**
    288364     * Sets the selection of the map to the current selected items.
    289365     */
    290366    @SuppressWarnings("unchecked")