Ticket #17268: clear_ignored_errors_v2.patch

File clear_ignored_errors_v2.patch, 6.8 KB (added by taylor.smock, 7 years ago)

Add additional dialogue options to reset/restore (doesn't ask for confirmation to reset)

  • src/org/openstreetmap/josm/actions/ResetValidationIgnoreList.java

     
     1// License: GPL. For details, see LICENSE file.
     2package org.openstreetmap.josm.actions;
     3
     4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
     5import static org.openstreetmap.josm.tools.I18n.tr;
     6
     7import java.awt.Dimension;
     8import java.awt.event.ActionEvent;
     9import java.awt.event.KeyEvent;
     10import java.io.File;
     11import java.io.IOException;
     12import java.nio.charset.StandardCharsets;
     13import java.nio.file.Files;
     14import java.nio.file.Paths;
     15import java.nio.file.StandardCopyOption;
     16import java.util.List;
     17
     18import org.openstreetmap.josm.data.validation.OsmValidator;
     19import org.openstreetmap.josm.gui.ExtendedDialog;
     20import org.openstreetmap.josm.gui.MainApplication;
     21import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
     22import org.openstreetmap.josm.gui.util.GuiHelper;
     23import org.openstreetmap.josm.spi.preferences.Config;
     24import org.openstreetmap.josm.tools.Shortcut;
     25
     26/**
     27 * Resets the ignored errors list
     28 *
     29 * @author Taylor Smock
     30 */
     31public class ResetValidationIgnoreList extends JosmAction {
     32
     33    /**
     34     * Constructs a new {@code ResetValidationIgnoreList}
     35     */
     36    public ResetValidationIgnoreList() {
     37        super(
     38                tr("Reset ignored errors list"),
     39                "fix",
     40                tr("Reset the ignored errors list"),
     41                Shortcut.registerShortcut("help:resetignorrederrors", tr("Help: {0}",
     42                        tr("Reset ignored errors")), KeyEvent.CHAR_UNDEFINED, Shortcut.NONE), false);
     43
     44        setHelpId(ht("/Action/ResetValidationIgnoreList"));
     45        putValue("toolbar", "help/resetvalidationignorelist");
     46        MainApplication.getToolbar().register(this);
     47    }
     48
     49    @Override
     50    public void actionPerformed(ActionEvent e) {
     51        StringBuilder text = new StringBuilder();
     52        File ignoredErrors = new File(Config.getDirs().getUserDataDirectory(true), "validator");
     53        ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors");
     54        try {
     55            List<String> lines = Files.readAllLines(Paths.get(ignoredErrors.getAbsolutePath()), StandardCharsets.UTF_8);
     56            for (String line : lines) {
     57                text.append(line);
     58                text.append(System.lineSeparator());
     59            }
     60        } catch (IOException e1) {
     61            text.append(tr("There was no ignored errors file"));
     62        }
     63
     64        DebugTextDisplay ta = new DebugTextDisplay(text.toString());
     65        ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
     66                tr("Ignored Errors"),
     67                tr("Reset"), tr("Restore"), tr("Close"));
     68        ed.setButtonIcons("fix", "copy", "cancel");
     69        ed.setContent(ta, false);
     70        ed.setMinimumSize(new Dimension(380, 200));
     71        ed.setPreferredSize(new Dimension(700, MainApplication.getMainFrame().getHeight()-50));
     72        switch (ed.showDialog().getValue()) {
     73            case 1: resetErrorList(); break;
     74            case 2: restoreErrorList(); break;
     75            default:
     76        }
     77    GuiHelper.destroyComponents(ed, false);
     78    }
     79
     80    /**
     81     * Reset the error list by deleting ignorederrors
     82     */
     83    public void resetErrorList() {
     84        OsmValidator.saveIgnoredErrors();
     85        File ignoredErrors = new File(Config.getDirs().getUserDataDirectory(true), "validator");
     86        ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors");
     87        if (!ignoredErrors.isFile()) return;
     88        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     89        try {
     90            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     91        } catch (IOException e) {
     92            System.out.println(tr("We couldn''t save ignorederrors"));
     93            ignoredErrors.delete();
     94        }
     95        OsmValidator.initialize();
     96    }
     97
     98    /**
     99     * Restore the error list by copying ignorederrors.bak to ignorederrors
     100     */
     101    public void restoreErrorList() {
     102        OsmValidator.saveIgnoredErrors();
     103        File ignoredErrors = new File(Config.getDirs().getUserDataDirectory(true), "validator");
     104        ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors");
     105        if (!ignoredErrors.isFile()) return;
     106        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     107        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     108        try {
     109            Files.move(ignoredErrors.toPath(),
     110                    ignoredErrorsBak2.toPath(),
     111                    StandardCopyOption.REPLACE_EXISTING);
     112            if (ignoredErrorsBak.isFile()) {
     113                Files.move(ignoredErrorsBak.toPath(),
     114                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     115            }
     116            Files.move(ignoredErrorsBak2.toPath(),
     117                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     118        } catch (IOException e) {
     119            System.out.println(tr("We were unable to restore ignorederrors"));
     120            e.printStackTrace();
     121        }
     122        OsmValidator.initialize();
     123    }
     124}
  • src/org/openstreetmap/josm/gui/MainMenu.java

     
    8282import org.openstreetmap.josm.actions.RedoAction;
    8383import org.openstreetmap.josm.actions.ReorderImageryLayersAction;
    8484import org.openstreetmap.josm.actions.ReportBugAction;
     85import org.openstreetmap.josm.actions.ResetValidationIgnoreList;
    8586import org.openstreetmap.josm.actions.RestartAction;
    8687import org.openstreetmap.josm.actions.ReverseWayAction;
    8788import org.openstreetmap.josm.actions.SaveAction;
     
    317318    public final AboutAction about = new AboutAction();
    318319    /** Help / Show Status Report */
    319320    public final ShowStatusReportAction statusreport = new ShowStatusReportAction();
     321    /** Help / Reset validation ignore list */
     322    public final ResetValidationIgnoreList resetignore = new ResetValidationIgnoreList();
    320323    /** Help / Report bug */
    321324    public final ReportBugAction reportbug = new ReportBugAction();
    322325
     
    834837        add(helpMenu, new MenuItemSearchDialog.Action());
    835838        helpMenu.addSeparator();
    836839        add(helpMenu, statusreport);
     840        add(helpMenu, resetignore);
    837841        add(helpMenu, reportbug);
    838842        helpMenu.addSeparator();
    839843