Search code examples
javasilktest

Check whether node in tree has been deleted


I am deleting nodes from a SapTree with the following code:

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");

However, sometimes I cannot delete an item, e.g. due to permissions or other dependencies. How do I check whether it was possible to delete the item?

All of the above methods return void, so there is no feedback that way.

What have I tried?

I looked up the documentation (SapTree [MicroFocus]) for a method that would take a key and return something. I expected to find a boolean exists(String key) or similar method.


Solution

  • Almost any method that takes a key parameter will throw a RuntimeException if the node does not exist. So I ended calling getNodeTop(), which does not cause any side effects when operating on the tree (in contrast to selectNode() and others). By catching the exception I decide whether the node exists or not:

    /**
     * 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;
        }
    }
    

    This answer is co-licenced under CC0.