Ticket #17268: clear_ignored_errors_v7.patch

File clear_ignored_errors_v7.patch, 7.7 KB (added by taylor.smock, 7 years ago)

Add check to avoid crash when there is no osm data and change the name of the question window

  • 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        if (!validateAction.isEnabled()) return;
     350        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     351                "rerun_validation_when_ignorelist_changed",
     352                MainApplication.getMainFrame(),
     353                "<hmtl><h3>" + tr("Should the validation be rerun?") + "</h3></html>",
     354                tr("Ignored error filter changed"),
     355                JOptionPane.YES_NO_CANCEL_OPTION,
     356                JOptionPane.QUESTION_MESSAGE,
     357                null,
     358                null);
     359        if (answer == JOptionPane.YES_OPTION) {
     360            validateAction.doValidate(true);
     361        }
     362    }
     363
     364    /**
     365     * Reset the error list by deleting ignorederrors
     366     */
     367    public void resetErrorList() {
     368        OsmValidator.saveIgnoredErrors();
     369        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     370        if (!ignoredErrors.isFile()) return;
     371        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     372        try {
     373            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     374        } catch (IOException e) {
     375            ignoredErrors.delete();
     376        }
     377        OsmValidator.initialize();
     378        rerunValidatorPrompt();
     379    }
     380
     381    /**
     382     * Restore the error list by copying ignorederrors.bak to ignorederrors
     383     */
     384    public void restoreErrorList() {
     385        OsmValidator.saveIgnoredErrors();
     386        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     387        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     388        if (!ignoredErrors.isFile() || !ignoredErrorsBak.isFile()) return;
     389        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     390        try {
     391            Files.move(ignoredErrors.toPath(),
     392                    ignoredErrorsBak2.toPath(),
     393                    StandardCopyOption.REPLACE_EXISTING);
     394            if (ignoredErrorsBak.isFile()) {
     395                Files.move(ignoredErrorsBak.toPath(),
     396                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     397            }
     398            Files.move(ignoredErrorsBak2.toPath(),
     399                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     400        } catch (IOException e) {
     401            Logging.debug(e);
     402        }
     403        OsmValidator.initialize();
     404        rerunValidatorPrompt();
     405    }
     406
     407    /**
    288408     * Sets the selection of the map to the current selected items.
    289409     */
    290410    @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() {