Ticket #17268: clear_ignored_errors_v6.patch

File clear_ignored_errors_v6.patch, 7.6 KB (added by taylor.smock, 5 years ago)

Ask if validation should be rerun (only does selection)

  • 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;
     12import java.nio.file.Files;
     13import java.nio.file.StandardCopyOption;
    1114import java.util.ArrayList;
    1215import java.util.Collection;
    1316import java.util.Enumeration;
     
    4649import org.openstreetmap.josm.data.validation.OsmValidator;
    4750import org.openstreetmap.josm.data.validation.TestError;
    4851import org.openstreetmap.josm.data.validation.ValidatorVisitor;
     52import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil;
    4953import org.openstreetmap.josm.gui.MainApplication;
    5054import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    5155import org.openstreetmap.josm.gui.PopupMenuHandler;
     
    6367import org.openstreetmap.josm.tools.ImageProvider;
    6468import org.openstreetmap.josm.tools.InputMapUtils;
    6569import org.openstreetmap.josm.tools.JosmRuntimeException;
     70import org.openstreetmap.josm.tools.Logging;
    6671import org.openstreetmap.josm.tools.Shortcut;
    6772import org.xml.sax.SAXException;
    6873
     
    8590    private final SideButton fixButton;
    8691    /** The ignore button */
    8792    private final SideButton ignoreButton;
     93    /** The reset ignorelist button */
     94    private final SideButton resetignorelistButton;
    8895    /** The select button */
    8996    private final SideButton selectButton;
    9097    /** The lookup button */
     
    177184        } else {
    178185            ignoreButton = null;
    179186        }
     187
     188        resetignorelistButton = new SideButton(new AbstractAction() {
     189            int reset = 0;
     190            {
     191                toggle();
     192            }
     193
     194            public void toggle() {
     195                this.setEnabled(true);
     196                if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     197                    putValue(NAME, tr("Clear Ignore"));
     198                    putValue(SHORT_DESCRIPTION, tr("Clear ignore list"));
     199                    new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     200                    reset = 1;
     201                } else {
     202                    File ignoredErrors = new File(OsmValidator.getValidatorDir());
     203                    ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors.bak");
     204                    if (ignoredErrors.isFile()) {
     205                        putValue(NAME, tr("Restore Ignore"));
     206                        putValue(SHORT_DESCRIPTION, tr("Restore ignore list"));
     207                        new ImageProvider("copy").getResource().attachImageIcon(this, true);
     208                        reset = 2;
     209                    } else if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     210                        putValue(NAME, tr("Save Ignore"));
     211                        putValue(SHORT_DESCRIPTION, tr("Save ignore list"));
     212                        new ImageProvider("save").getResource().attachImageIcon(this, true);
     213                        reset = 3;
     214                    } else {
     215                        putValue(NAME, tr("Ignore list modification"));
     216                        putValue(SHORT_DESCRIPTION, tr("Clear/Restore/Save the ignore list, depending upon various conditions"));
     217                        new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     218                        //this.setEnabled(false); // TODO enable when I figure out how to call from ignore
     219                    }
     220                }
     221            }
     222
     223            @Override
     224            public void actionPerformed(ActionEvent e) {
     225                if (reset == 1) {
     226                    resetErrorList();
     227                } else if (reset == 2) {
     228                    restoreErrorList();
     229                } else if (reset == 3 && !OsmValidator.getIgnoredErrors().isEmpty()) {
     230                    OsmValidator.saveIgnoredErrors();
     231                } else if (reset == 0) {
     232                    // Do nothing
     233                }
     234                toggle();
     235            }
     236        });
     237        buttons.add(resetignorelistButton);
    180238        createLayout(tree, true, buttons);
    181239    }
    182240
     
    285343    }
    286344
    287345    /**
     346     * Prompt to rerun the validator when the ignore list changes
     347     */
     348    public void rerunValidatorPrompt() {
     349        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     350                "rerun_validation_when_ignorelist_changed",
     351                MainApplication.getMainFrame(),
     352                tr("Should the validation be rerun?"),
     353                tr("ignorederrors list 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    /**
     364     * Reset the error list by deleting ignorederrors
     365     */
     366    public void resetErrorList() {
     367        OsmValidator.saveIgnoredErrors();
     368        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     369        if (!ignoredErrors.isFile()) return;
     370        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     371        try {
     372            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     373        } catch (IOException e) {
     374            ignoredErrors.delete();
     375        }
     376        OsmValidator.initialize();
     377        rerunValidatorPrompt();
     378    }
     379
     380    /**
     381     * Restore the error list by copying ignorederrors.bak to ignorederrors
     382     */
     383    public void restoreErrorList() {
     384        OsmValidator.saveIgnoredErrors();
     385        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     386        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     387        if (!ignoredErrors.isFile() || !ignoredErrorsBak.isFile()) return;
     388        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     389        try {
     390            Files.move(ignoredErrors.toPath(),
     391                    ignoredErrorsBak2.toPath(),
     392                    StandardCopyOption.REPLACE_EXISTING);
     393            if (ignoredErrorsBak.isFile()) {
     394                Files.move(ignoredErrorsBak.toPath(),
     395                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     396            }
     397            Files.move(ignoredErrorsBak2.toPath(),
     398                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     399        } catch (IOException e) {
     400            Logging.debug(e);
     401        }
     402        OsmValidator.initialize();
     403        rerunValidatorPrompt();
     404    }
     405
     406    /**
    288407     * Sets the selection of the map to the current selected items.
    289408     */
    290409    @SuppressWarnings("unchecked")
  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

     
    241241    }
    242242
    243243    /**
     244     * Get the list of all ignored errors
     245     * @return The <code>Collection&ltString&gt</code> of errors that are ignored
     246     */
     247    public static Collection<String> getIgnoredErrors() {
     248        return ignoredErrors;
     249    }
     250
     251    /**
    244252     * Saves the names of the ignored errors to a file
    245253     */
    246254    public static void saveIgnoredErrors() {