Ticket #17268: clear_ignored_errors_v12.2.patch

File clear_ignored_errors_v12.2.patch, 10.2 KB (added by taylor.smock, 7 years ago)

Use ValidatorPrefHelper for preferences, Cancel appears to work properly in the rerunValidator dialogue, still uses a .bak preference key, and fixes a crash when the ignorelist is empty.

  • src/org/openstreetmap/josm/data/preferences/sources/ValidatorPrefHelper.java

     
    1010import java.util.Map;
    1111
    1212import org.openstreetmap.josm.data.preferences.BooleanProperty;
     13import org.openstreetmap.josm.data.preferences.ListProperty;
    1314import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
    1415
    1516/**
     
    4445    /** The preferences for ignored severity other */
    4546    public static final BooleanProperty PREF_OTHER = new BooleanProperty(PREFIX + ".other", false);
    4647
     48    /** The preferences key for the ignorelist */
     49    public static final ListProperty PREF_IGNORELIST = new ListProperty(PREFIX + ".ignorelist", null);
     50
     51    /** The preferences key for the ignorelist backup */
     52    public static final ListProperty PREF_IGNORELIST_BACKUP = new ListProperty(PREFIX + ".ignorelist.bak", null);
    4753    /**
    4854     * The preferences key for enabling the permanent filtering
    4955     * of the displayed errors in the tree regarding the current selection
  • 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            List<String> errors = ValidatorPrefHelper.PREF_IGNORELIST.get();
     211            if (errors != null && !errors.isEmpty()) {
     212                ignoredErrors.addAll(ValidatorPrefHelper.PREF_IGNORELIST.get());
     213            }
    207214            Path path = Paths.get(getValidatorDir()).resolve("ignorederrors");
    208215            try {
    209216                if (path.toFile().exists()) {
    210217                    try {
    211218                        ignoredErrors.addAll(Files.readAllLines(path, StandardCharsets.UTF_8));
     219                        saveIgnoredErrors();
     220                        Files.deleteIfExists(path);
    212221                    } catch (FileNotFoundException e) {
    213222                        Logging.debug(Logging.getErrorMessage(e));
    214223                    } catch (IOException e) {
     
    241250    }
    242251
    243252    /**
     253     * Get the list of all ignored errors
     254     * @return The <code>Collection&ltString&gt</code> of errors that are ignored
     255     */
     256    public static Collection<String> getIgnoredErrors() {
     257        return ignoredErrors;
     258    }
     259
     260    /**
     261     * Reset the error list by deleting ignorederrors
     262     */
     263    public static void resetErrorList() {
     264        saveIgnoredErrors();
     265        backupErrorList();
     266        ValidatorPrefHelper.PREF_USE_IGNORE.put(null);
     267        OsmValidator.initialize();
     268    }
     269
     270    /**
     271     * Restore the error list by copying ignorederrors.bak to ignorederrors
     272     */
     273    public static void restoreErrorList() {
     274        saveIgnoredErrors();
     275        List<String> tlist = ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.get();
     276        backupErrorList();
     277        ValidatorPrefHelper.PREF_IGNORELIST.put(tlist);
     278        OsmValidator.initialize();
     279    }
     280
     281    private static void backupErrorList() {
     282        List<String> tlist = ValidatorPrefHelper.PREF_IGNORELIST.get();
     283        if (tlist.isEmpty()) tlist = null;
     284        ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.put(tlist);
     285    }
     286
     287    /**
    244288     * Saves the names of the ignored errors to a file
    245289     */
    246290    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         }
     291        List<String> list = new ArrayList<>(ignoredErrors);
     292        Collections.sort(list);
     293        ValidatorPrefHelper.PREF_IGNORELIST.put(list);
    254294    }
    255295
    256296    /**
  • 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                int reset;
     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                        List<String> ignoredErrors = ValidatorPrefHelper.PREF_IGNORELIST_BACKUP.get();
     196                        if (!ignoredErrors.isEmpty()) {
     197                            putValue(NAME, tr("Restore Ignore"));
     198                            putValue(SHORT_DESCRIPTION, tr("Restore ignore list"));
     199                            new ImageProvider("copy").getResource().attachImageIcon(this, true);
     200                            reset = 2;
     201                        } else if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     202                            putValue(NAME, tr("Save Ignore"));
     203                            putValue(SHORT_DESCRIPTION, tr("Save ignore list"));
     204                            new ImageProvider("save").getResource().attachImageIcon(this, true);
     205                            reset = 3;
     206                        } else {
     207                            putValue(NAME, tr("Ignore list modification"));
     208                            putValue(SHORT_DESCRIPTION, tr("Clear/Restore/Save the ignore list, depending upon various conditions"));
     209                            new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     210                            this.setEnabled(false);
     211                        }
     212                    }
     213                }
     214
     215                @Override
     216                public void actionPerformed(ActionEvent e) {
     217                    if (e != null) {
     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                        }
     225                        if (reset == 1 || reset == 2) rerunValidatorPrompt();
     226                    }
     227                    toggle();
     228                }
     229            });
     230            buttons.add(resetignorelistButton);
    177231        } else {
    178232            ignoreButton = null;
     233            resetignorelistButton = null;
    179234        }
     235
    180236        createLayout(tree, true, buttons);
    181237    }
    182238
     
    280336        if (changed.get()) {
    281337            tree.resetErrors();
    282338            OsmValidator.saveIgnoredErrors();
     339            if (resetignorelistButton != null) {
     340                resetignorelistButton.getAction().actionPerformed(null);
     341            }
    283342            invalidateValidatorLayers();
    284343        }
    285344    }
    286345
    287346    /**
     347     * Prompt to rerun the validator when the ignore list changes
     348     */
     349    public void rerunValidatorPrompt() {
     350        MapFrame map = MainApplication.getMap();
     351        List<TestError> errors = map.validatorDialog.tree.getErrors();
     352        if (!validateAction.isEnabled() || errors == null || errors.isEmpty()) return;
     353        final int answer = ConditionalOptionPaneUtil.showOptionDialog(
     354                "rerun_validation_when_ignorelist_changed",
     355                MainApplication.getMainFrame(),
     356                "<hmtl><h3>" + tr("Should the validation be rerun?") + "</h3></html>",
     357                tr("Ignored error filter changed"),
     358                JOptionPane.YES_NO_CANCEL_OPTION,
     359                JOptionPane.QUESTION_MESSAGE,
     360                null,
     361                null);
     362        if (answer == JOptionPane.YES_OPTION) {
     363            validateAction.doValidate(true);
     364        }
     365    }
     366
     367    /**
    288368     * Sets the selection of the map to the current selected items.
    289369     */
    290     @SuppressWarnings("unchecked")
    291370    private void setSelectedItems() {
    292371        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    293372        if (tree == null || ds == null)