// Path: src/main/java/org/robotframework/formslibrary/operator/TreeOperator.java
// public class TreeOperator extends AbstractComponentOperator {
// 
//     /**
//      * Initialize a TreeOperator with the navigation tree found in the current
//      * context.
//      */
//     public TreeOperator() {
//         super(new ByComponentTypeChooser(0, ComponentType.TREE));
//     }
// 
//     /**
//      * Select entries matching the given path
//      * 
//      * @param path
//      *            e.g. level1 > level2 > level3
//      */
//     public void select(String path) {
// 
//         Object dTreeRootItem = ObjectUtil.invokeMethod(getSource(), "getRoot()");
//         Object treeItem = findTreeItem(dTreeRootItem, path);
//         if (treeItem == null) {
//             throw new FormsLibraryException("Could not find tree path " + path);
//         }
// 
//         Object tree = ObjectUtil.invokeMethod(dTreeRootItem, "getTree()");
//         Object selection = ObjectUtil.invokeMethod(tree, "getSelection()");
//         ObjectUtil.invokeMethodWithTypedArg(selection, "selectItem()", ComponentType.TREE_ITEM.toString(), treeItem);
// 
//     }
// 
//     private Object findTreeItem(Object parentItem, String path) {
// 
//         boolean isLastLevel = path.indexOf(Constants.LEVEL_SEPARATOR) == -1;
//         String labelToSelect = TextUtil.getFirstSegment(path, Constants.LEVEL_SEPARATOR);
// 
//         int itemCount = (Integer) ObjectUtil.invokeMethod(parentItem, "getItemCount()");
//         for (int i = 0; i < itemCount; i++) {
// 
//             Object treeItem = ObjectUtil.invokeMethodWithIntArg(parentItem, "getItem()", i);
//             String itemLabel = ObjectUtil.getString(treeItem, "getLabel()");
//             boolean isExpanded = ObjectUtil.getBoolean(treeItem, "isExpanded()");
//             Logger.debug("Found tree item '" + itemLabel + "' (expanded=" + isExpanded + ")");
// 
//             if (!TextUtil.matches(itemLabel, labelToSelect)) {
//                 continue;
//             }
// 
//             if (isLastLevel) {
//                 return treeItem;
//             } else {
//                 if (!isExpanded) {
//                     ObjectUtil.invokeMethodWithBoolArg(treeItem, "setExpanded()", true);
//                     isExpanded = ObjectUtil.getBoolean(treeItem, "isExpanded()");
//                     Logger.debug("Tree item '" + itemLabel + "' (expanded=" + isExpanded + ")");
//                     getSource().invalidate();
//                 }
//                 return findTreeItem(treeItem, TextUtil.getNextSegments(path, Constants.LEVEL_SEPARATOR));
//             }
//         }
// 
//         return null;
//     }
// 
// }


import org.robotframework.formslibrary.operator.TreeOperator;
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

package org.robotframework.formslibrary.keyword;


@RobotKeywords
public class TreeKeywords {

    @RobotKeyword("Select a tree entry by its label. Use '>' between different levels.\n\n Example:\n"
            + "| Select Tree | _root > branch1 > branch1.1_ |\n")
    @ArgumentNames({ "treePath" })
    public void selectTree(String treePath) {
        // locate the DTree by index walk the menu in the select

// Path: src/main/java/org/robotframework/formslibrary/operator/TreeOperator.java
// public class TreeOperator extends AbstractComponentOperator {
// 
//     /**
//      * Initialize a TreeOperator with the navigation tree found in the current
//      * context.
//      */
//     public TreeOperator() {
//         super(new ByComponentTypeChooser(0, ComponentType.TREE));
//     }
// 
//     /**
//      * Select entries matching the given path
//      * 
//      * @param path
//      *            e.g. level1 > level2 > level3
//      */
//     public void select(String path) {
// 
//         Object dTreeRootItem = ObjectUtil.invokeMethod(getSource(), "getRoot()");
//         Object treeItem = findTreeItem(dTreeRootItem, path);
//         if (treeItem == null) {
//             throw new FormsLibraryException("Could not find tree path " + path);
//         }
// 
//         Object tree = ObjectUtil.invokeMethod(dTreeRootItem, "getTree()");
//         Object selection = ObjectUtil.invokeMethod(tree, "getSelection()");
//         ObjectUtil.invokeMethodWithTypedArg(selection, "selectItem()", ComponentType.TREE_ITEM.toString(), treeItem);
// 
//     }
// 
//     private Object findTreeItem(Object parentItem, String path) {
// 
//         boolean isLastLevel = path.indexOf(Constants.LEVEL_SEPARATOR) == -1;
//         String labelToSelect = TextUtil.getFirstSegment(path, Constants.LEVEL_SEPARATOR);
// 
//         int itemCount = (Integer) ObjectUtil.invokeMethod(parentItem, "getItemCount()");
//         for (int i = 0; i < itemCount; i++) {
// 
//             Object treeItem = ObjectUtil.invokeMethodWithIntArg(parentItem, "getItem()", i);
//             String itemLabel = ObjectUtil.getString(treeItem, "getLabel()");
//             boolean isExpanded = ObjectUtil.getBoolean(treeItem, "isExpanded()");
//             Logger.debug("Found tree item '" + itemLabel + "' (expanded=" + isExpanded + ")");
// 
//             if (!TextUtil.matches(itemLabel, labelToSelect)) {
//                 continue;
//             }
// 
//             if (isLastLevel) {
//                 return treeItem;
//             } else {
//                 if (!isExpanded) {
//                     ObjectUtil.invokeMethodWithBoolArg(treeItem, "setExpanded()", true);
//                     isExpanded = ObjectUtil.getBoolean(treeItem, "isExpanded()");
//                     Logger.debug("Tree item '" + itemLabel + "' (expanded=" + isExpanded + ")");
//                     getSource().invalidate();
//                 }
//                 return findTreeItem(treeItem, TextUtil.getNextSegments(path, Constants.LEVEL_SEPARATOR));
//             }
//         }
// 
//         return null;
//     }
// 
// }


// Path: src/main/java/org/robotframework/formslibrary/keyword/TreeKeywords.java
import org.robotframework.formslibrary.operator.TreeOperator;
import org.robotframework.javalib.annotation.ArgumentNames;
import org.robotframework.javalib.annotation.RobotKeyword;
import org.robotframework.javalib.annotation.RobotKeywords;

package org.robotframework.formslibrary.keyword;


@RobotKeywords
public class TreeKeywords {

    @RobotKeyword("Select a tree entry by its label. Use '>' between different levels.\n\n Example:\n"
            + "| Select Tree | _root > branch1 > branch1.1_ |\n")
    @ArgumentNames({ "treePath" })
    public void selectTree(String treePath) {
        // locate the DTree by index walk the menu in the select

        new TreeOperator().select(treePath);
