Ignore:
Timestamp:
2018-06-17T14:14:33+02:00 (6 years ago)
Author:
Don-vip
Message:

refactor/cleanup validator tree API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/ValidatorDialog.java

    r13925 r13940  
    1616import java.util.List;
    1717import java.util.Set;
     18import java.util.concurrent.atomic.AtomicBoolean;
    1819
    1920import javax.swing.AbstractAction;
     
    207208     * Fix selected errors
    208209     */
    209     @SuppressWarnings("unchecked")
    210210    private void fixErrors() {
    211211        TreePath[] selectionPaths = tree.getSelectionPaths();
     
    218218        for (TreePath path : selectionPaths) {
    219219            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
    220             if (node == null) {
    221                 continue;
    222             }
    223 
    224             Enumeration<TreeNode> children = node.breadthFirstEnumeration();
    225             while (children.hasMoreElements()) {
    226                 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    227                 if (processedNodes.contains(childNode)) {
    228                     continue;
    229                 }
    230 
    231                 processedNodes.add(childNode);
    232                 Object nodeInfo = childNode.getUserObject();
    233                 if (nodeInfo instanceof TestError) {
    234                     errorsToFix.add((TestError) nodeInfo);
    235                 }
     220            if (node != null) {
     221                ValidatorTreePanel.visitTestErrors(node, errorsToFix::add, processedNodes);
    236222            }
    237223        }
    238224
    239225        // run fix task asynchronously
    240         //
    241         FixTask fixTask = new FixTask(errorsToFix);
    242         MainApplication.worker.submit(fixTask);
     226        MainApplication.worker.submit(new FixTask(errorsToFix));
    243227    }
    244228
     
    246230     * Set selected errors to ignore state
    247231     */
    248     @SuppressWarnings("unchecked")
    249232    private void ignoreErrors() {
    250233        int asked = JOptionPane.DEFAULT_OPTION;
    251         boolean changed = false;
     234        AtomicBoolean changed = new AtomicBoolean();
    252235        TreePath[] selectionPaths = tree.getSelectionPaths();
    253236        if (selectionPaths == null)
     
    272255                }
    273256                if (asked == JOptionPane.YES_NO_OPTION) {
    274                     Enumeration<TreeNode> children = node.breadthFirstEnumeration();
    275                     while (children.hasMoreElements()) {
    276                         DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    277                         if (processedNodes.contains(childNode)) {
    278                             continue;
    279                         }
    280 
    281                         processedNodes.add(childNode);
    282                         Object nodeInfo = childNode.getUserObject();
    283                         if (nodeInfo instanceof TestError) {
    284                             TestError err = (TestError) nodeInfo;
    285                             err.setIgnored(true);
    286                             changed = true;
    287                             state.add(node.getDepth() == 1 ? err.getIgnoreSubGroup() : err.getIgnoreGroup());
    288                         }
    289                     }
     257                    ValidatorTreePanel.visitTestErrors(node, err -> {
     258                        err.setIgnored(true);
     259                        changed.set(true);
     260                        state.add(node.getDepth() == 1 ? err.getIgnoreSubGroup() : err.getIgnoreGroup());
     261                    }, processedNodes);
    290262                    for (String s : state) {
    291263                        OsmValidator.addIgnoredError(s);
     
    297269            }
    298270
    299             Enumeration<TreeNode> children = node.breadthFirstEnumeration();
    300             while (children.hasMoreElements()) {
    301                 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    302                 if (processedNodes.contains(childNode)) {
    303                     continue;
    304                 }
    305 
    306                 processedNodes.add(childNode);
    307                 Object nodeInfo = childNode.getUserObject();
    308                 if (nodeInfo instanceof TestError) {
    309                     TestError error = (TestError) nodeInfo;
    310                     String state = error.getIgnoreState();
    311                     if (state != null) {
    312                         OsmValidator.addIgnoredError(state);
    313                     }
    314                     changed = true;
    315                     error.setIgnored(true);
    316                 }
    317             }
    318         }
    319         if (changed) {
     271            ValidatorTreePanel.visitTestErrors(node, error -> {
     272                String state = error.getIgnoreState();
     273                if (state != null) {
     274                    OsmValidator.addIgnoredError(state);
     275                }
     276                changed.set(true);
     277                error.setIgnored(true);
     278            }, processedNodes);
     279        }
     280        if (changed.get()) {
    320281            tree.resetErrors();
    321282            OsmValidator.saveIgnoredErrors();
     
    329290    @SuppressWarnings("unchecked")
    330291    private void setSelectedItems() {
    331         if (tree == null)
     292        DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
     293        if (tree == null || ds == null)
    332294            return;
    333 
    334         Collection<OsmPrimitive> sel = new HashSet<>(40);
    335295
    336296        TreePath[] selectedPaths = tree.getSelectionPaths();
     
    338298            return;
    339299
     300        Collection<OsmPrimitive> sel = new HashSet<>(40);
    340301        for (TreePath path : selectedPaths) {
    341302            DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
     
    352313            }
    353314        }
    354         DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
    355         if (ds != null) {
    356             ds.setSelected(sel);
    357         }
     315        ds.setSelected(sel);
    358316    }
    359317
     
    368326     * @return whether the selected elements has any fix
    369327     */
    370     @SuppressWarnings("unchecked")
    371328    private boolean setSelection(Collection<OsmPrimitive> sel, boolean addSelected) {
    372         boolean hasFixes = false;
     329        AtomicBoolean hasFixes = new AtomicBoolean();
    373330
    374331        DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
    375332        if (lastSelectedNode != null && !lastSelectedNode.equals(node)) {
    376             Enumeration<TreeNode> children = lastSelectedNode.breadthFirstEnumeration();
    377             while (children.hasMoreElements()) {
    378                 DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    379                 Object nodeInfo = childNode.getUserObject();
    380                 if (nodeInfo instanceof TestError) {
    381                     TestError error = (TestError) nodeInfo;
    382                     error.setSelected(false);
    383                 }
    384             }
     333            ValidatorTreePanel.visitTestErrors(lastSelectedNode, error -> error.setSelected(false));
    385334        }
    386335
    387336        lastSelectedNode = node;
    388         if (node == null)
    389             return hasFixes;
    390 
    391         Enumeration<TreeNode> children = node.breadthFirstEnumeration();
    392         while (children.hasMoreElements()) {
    393             DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) children.nextElement();
    394             Object nodeInfo = childNode.getUserObject();
    395             if (nodeInfo instanceof TestError) {
    396                 TestError error = (TestError) nodeInfo;
     337        if (node != null) {
     338            ValidatorTreePanel.visitTestErrors(node, error -> {
    397339                error.setSelected(true);
    398340
    399                 hasFixes = hasFixes || error.isFixable();
     341                hasFixes.set(hasFixes.get() || error.isFixable());
    400342                if (addSelected) {
    401343                    error.getPrimitives().stream()
     
    403345                            .forEach(sel::add);
    404346                }
    405             }
    406         }
    407         selectButton.setEnabled(true);
    408         if (ignoreButton != null) {
    409             ignoreButton.setEnabled(true);
    410         }
    411 
    412         return hasFixes;
     347            });
     348            selectButton.setEnabled(true);
     349            if (ignoreButton != null) {
     350                ignoreButton.setEnabled(true);
     351            }
     352        }
     353
     354        return hasFixes.get();
    413355    }
    414356
Note: See TracChangeset for help on using the changeset viewer.