Ticket #17268: clear_ignored_errors_v5.patch

File clear_ignored_errors_v5.patch, 6.5 KB (added by taylor.smock, 7 years ago)

Switch from size() > 0 to !<var>.isEmpty() and remove some tree lines.

  • 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                this.setEnabled(true);
     195                if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     196                    putValue(NAME, tr("Clear Ignore"));
     197                    putValue(SHORT_DESCRIPTION, tr("Clear ignore list"));
     198                    new ImageProvider("dialogs", "fix").getResource().attachImageIcon(this, true);
     199                    reset = 1;
     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                    } else if (!OsmValidator.getIgnoredErrors().isEmpty()) {
     209                        putValue(NAME, tr("Save Ignore"));
     210                        putValue(SHORT_DESCRIPTION, tr("Save ignore list"));
     211                        new ImageProvider("save").getResource().attachImageIcon(this, true);
     212                        reset = 3;
     213                    } else {
     214                        putValue(NAME, tr("Ignore list modification"));
     215                        putValue(SHORT_DESCRIPTION, tr("Clear/Restore/Save the ignore list, depending upon various conditions"));
     216                        new ImageProvider("dialogs", "validator").getResource().attachImageIcon(this, true);
     217                        //this.setEnabled(false); // TODO enable when I figure out how to call from ignore
     218                    }
     219                }
     220            }
     221
     222            @Override
     223            public void actionPerformed(ActionEvent e) {
     224                if (reset == 1) {
     225                    resetErrorList();
     226                } else if (reset == 2) {
     227                    restoreErrorList();
     228                } else if (reset == 3 && !OsmValidator.getIgnoredErrors().isEmpty()) {
     229                    OsmValidator.saveIgnoredErrors();
     230                } else if (reset == 0) {
     231                    // Do nothing
     232                }
     233                toggle();
     234            }
     235        });
     236        buttons.add(resetignorelistButton);
    180237        createLayout(tree, true, buttons);
    181238    }
    182239
     
    285342    }
    286343
    287344    /**
     345     * Reset the error list by deleting ignorederrors
     346     */
     347    public void resetErrorList() {
     348        OsmValidator.saveIgnoredErrors();
     349        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     350        if (!ignoredErrors.isFile()) return;
     351        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     352        try {
     353            Files.move(ignoredErrors.toPath(), ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     354        } catch (IOException e) {
     355            ignoredErrors.delete();
     356        }
     357        OsmValidator.initialize();
     358    }
     359
     360    /**
     361     * Restore the error list by copying ignorederrors.bak to ignorederrors
     362     */
     363    public void restoreErrorList() {
     364        OsmValidator.saveIgnoredErrors();
     365        File ignoredErrors = new File(OsmValidator.getValidatorDir(), "ignorederrors");
     366        File ignoredErrorsBak = new File(ignoredErrors.getAbsolutePath() + ".bak");
     367        if (!ignoredErrors.isFile() || !ignoredErrorsBak.isFile()) return;
     368        File ignoredErrorsBak2 = new File(ignoredErrorsBak.getAbsolutePath() + "2");
     369        try {
     370            Files.move(ignoredErrors.toPath(),
     371                    ignoredErrorsBak2.toPath(),
     372                    StandardCopyOption.REPLACE_EXISTING);
     373            if (ignoredErrorsBak.isFile()) {
     374                Files.move(ignoredErrorsBak.toPath(),
     375                        ignoredErrors.toPath(), StandardCopyOption.REPLACE_EXISTING);
     376            }
     377            Files.move(ignoredErrorsBak2.toPath(),
     378                    ignoredErrorsBak.toPath(), StandardCopyOption.REPLACE_EXISTING);
     379        } catch (IOException e) {
     380            Logging.debug(e);
     381        }
     382        OsmValidator.initialize();
     383    }
     384
     385    /**
    288386     * Sets the selection of the map to the current selected items.
    289387     */
    290388    @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() {