Ticket #17268: clear_ignored_errors_v4.patch

File clear_ignored_errors_v4.patch, 6.5 KB (added by taylor.smock, 5 years ago)

Change copy image to not use the dialogs directroy, hopefully fix the crash when ignorederrors files are not present, remove System.out.println() (I was using these as debug statements for "You are here"), change Reset to Clear, and (hopefully) change the displayed tree when switching.

  • 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;
     
    6366import org.openstreetmap.josm.tools.ImageProvider;
    6467import org.openstreetmap.josm.tools.InputMapUtils;
    6568import org.openstreetmap.josm.tools.JosmRuntimeException;
     69import org.openstreetmap.josm.tools.Logging;
    6670import org.openstreetmap.josm.tools.Shortcut;
    6771import org.xml.sax.SAXException;
    6872
     
    8589    private final SideButton fixButton;
    8690    /** The ignore button */
    8791    private final SideButton ignoreButton;
     92    /** The reset ignorelist button */
     93    private final SideButton resetignorelistButton;
    8894    /** The select button */
    8995    private final SideButton selectButton;
    9096    /** The lookup button */
     
    177183        } else {
    178184            ignoreButton = null;
    179185        }
     186
     187        resetignorelistButton = new SideButton(new AbstractAction() {
     188            int reset = 0;
     189            {
     190                toggle();
     191            }
     192
     193            public void toggle() {
     194                if (OsmValidator.getIgnoredErrors().size() > 0) {
     195                    putValue(NAME, tr("Clear Ignore"));
     196                    putValue(SHORT_DESCRIPTION, tr("Clear ignore list"));
     197                    new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     198                    reset = 1;
     199                    this.setEnabled(true);
     200                } else {
     201                    File ignoredErrors = new File(OsmValidator.getValidatorDir());
     202                    ignoredErrors = new File(ignoredErrors.getAbsolutePath() + File.separator + "ignorederrors.bak");
     203                    if (ignoredErrors.isFile()) {
     204                        putValue(NAME, tr("Restore Ignore"));
     205                        putValue(SHORT_DESCRIPTION, tr("Restore ignore list"));
     206                        new ImageProvider("copy").getResource().attachImageIcon(this, true);
     207                        reset = 2;
     208                        this.setEnabled(true);
     209                    } else if (OsmValidator.getIgnoredErrors().size() > 0) {
     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                        this.setEnabled(true);
     215                    } else {
     216                        putValue(NAME, tr("Ignore list modification"));
     217                        putValue(SHORT_DESCRIPTION, tr("Clear/Restore/Save the ignore list, depending upon various conditions"));
     218                        new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     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().size() > 0) {
     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     * Reset the error list by deleting ignorederrors
     347     */
     348    public void resetErrorList() {
     349        OsmValidator.saveIgnoredErrors();
     350        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     351        if (!ignoredErrors.isFile()) return;
     352        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     353        try {
     354            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     355        } catch (IOException e) {
     356            ignoredErrors.delete();
     357        }
     358        OsmValidator.initialize();
     359        tree.resetErrors();
     360    }
     361
     362    /**
     363     * Restore the error list by copying ignorederrors.bak to ignorederrors
     364     */
     365    public void restoreErrorList() {
     366        OsmValidator.saveIgnoredErrors();
     367        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     368        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     369        if (!ignoredErrors.isFile() || !ignoredErrorsBak.isFile()) return;
     370        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     371        try {
     372            Files.move(ignoredErrors.toPath(),
     373                    ignoredErrorsBak2.toPath(),
     374                    StandardCopyOption.REPLACE_EXISTING);
     375            if (ignoredErrorsBak.isFile()) {
     376                Files.move(ignoredErrorsBak.toPath(),
     377                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     378            }
     379            Files.move(ignoredErrorsBak2.toPath(),
     380                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     381        } catch (IOException e) {
     382            Logging.debug(e);
     383        }
     384        OsmValidator.initialize();
     385        tree.resetErrors();
     386    }
     387
     388    /**
    288389     * Sets the selection of the map to the current selected items.
    289390     */
    290391    @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() {