org.zkoss.zul
Class DefaultTreeModel<E>

java.lang.Object
  extended by org.zkoss.zul.AbstractTreeModel<TreeNode<E>>
      extended by org.zkoss.zul.DefaultTreeModel<E>
All Implemented Interfaces:
Serializable, Openable<TreeNode<E>>, Selectable<TreeNode<E>>, TreeModel<TreeNode<E>>, TreeModelExt<TreeNode<E>>

public class DefaultTreeModel<E>
extends AbstractTreeModel<TreeNode<E>>
implements TreeModelExt<TreeNode<E>>, Serializable

A simple tree data model that uses TreeNode to represent a tree. Thus the whole tree of data must be loaded into memory, and each node must be represented by TreeNode.

If you want to implement a huge tree that only a visible part shall be loaded, it is better to implement it by extending from AbstractTreeModel.

DefaultTreeModel depends on TreeNode only. It does not depend on DefaultTreeNode. However, DefaultTreeNode depends on DefaultTreeModel.

For introduction, please refer to ZK Developer's Reference: Tree Model.

Since:
5.0.6
Author:
tomyeh
See Also:
Serialized Form

Constructor Summary
DefaultTreeModel(TreeNode<E> root)
          Creates a tree with the specified note as the root.
 
Method Summary
 TreeNode<E> getChild(TreeNode<E> parent, int index)
          Returns the child of parent at index index in the parent's child array.
 int getChildCount(TreeNode<E> parent)
          Returns the number of children of parent.
 int getIndexOfChild(TreeNode<E> parent, TreeNode<E> child)
          Returns the index of child in parent.
 boolean isLeaf(TreeNode<E> node)
          Returns true if node is a leaf.
 void sort(Comparator<TreeNode<E>> cmpr, boolean ascending)
          Sorts the data.
 
Methods inherited from class org.zkoss.zul.AbstractTreeModel
addSelection, addTreeDataListener, clearOpen, clearSelection, fireEvent, getPath, getRoot, getSelection, isOpen, removeAllSelection, removeSelection, removeTreeDataListener, retainAllSelection, setOpen
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultTreeModel

public DefaultTreeModel(TreeNode<E> root)
Creates a tree with the specified note as the root.

Parameters:
root - the root (cannot be null).
Method Detail

isLeaf

public boolean isLeaf(TreeNode<E> node)
Description copied from interface: TreeModel
Returns true if node is a leaf. Notice that not all non-leaf nodes have children. In file-system terminology, a leaf node is a file, while a non-leaf node is a folder.

Specified by:
isLeaf in interface TreeModel<TreeNode<E>>
Parameters:
node - a node in the tree, obtained from this data source
Returns:
true if node is a leaf.

getChild

public TreeNode<E> getChild(TreeNode<E> parent,
                            int index)
Description copied from interface: TreeModel
Returns the child of parent at index index in the parent's child array.

Specified by:
getChild in interface TreeModel<TreeNode<E>>
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the child of parent at index index

getChildCount

public int getChildCount(TreeNode<E> parent)
Description copied from interface: TreeModel
Returns the number of children of parent.

Specified by:
getChildCount in interface TreeModel<TreeNode<E>>
Parameters:
parent - a node in the tree, obtained from this data source
Returns:
the number of children of the node parent

getIndexOfChild

public int getIndexOfChild(TreeNode<E> parent,
                           TreeNode<E> child)
Description copied from class: AbstractTreeModel
Returns the index of child in parent. If either parent or child is null, returns -1. If either parent or child don't belong to this tree model, returns -1.

The default implementation iterates through all children of parent by invoking, and check if child is part of them. You could override it if you have a better algorithm. TreeModel.getChild(E, int)

Specified by:
getIndexOfChild in interface TreeModel<TreeNode<E>>
Overrides:
getIndexOfChild in class AbstractTreeModel<TreeNode<E>>
Parameters:
parent - a node in the tree, obtained from this data source
child - the node we are interested in
Returns:
the index of the child in the parent, or -1 if either child or parent are null or don't belong to this tree model

sort

public void sort(Comparator<TreeNode<E>> cmpr,
                 boolean ascending)
Sorts the data.

Specified by:
sort in interface TreeModelExt<TreeNode<E>>
Parameters:
cmpr - the comparator.
ascending - whether to sort in the ascending order. It is ignored since this implementation uses cmprt to compare.


Copyright © 2011. All Rights Reserved.