| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.actions; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht; |
| | 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 6 | |
| | 7 | import java.awt.Dimension; |
| | 8 | import java.awt.event.ActionEvent; |
| | 9 | import java.awt.event.KeyEvent; |
| | 10 | import java.io.File; |
| | 11 | import java.io.IOException; |
| | 12 | import java.nio.charset.StandardCharsets; |
| | 13 | import java.nio.file.Files; |
| | 14 | import java.nio.file.Paths; |
| | 15 | import java.nio.file.StandardCopyOption; |
| | 16 | import java.util.List; |
| | 17 | |
| | 18 | import org.openstreetmap.josm.data.validation.OsmValidator; |
| | 19 | import org.openstreetmap.josm.gui.ExtendedDialog; |
| | 20 | import org.openstreetmap.josm.gui.MainApplication; |
| | 21 | import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay; |
| | 22 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| | 23 | import org.openstreetmap.josm.spi.preferences.Config; |
| | 24 | import org.openstreetmap.josm.tools.Shortcut; |
| | 25 | |
| | 26 | /** |
| | 27 | * Resets the ignored errors list |
| | 28 | * |
| | 29 | * @author Taylor Smock |
| | 30 | */ |
| | 31 | public 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 | } |