function main()
{
startApplication("my_aut_with_swt_tree");
tree = waitForObject({"isvisible": "true", "type": "org.eclipse.swt.widgets.Tree"});
item = getSWTTreeItem(tree, ["item 0", ["item 0 0", 2], "item 0 0 2"], true);
// Just to show it off, click on the tree item
// with coordinates relative to the tree:
itemBounds = item.getBounds();
mouseClick(tree, itemBounds.x + 5, itemBounds.y + 5, 0, Button.Button3);
snooze(3);
}
/**
* Get item in an SWT tree.
*
* path is a list of TreeItem texts, denoting the path of
* the desired item. For example:
*
* Node1
* Sub-Node1
* Node2
*
* path = ["Node1", "Sub-Node1"]
*
* In addition each element of path can also be an array
* of [TreeItem_text, occurrence], where occurrence
* specifies which TreeItem to use in case of duplicates
* under the same parent TreeItem. For example:
*
* Node0
* Sub-Node1
* Sub-Node1
*
* path = ["Node0", ["Sub-Node1", 2]]
*/
function getSWTTreeItem(treeOrItem, path, verbose)
{
if (verbose == null) {
verbose = false;
}
return getSWTTreeItemImpl(treeOrItem, path, path, verbose);
}
function getSWTTreeItemImpl(treeOrItem, path, subPath, verbose)
{
var n = treeOrItem.getItemCount();
var occurrence = null;
for (var i = 0; i < n; i++) {
item = treeOrItem.getItem(i);
itemText = item.getText();
if (occurrence == null) {
pathElement = subPath[0];
if (!(pathElement instanceof Array)) {
pathElement = [pathElement, 1];
}
occurrence = pathElement[1];
pathElement = pathElement[0];
}
if (itemText == pathElement) {
if (occurrence > 1) {
if (verbose) {
test.log("Ignoring: " + itemText + ", occurrence: " + occurrence);
}
occurrence--;
continue;
}
occurrence = null;
if (verbose) {
test.log("Found " + itemText);
}
if (subPath.length == 1) {
if (verbose) {
test.log("Reached end of path");
}
return item;
} else {
return getSWTTreeItemImpl(item, path, subPath.slice(1), verbose);
}
}
}
if (verbose) {
test.log("Error: Path element not found: " + subPath[0] + " (Complete path: " + path.toString() + ")");
}
return null;
}