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