如何在zk框架中使用抽象方法TreeModel?

时间:2016-11-10 04:57:44

标签: java zk

我的问题很短。"如何使用此方法中使用的抽象方法或示例?"

此方法来自org.zkoss.zul.TreeModel

  tmtAtasan = new TreeModel<Map<String,Object>>() {

        @Override
        public void addTreeDataListener(TreeDataListener arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public Map<String, Object> getChild(int[] arg0) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public Map<String, Object> getChild(Map<String, Object> arg0,
                int arg1) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public int getChildCount(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            return 0;
        }
        @Override
        public int getIndexOfChild(Map<String, Object> arg0,
                Map<String, Object> arg1) {
            // TODO Auto-generated method stub
            return 0;
        }
        @Override
        public int[] getPath(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public Map<String, Object> getRoot() {
            // TODO Auto-generated method stub
            return null;
        }
        @Override
        public boolean isLeaf(Map<String, Object> arg0) {
            // TODO Auto-generated method stub
            return false;
        }
        @Override
        public void removeTreeDataListener(TreeDataListener arg0) {
            // TODO Auto-generated method stub

        }
    };

我非常坚持这一点。任何帮助都会非常值得欣赏。 提前谢谢!

1 个答案:

答案 0 :(得分:0)

好的,根据我的理解,您只想使用通用的TreeModel而无需重新定义特定的行为。

因此,让我们假设您的模型是员工bean的列表,如:

public class Employee {
    private String name;
    private List<Employee> listSubordinates = new ArrayList<Employee>();

    public Employee(String pName) {
        name = pName;
    }

    public void setName(String pName) {
        this.name = pName;
    }
    public String getName() {
        return name;
    }

    public List<Employee> getListSubordinates() {
        return listSubordinates;
    }
    public void setListSubordinates(List<Employee> pListSubordinates) {
        this.listSubordinates = pListSubordinates;
    }
}

对于此示例,我们假设您已经检索了按层次结构排序的员工列表(以简化示例)。

Employee boss1 = new Employee("Boss1");
Employee sub1 = new Employee("Sub1");
boss1.getListSubordinates().add(sub1);
Employee sub2 = new Employee("Sub2");
boss1.getListSubordinates().add(sub2);

Employee boss2 = new Employee("Boss2");
Employee sub3 = new Employee("Sub3");
boss2.getListSubordinates().add(sub3);

List<Employee> listBosses = Arrays.asList(boss1, boss2);

同样,这是一个只有一级层次结构的简单示例,如果您有一个可变级别的层次结构,则以下代码必须是递归的。

// Build the list of the nodes sorted by hierarchy
List<DefaultTreeNode<Employee>> firstLevelNodes = new ArrayList<DefaultTreeNode<Employee>>();
// For each employee of the highest level
for (Employee boss : listBosses) {
    // Build the list of its sub employee
    List<DefaultTreeNode<Employee>> listSubordinates = new ArrayList<DefaultTreeNode<Employee>>();
    for (Employee subordinate : boss.getListSubordinates()) {
        listSubordinates.add(new DefaultTreeNode<Employee>(subordinate));
    }
    // Then build the boss node with its data and its children nodes
    DefaultTreeNode<Employee> bossNode = new DefaultTreeNode<Employee>(boss, listSubordinates);
    // And add it to the list of first level nodes
    firstLevelNodes.add(bossNode);
}

// Build the ROOT, a 'technical' node containing the nodes of the tree.
DefaultTreeNode<Employee> root = new DefaultTreeNode<Employee>(null, firstLevelNodes);
// Create the TreeModel
TreeModel treeModel = new DefaultTreeModel<Employee>(root);

现在你只需要将TreeModel设置为Tree组件。

希望这有帮助。