2015-03-03 105 views
1

我刪除節點從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

如果節點不存在,幾乎所有采用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共同許可。

+0

我真的很希望你的'弦針'是獨一無二的。 – Nessuno 2015-03-03 10:25:08

+0

@Nessuno:節點有一個鍵,文本和一個工具提示。據我觀察,關鍵是獨一無二的(雖然我在文檔中找不到)。我已經更新了使用nodeKey而不是needle的答案,因此很明顯,不要將它與任何任意文本一起使用。 – 2015-03-03 10:29:50