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

Check whether node in tree has been deleted

我正在使用以下代码从 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");

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

以上所有方法returnvoid,所以没有反馈那种方式。

我尝试了什么?

我在文档 (SapTree [MicroFocus]) 中查找了一种需要密钥和 return 东西的方法。我希望找到 boolean exists(String key) 或类似的方法。

如果节点不存在,几乎任何采用 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 共同授权的。