2016-11-10 33 views
0

我的問題很短。 「如何使用此方法中使用的抽象方法或示例?」如何在zk框架中使用抽象方法TreeModel?

這種方法是從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 

     } 
    }; 

我非常堅持這個。任何幫助將非常感激。 在此先感謝!

+0

https://www.zkoss.org/wiki/ZK_Developer's_Reference/MVC/Model/Tree_Model – flo

+0

你想做什麼?如果您只是想使用基本TreeModel調用DefaultTreeModel構造函數。 – TheBakker

+0

我想用Tree,ArrayList中的數據創建Hierarchy員工數據。我不知道該怎麼做。你能告訴我如何?或者類似的例子。感謝您回答^ _ ^ –

回答

0

好吧,據我所知你只是想使用一個通用的TreeModel而不需要重新定義特定的行爲。

因此,讓我們想象一下你的模型是員工豆子一樣的列表:

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組件。

希望這會有所幫助。