| | 1 | // License: GPL. For details, see LICENSE file. |
| | 2 | package org.openstreetmap.josm.gui.dialogs; |
| | 3 | |
| | 4 | import static org.openstreetmap.josm.tools.I18n.tr; |
| | 5 | |
| | 6 | import java.awt.GridBagLayout; |
| | 7 | import java.awt.Rectangle; |
| | 8 | import java.awt.event.ActionEvent; |
| | 9 | import java.awt.event.MouseAdapter; |
| | 10 | import java.awt.event.MouseEvent; |
| | 11 | import java.util.Enumeration; |
| | 12 | import java.util.HashMap; |
| | 13 | import java.util.List; |
| | 14 | import java.util.Locale; |
| | 15 | import java.util.TreeMap; |
| | 16 | |
| | 17 | import javax.swing.AbstractAction; |
| | 18 | import javax.swing.ImageIcon; |
| | 19 | import javax.swing.JMenuItem; |
| | 20 | import javax.swing.JOptionPane; |
| | 21 | import javax.swing.JPanel; |
| | 22 | import javax.swing.JPopupMenu; |
| | 23 | import javax.swing.JScrollPane; |
| | 24 | import javax.swing.JTree; |
| | 25 | import javax.swing.tree.DefaultMutableTreeNode; |
| | 26 | import javax.swing.tree.TreeModel; |
| | 27 | import javax.swing.tree.TreeNode; |
| | 28 | import javax.swing.tree.TreePath; |
| | 29 | |
| | 30 | import org.openstreetmap.josm.actions.ValidateAction; |
| | 31 | import org.openstreetmap.josm.data.validation.OsmValidator; |
| | 32 | import org.openstreetmap.josm.data.validation.TestError; |
| | 33 | import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; |
| | 34 | import org.openstreetmap.josm.gui.ExtendedDialog; |
| | 35 | import org.openstreetmap.josm.gui.MainApplication; |
| | 36 | import org.openstreetmap.josm.gui.MapFrame; |
| | 37 | import org.openstreetmap.josm.gui.util.GuiHelper; |
| | 38 | import org.openstreetmap.josm.tools.GBC; |
| | 39 | import org.openstreetmap.josm.tools.ImageProvider; |
| | 40 | import org.openstreetmap.josm.tools.Logging; |
| | 41 | |
| | 42 | |
| | 43 | /** |
| | 44 | * A management window for the validator's ignorelist |
| | 45 | * @author Taylor Smock |
| | 46 | * @since xxx |
| | 47 | */ |
| | 48 | public class ValidatorListManagementDialog extends ExtendedDialog { |
| | 49 | enum BUTTONS { |
| | 50 | OK(0, tr("OK"), new ImageProvider("ok")), |
| | 51 | CLEAR(1, tr("Clear All"), new ImageProvider("dialogs", "fix")), |
| | 52 | RESTORE(2, tr("Restore"), new ImageProvider("copy")), |
| | 53 | CANCEL(3, tr("Cancel"), new ImageProvider("cancel")); |
| | 54 | |
| | 55 | private int index; |
| | 56 | private String name; |
| | 57 | private ImageIcon icon; |
| | 58 | |
| | 59 | BUTTONS(int index, String name, ImageProvider image) { |
| | 60 | this.index = index; |
| | 61 | this.name = name; |
| | 62 | this.icon = image.getResource().getImageIcon(); |
| | 63 | } |
| | 64 | |
| | 65 | public ImageIcon getImageIcon() { |
| | 66 | return icon; |
| | 67 | } |
| | 68 | |
| | 69 | public int getIndex() { |
| | 70 | return index; |
| | 71 | } |
| | 72 | |
| | 73 | public String getName() { |
| | 74 | return name; |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | private static final String[] BUTTON_TEXTS = {BUTTONS.OK.getName(), BUTTONS.CLEAR.getName(), |
| | 79 | BUTTONS.RESTORE.getName(), BUTTONS.CANCEL.getName() |
| | 80 | }; |
| | 81 | |
| | 82 | private static final ImageIcon[] BUTTON_IMAGES = {BUTTONS.OK.getImageIcon(), BUTTONS.CLEAR.getImageIcon(), |
| | 83 | BUTTONS.RESTORE.getImageIcon(), BUTTONS.CANCEL.getImageIcon() |
| | 84 | }; |
| | 85 | |
| | 86 | private final JPanel panel = new JPanel(new GridBagLayout()); |
| | 87 | |
| | 88 | private final JTree ignoreErrors; |
| | 89 | |
| | 90 | private final String type; |
| | 91 | |
| | 92 | /** |
| | 93 | * Create a new {@link ValidatorListManagementDialog} |
| | 94 | * @param type The type of list to create (first letter may or may not be |
| | 95 | * capitalized, it is put into all lowercase after building the title) |
| | 96 | */ |
| | 97 | public ValidatorListManagementDialog(String type) { |
| | 98 | super(MainApplication.getMainFrame(), tr("Validator {0} List Management", type), BUTTON_TEXTS, false); |
| | 99 | this.type = type.toLowerCase(Locale.ENGLISH); |
| | 100 | setButtonIcons(BUTTON_IMAGES); |
| | 101 | |
| | 102 | ignoreErrors = buildList(); |
| | 103 | JScrollPane scroll = GuiHelper.embedInVerticalScrollPane(ignoreErrors); |
| | 104 | |
| | 105 | panel.add(scroll, GBC.eol().fill(GBC.BOTH).anchor(GBC.CENTER)); |
| | 106 | setContent(panel); |
| | 107 | setDefaultButton(1); |
| | 108 | setupDialog(); |
| | 109 | showDialog(); |
| | 110 | } |
| | 111 | |
| | 112 | @Override |
| | 113 | public void buttonAction(int buttonIndex, ActionEvent evt) { |
| | 114 | // Currently OK/Cancel buttons do nothing |
| | 115 | final int answer; |
| | 116 | if (buttonIndex == BUTTONS.RESTORE.getIndex()) { |
| | 117 | dispose(); |
| | 118 | answer = rerunValidatorPrompt(); |
| | 119 | if (answer == JOptionPane.YES_OPTION || answer == JOptionPane.NO_OPTION) { |
| | 120 | OsmValidator.restoreErrorList(); |
| | 121 | } |
| | 122 | } else if (buttonIndex == BUTTONS.CLEAR.getIndex()) { |
| | 123 | dispose(); |
| | 124 | answer = rerunValidatorPrompt(); |
| | 125 | if (answer == JOptionPane.YES_OPTION || answer == JOptionPane.NO_OPTION) { |
| | 126 | OsmValidator.resetErrorList(); |
| | 127 | } |
| | 128 | } else if (buttonIndex == BUTTONS.OK.getIndex()) { |
| | 129 | HashMap<String, String> errors = OsmValidator.getIgnoredErrors(); |
| | 130 | HashMap<String, String> tree = buildIgnore(ignoreErrors); |
| | 131 | if (!errors.equals(tree)) { |
| | 132 | answer = rerunValidatorPrompt(); |
| | 133 | if (answer == JOptionPane.YES_OPTION || answer == JOptionPane.NO_OPTION) { |
| | 134 | OsmValidator.resetErrorList(); |
| | 135 | Logging.setLogLevel(Logging.LEVEL_DEBUG); |
| | 136 | Logging.debug("Starting to rebuild the error list of size {0}", tree.size()); |
| | 137 | tree.forEach((ignore, description) -> { |
| | 138 | Logging.debug("Adding {0} with description {1}", ignore, description); |
| | 139 | OsmValidator.addIgnoredError(ignore, description); |
| | 140 | }); |
| | 141 | OsmValidator.saveIgnoredErrors(); |
| | 142 | OsmValidator.initialize(); |
| | 143 | } |
| | 144 | } |
| | 145 | dispose(); |
| | 146 | } else { |
| | 147 | super.buttonAction(buttonIndex, evt); |
| | 148 | } |
| | 149 | } |
| | 150 | |
| | 151 | /** |
| | 152 | * Build a {@code HashMap} from a tree of ignored errors |
| | 153 | * @param tree The JTree of ignored errors |
| | 154 | * @return A {@code HashMap} of the ignored errors for comparison |
| | 155 | */ |
| | 156 | public HashMap<String, String> buildIgnore(JTree tree) { |
| | 157 | TreeModel model = tree.getModel(); |
| | 158 | DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot(); |
| | 159 | return buildIgnore(model, root); |
| | 160 | } |
| | 161 | |
| | 162 | private HashMap<String, String> buildIgnore(TreeModel model, DefaultMutableTreeNode node) { |
| | 163 | Logging.setLogLevel(Logging.LEVEL_DEBUG); |
| | 164 | HashMap<String, String> rHashMap = new HashMap<>(); |
| | 165 | |
| | 166 | String osmids = node.getUserObject().toString(); |
| | 167 | String description = ""; |
| | 168 | |
| | 169 | if (!model.getRoot().equals(node)) description = ((DefaultMutableTreeNode) node.getParent()).getUserObject().toString(); |
| | 170 | if (!osmids.matches("^[0-9]+_.*")) osmids = ""; |
| | 171 | |
| | 172 | for (int i = 0; i < model.getChildCount(node); i++) { |
| | 173 | DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(node, i); |
| | 174 | if (model.getChildCount(child) == 0) { |
| | 175 | String ignoreName = child.getUserObject().toString(); |
| | 176 | if (ignoreName.matches("^(r|w|n)_.*")) { |
| | 177 | osmids += ":" + child.getUserObject().toString(); |
| | 178 | } else if (ignoreName.matches("^[0-9]+_.*")) { |
| | 179 | rHashMap.put(ignoreName, description); |
| | 180 | } |
| | 181 | } else { |
| | 182 | rHashMap.putAll(buildIgnore(model, child)); |
| | 183 | } |
| | 184 | } |
| | 185 | if (!osmids.equals("") && osmids.indexOf(":") != 0) rHashMap.put(osmids, description); |
| | 186 | return rHashMap; |
| | 187 | } |
| | 188 | |
| | 189 | private DefaultMutableTreeNode inTree(DefaultMutableTreeNode root, String name) { |
| | 190 | @SuppressWarnings("unchecked") |
| | 191 | Enumeration<TreeNode> trunks = root.children(); |
| | 192 | while (trunks.hasMoreElements()) { |
| | 193 | TreeNode ttrunk = trunks.nextElement(); |
| | 194 | if (ttrunk instanceof DefaultMutableTreeNode) { |
| | 195 | DefaultMutableTreeNode trunk = (DefaultMutableTreeNode) ttrunk; |
| | 196 | if (name.equals(trunk.getUserObject())) { |
| | 197 | return trunk; |
| | 198 | } |
| | 199 | } |
| | 200 | } |
| | 201 | return new DefaultMutableTreeNode(name); |
| | 202 | } |
| | 203 | |
| | 204 | /** |
| | 205 | * Build a JTree with a list |
| | 206 | * @return <type>list as a {@code JTree} |
| | 207 | */ |
| | 208 | public JTree buildList() { |
| | 209 | TreeMap<String, String> map = new TreeMap<>(); |
| | 210 | if ("ignore".equals(type)) { |
| | 211 | HashMap<String, String> tmap; |
| | 212 | tmap = OsmValidator.getIgnoredErrors(); |
| | 213 | if (tmap.isEmpty()) { |
| | 214 | OsmValidator.initialize(); |
| | 215 | tmap = OsmValidator.getIgnoredErrors(); |
| | 216 | } |
| | 217 | map.putAll(tmap); |
| | 218 | } else { |
| | 219 | Logging.error(tr("Cannot understand the following type: {0}", type)); |
| | 220 | return null; |
| | 221 | } |
| | 222 | DefaultMutableTreeNode root = new DefaultMutableTreeNode(tr("{0} list", type)); |
| | 223 | |
| | 224 | for (String key : map.keySet()) { |
| | 225 | String value = map.get(key); |
| | 226 | String[] osmobjects = key.split(":(r|w|n)_"); |
| | 227 | DefaultMutableTreeNode trunk; |
| | 228 | DefaultMutableTreeNode branch; |
| | 229 | |
| | 230 | if (value != null && !value.isEmpty()) { |
| | 231 | trunk = inTree(root, value); |
| | 232 | branch = inTree(trunk, osmobjects[0]); |
| | 233 | trunk.add(branch); |
| | 234 | } else { |
| | 235 | trunk = inTree(root, osmobjects[0]); |
| | 236 | branch = trunk; |
| | 237 | } |
| | 238 | for (int i = 1; i < osmobjects.length; i++) { |
| | 239 | String osmid = osmobjects[i]; |
| | 240 | int index = key.indexOf(osmid); |
| | 241 | char type = key.charAt(index - 2); |
| | 242 | DefaultMutableTreeNode leaf = new DefaultMutableTreeNode(type + "_" + osmid); |
| | 243 | branch.add(leaf); |
| | 244 | } |
| | 245 | root.add(trunk); |
| | 246 | } |
| | 247 | JTree tree = new JTree(root); |
| | 248 | tree.setRootVisible(false); |
| | 249 | tree.setShowsRootHandles(true); |
| | 250 | tree.addMouseListener(new MouseAdapter() { |
| | 251 | @Override |
| | 252 | public void mousePressed(MouseEvent e) { |
| | 253 | if (e.isPopupTrigger()) { |
| | 254 | TreePath[] paths = tree.getSelectionPaths(); |
| | 255 | if (paths == null) return; |
| | 256 | Rectangle bounds = tree.getUI().getPathBounds(tree, paths[0]); |
| | 257 | if (bounds != null && bounds.contains(e.getX(), e.getY())) { |
| | 258 | JPopupMenu menu = new JPopupMenu(); |
| | 259 | JMenuItem delete = new JMenuItem(new AbstractAction(tr("Delete")) { |
| | 260 | @Override |
| | 261 | public void actionPerformed(ActionEvent e1) { |
| | 262 | for (TreePath path : paths) { |
| | 263 | tree.clearSelection(); |
| | 264 | tree.addSelectionPath(path); |
| | 265 | DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); |
| | 266 | DefaultMutableTreeNode parent = (DefaultMutableTreeNode) node.getParent(); |
| | 267 | node.removeAllChildren(); |
| | 268 | while (node.getChildCount() == 0) { |
| | 269 | node.removeFromParent(); |
| | 270 | node = parent; |
| | 271 | if (parent.isRoot()) break; |
| | 272 | parent = (DefaultMutableTreeNode) node.getParent(); |
| | 273 | } |
| | 274 | } |
| | 275 | tree.updateUI(); |
| | 276 | } |
| | 277 | }); |
| | 278 | menu.add(delete); |
| | 279 | menu.show(e.getComponent(), e.getX(), e.getY()); |
| | 280 | } |
| | 281 | } |
| | 282 | } |
| | 283 | }); |
| | 284 | return tree; |
| | 285 | } |
| | 286 | |
| | 287 | /** |
| | 288 | * Prompt to rerun the validator when the ignore list changes |
| | 289 | * @return {@code JOptionPane.YES_OPTION}, {@code JOptionPane.NO_OPTION}, |
| | 290 | * or {@code JOptionPane.CANCEL_OPTION} |
| | 291 | */ |
| | 292 | public int rerunValidatorPrompt() { |
| | 293 | MapFrame map = MainApplication.getMap(); |
| | 294 | List<TestError> errors = map.validatorDialog.tree.getErrors(); |
| | 295 | ValidateAction validateAction = ValidatorDialog.validateAction; |
| | 296 | if (!validateAction.isEnabled() || errors == null || errors.isEmpty()) return JOptionPane.NO_OPTION; |
| | 297 | final int answer = ConditionalOptionPaneUtil.showOptionDialog( |
| | 298 | "rerun_validation_when_ignorelist_changed", |
| | 299 | MainApplication.getMainFrame(), |
| | 300 | tr("{0}Should the validation be rerun?{1}", "<hmtl><h3>", "</h3></html>"), |
| | 301 | tr("Ignored error filter changed"), |
| | 302 | JOptionPane.YES_NO_CANCEL_OPTION, |
| | 303 | JOptionPane.QUESTION_MESSAGE, |
| | 304 | null, |
| | 305 | null); |
| | 306 | if (answer == JOptionPane.YES_OPTION) { |
| | 307 | validateAction.doValidate(true); |
| | 308 | } |
| | 309 | return answer; |
| | 310 | } |
| | 311 | } |