如何在ZK中循环树?

时间:2016-11-28 08:28:41

标签: zk

我尝试从this示例中获取ZK树的值。但我没有得到所有孩子的价值......我尝试过:

    List<Treechildren> treeChildren = new ArrayList<>();
    List<Label> labelList = new ArrayList<>();
    List<Label> childs = new ArrayList<>();

    tree.getChildren().forEach(component -> {
        if(component instanceof Treechildren){
            treeChildren.add((Treechildren) component);
        }
    });
    List<Treeitem> treeItems = new ArrayList<>();
    treeChildren.forEach(treechildren ->{
        treeItems.addAll(treechildren.getChildren());           
    });
    List<Treechildren> children = new ArrayList<>();

    treeItems.forEach(treeitem -> {
        System.err.println("treeitem: " + treeitem);
        if (treeitem.getChildren().size() > 1) {    
            treeitem.getChildren().forEach(child -> {
                if(child instanceof Treechildren){
                    children.add((Treechildren) child);
                } else {
                    List<Treecell> tcList = new ArrayList<>();
                    child.getChildren().forEach(component -> { 
                        if(component instanceof Treecell) {
                            tcList.add((Treecell) component);
                        }
                    });
                    List<Hlayout> hlList = new ArrayList<>();
                    tcList.forEach(treecell ->{
                        System.err.println("treecell" + treecell);
                        hlList.addAll(treecell.getChildren());
                    });

                    hlList.forEach(hlayout -> {
                        childs.addAll(hlayout.getChildren());
                    });

                }
            });
        }else {

            List<Treerow> tr = new ArrayList<>();
            treeitem.getChildren().forEach(component -> {
                if (component instanceof Treerow) {
                    tr.add((Treerow) component);
                } else {
                }

            });
            List<Treecell> TC = new ArrayList<>();
            tr.forEach(treerow -> {
                TC.addAll(treerow.getChildren());
            });

            List<Hlayout> HL = new ArrayList<>();
            TC.forEach(treecell -> {
                HL.addAll(treecell.getChildren());
            });

            HL.forEach(hlayout -> labelList.addAll(hlayout.getChildren()));
        }
    });

    labelList.forEach(label -> System.err.println("label: " + label.getValue()));
    childs.forEach(label -> System.err.println("childs: " + label.getValue()));
}

但是我没有得到整个树标签....目的是当我按下提交按钮时将树保存到数据库。

1 个答案:

答案 0 :(得分:1)

由于你没有发布你的树,我只能猜测它的样子。但我试图至少理解你的代码并发现了一个潜在的错误:在第21行处理树节点有子节点的情况。您将Treechildren添加到children,但从不对它做任何事情。所以你的循环将停在第一级。

首先,我建议对树及其组件使用特殊方法:

  • 树:getTreechildren()
  • Treeitem:getTree(),getTreerow()和getTreechildren()
  • Treerow:getTree()和getLinkedTreechildren()

这些可以帮助您摆脱所有的转换和instanceof检查。此外,您可以在调用getChildren()时指定孩子的类型:

List<Treecell> cells = row.<Treecell> getChildren()

现在你不必演员,但要小心所有的孩子都属于同一类型。

最后,我重构了你的代码以理解它:

// Find all items in the tree, level by level
List<Treeitem> items = new ArrayList<>(tree.getTreechildren().getChildren());
List<Treeitem> itemsTemp = new ArrayList<>(tree.getTreechildren().getChildren());
while (!itemsTemp.isEmpty())
{
    Treeitem nextChild = itemsTemp.remove(0);
    if (nextChild.getTreechildren() != null)
    {
        items.addAll(nextChild.getTreechildren().getChildren());
        itemsTemp.addAll(nextChild.getTreechildren().getChildren());
    }
}

// convert item -> row -> cells -> hlayouts -> children
List<Component> children = items.stream()
                                .map(Treeitem::getTreerow)
                                .filter(Objects::nonNull)
                                .flatMap(row -> row.<Treecell> getChildren().stream())
                                .flatMap(cell -> cell.<Hlayout> getChildren().stream())
                                .flatMap(hlayout -> hlayout.getChildren().stream())
                                .collect(Collectors.toList());

    // // without streams
    // List<Component> children = new ArrayList<>();
    // for (Treeitem item : items)
    // {
    //     if (item.getTreerow() != null)
    //     {
    //         for (Treecell cell : item.getTreerow().<Treecell> getChildren())
    //         {
    //             for (Hlayout hlayout : cell.<Hlayout> getChildren())
    //             {
    //                 children.addAll(hlayout.getChildren());
    //             }
    //         }
    //     }
    // }

你可以看到,在while循环中,我看了第一层的树的孩子,然后看着他们的孩子,那些孩子的孩子,等等。然后我使用上面提到的方法来查找每个树节点的组件。

同样,我不知道你的树是怎么样的,所以我不知道这是否适合你。也许你可以张贴一个小例子。