检查树中的节点是否已被删除

时间:2015-03-03 10:19:16

标签: java silktest

我正在使用以下代码从SapTree删除节点:

SapTree tree; // initialized somewhere
String key; // initialized somewhere
String itemname; // initialized somewhere
tree.selectNode(key);
tree.expandNode(key);
tree.ensureVisibleHorizontalItem(key, itemname);
tree.nodeContextMenu(key);
tree.selectContextMenuItem("DELETE_OBJECT");

但是,有时我无法删除某个项目,例如由于权限或其他依赖性。如何检查是否可以删除该项?

以上所有方法都会返回void,因此没有反馈意见。

我尝试了什么?

我查看了文档(SapTree [MicroFocus]),找到了一个可以获取密钥并返回内容的方法。我希望找到一个boolean exists(String key)或类似的方法。

1 个答案:

答案 0 :(得分:1)

如果节点不存在,几乎所有采用key参数的方法都会抛出RuntimeException。所以我结束了调用getNodeTop(),这在树上操作时不会产生任何副作用(与selectNode()和其他人相反)。通过捕获异常,我决定节点是否存在:

/**
 * Checks whether a node with the given key exists in the tree
 * @param haystack    Tree to find the key in
 * @param nodeKey     Node key to be found
 * @return True if the node was found (determined by getting the top location), false if the node was not found
 */
private boolean nodeExists(SapTree haystack, String nodeKey)
{
    try
    {
        haystack.getNodeTop(nodeKey);
        return true;
    } catch (RuntimeException rex)
    {
        return false;
    }
}

此答案在CC0下共同许可。