-1

我正在通过扩展 DefaultTreeModel 来研究 zk 树。它在网页上正确呈现。但是当我尝试使用 root 获取与这棵树关联的模型时,我得到了空值。

我正在使用两种方法来做到这一点。1.使用Tree hierarchyTree;hierarchyTree.getModel().getRoot(); // 返回 null 2. 使用 AdvancedTreeModel contactTreeModel 扩展 DefaultTreeModel; ContactTreeNode nodes2 = (ContactTreeNode) contactTreeModel.getRoot(); //也返回null

你能否请告诉我如何获取根节点或与之关联的模型,它不为空。这样我就可以迭代它。

AdvanceTeeModel.java

package demo.tree.dynamic_tree;

import org.zkoss.zul.DefaultTreeModel;
import org.zkoss.zul.DefaultTreeNode;

import demo.data.pojo.Contact;

public class AdvancedTreeModel extends DefaultTreeModel<Contact> {
    private static final long serialVersionUID = -5513180500300189445L;

    DefaultTreeNode<Contact> _root;

    public AdvancedTreeModel(ContactTreeNode contactTreeNode) {
        super(contactTreeNode);
        _root = contactTreeNode;
    }

    /**
     * remove the nodes which parent is <code>parent</code> with indexes
     * <code>indexes</code>
     * 
     * @param parent
     *            The parent of nodes are removed
     * @param indexFrom
     *            the lower index of the change range
     * @param indexTo
     *            the upper index of the change range
     * @throws IndexOutOfBoundsException
     *             - indexFrom < 0 or indexTo > number of parent's children
     */
    public void remove(DefaultTreeNode<Contact> parent, int indexFrom, int indexTo) throws IndexOutOfBoundsException {
        DefaultTreeNode<Contact> stn = parent;
        for (int i = indexTo; i >= indexFrom; i--)
            try {
                stn.getChildren().remove(i);
            } catch (Exception exp) {
                exp.printStackTrace();
            }
    }

    public void remove(DefaultTreeNode<Contact> target) throws IndexOutOfBoundsException {
        int index = 0;
        DefaultTreeNode<Contact> parent = null;
        // find the parent and index of target
        parent = dfSearchParent(_root, target);
        if(parent!=null){
        for (index = 0; index < parent.getChildCount(); index++) {
            if (parent.getChildAt(index).equals(target)) {
                break;
            }
        }
            remove(parent, index, index);
        }
    }

    /**
     * insert new nodes which parent is <code>parent</code> with indexes
     * <code>indexes</code> by new nodes <code>newNodes</code>
     * 
     * @param parent
     *            The parent of nodes are inserted
     * @param indexFrom
     *            the lower index of the change range
     * @param indexTo
     *            the upper index of the change range
     * @param newNodes
     *            New nodes which are inserted
     * @throws IndexOutOfBoundsException
     *             - indexFrom < 0 or indexTo > number of parent's children
     */
    public void insert(DefaultTreeNode<Contact> parent, int indexFrom, int indexTo, DefaultTreeNode<Contact>[] newNodes)
            throws IndexOutOfBoundsException {
        DefaultTreeNode<Contact> stn = parent;
        for (int i = indexFrom; i <= indexTo; i++) {
            try {
                stn.getChildren().add(i, newNodes[i - indexFrom]);
            } catch (Exception exp) {
                throw new IndexOutOfBoundsException("Out of bound: " + i + " while size=" + stn.getChildren().size());
            }
        }
    }

    /**
     * append new nodes which parent is <code>parent</code> by new nodes
     * <code>newNodes</code>
     * 
     * @param parent
     *            The parent of nodes are appended
     * @param newNodes
     *            New nodes which are appended
     */
    public void add(DefaultTreeNode<Contact> parent, DefaultTreeNode<Contact>[] newNodes) {
        DefaultTreeNode<Contact> stn = (DefaultTreeNode<Contact>) parent;

        for (int i = 0; i < newNodes.length; i++)
            stn.getChildren().add(newNodes[i]);

    }

    private DefaultTreeNode<Contact> dfSearchParent(DefaultTreeNode<Contact> node, DefaultTreeNode<Contact> target) {
        if (node.getChildren() != null && node.getChildren().contains(target)) {
            return node;
        } else {
            int size = getChildCount(node);
            for (int i = 0; i < size; i++) {
                DefaultTreeNode<Contact> parent = dfSearchParent((DefaultTreeNode<Contact>) getChild(node, i), target);
                if (parent != null) {
                    return parent;
                }
            }
        }
        return null;
    }

}

实际上我正在使用这个例子,[http://www.zkoss.org/zkdemo/tree/dynamic_tree][来自ZK的动态树]

在 Composer 的覆盖渲染方法中,我使用以下代码行从树中获取根/模型。

ContactTreeNode nodes = (ContactTreeNode) hierarchyTree.getModel().getRoot();
            System.out.println("nodes: " + nodes);

            ContactTreeNode nodes2 = (ContactTreeNode) contactTreeModel.getRoot();
4

1 回答 1

1

您的删除方法必须关心删除根。
意思是,如果你删除根,你必须设置一个新的。
试试看是否能解决问题。或者尝试是否
在树初始化(并且没有编辑)之后立即出现问题。

于 2013-06-27T06:59:12.653 回答