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:
java.io.Serializable, Openable<TreeNode<E>>, Selectable<TreeNode<E>>, Sortable<TreeNode<E>>, TreeOpenableModel, TreeSelectableModel, TreeModel<TreeNode<E>>

public class DefaultTreeModel<E>
extends AbstractTreeModel<TreeNode<E>>
implements Sortable<TreeNode<E>>, java.io.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, jumperchen
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class org.zkoss.zul.AbstractTreeModel
AbstractTreeModel.Path
 
Field Summary
 
Fields inherited from class org.zkoss.zul.AbstractTreeModel
_opens, _selection
 
Constructor Summary
DefaultTreeModel(TreeNode<E> root)
          Creates a tree with the specified note as the root.
DefaultTreeModel(TreeNode<E> root, boolean emptyChildAsLeaf)
          Creates a tree with the specified note as the root.
 
Method Summary
 void addSelection(java.lang.Object obj)
          Deprecated. As of release 6.0.0, replaced with AbstractTreeModel.addToSelection(E).
 java.lang.Object clone()
           
 TreeNode<E> getChild(TreeNode<E> parent, int index)
          Returns the child of the given parent at the given index where the index indicates in the parent's child array.
 int getChildCount(TreeNode<E> parent)
          Returns the number of children of the given parent.
 int getIndexOfChild(TreeNode<E> parent, TreeNode<E> child)
          Returns the index of child in parent.
 int[] getPath(TreeNode<E> child)
          Returns the path from the child, where the path indicates the child is placed in the whole tree.
 java.lang.String getSortDirection(java.util.Comparator<TreeNode<E>> cmpr)
          Returns the sort direction of this model for the given comparator.
 boolean isLeaf(TreeNode<E> node)
          Returns true if node is a leaf.
 boolean isObjectOpened(java.lang.Object child)
          Returns whether the specified object be opened.
 boolean isOpen(java.lang.Object child)
          Deprecated. As of release 6.0.0, replaced with isObjectOpened(java.lang.Object).
 boolean isSelected(java.lang.Object child)
          Returns whether an object is selected.
 boolean removeFromSelection(java.lang.Object child)
          Remove the specified object from selection.
 boolean removeOpenObject(java.lang.Object child)
          Remove the specified object from selection.
 void removeSelection(java.lang.Object obj)
          Deprecated. As of release 6.0.0, replaced with removeFromSelection(java.lang.Object).
 void setOpen(TreeNode<E> child, boolean open)
          Deprecated. As of release 6.0.0, replaced with AbstractTreeModel.addOpenObject(E) and removeOpenObject(java.lang.Object).
 void sort(java.util.Comparator<TreeNode<E>> cmpr, boolean ascending)
          Sorts the data.
 
Methods inherited from class org.zkoss.zul.AbstractTreeModel
addOpenObject, addOpenPath, addOpenPaths, addSelectionPath, addSelectionPaths, addToSelection, addTreeDataListener, afterSort, beforeSort, clearOpen, clearSelection, fireEvent, fireEvent, fireOpenChanged, fireSelectionChanged, getChild, getOpenCount, getOpenObjects, getOpenPath, getOpenPaths, getRoot, getSelection, getSelectionCount, getSelectionPath, getSelectionPaths, isMultiple, isOpenEmpty, isPathOpened, isPathSelected, isSelectionEmpty, removeOpenPath, removeOpenPaths, removeSelectionPath, removeSelectionPaths, removeTreeDataListener, setMultiple, setOpenObjects, setSelection
 
Methods inherited from class java.lang.Object
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).

DefaultTreeModel

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

Parameters:
root - the root (cannot be null).
emptyChildAsLeaf - whether to treat the zero size of children node as a leaf node.
Since:
6.0.3
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 the given parent at the given index where the index indicates 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

getChildCount

public int getChildCount(TreeNode<E> parent)
Description copied from interface: TreeModel
Returns the number of children of the given 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. AbstractTreeModel.getChild(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

getPath

public int[] getPath(TreeNode<E> child)
Returns the path from the child, where the path indicates the child is placed in the whole tree.

Specified by:
getPath in interface TreeModel<TreeNode<E>>
Overrides:
getPath in class AbstractTreeModel<TreeNode<E>>
Parameters:
child - the node we are interested in
Since:
6.0.0

isSelected

public boolean isSelected(java.lang.Object child)
Description copied from interface: Selectable
Returns whether an object is selected.

Specified by:
isSelected in interface Selectable<TreeNode<E>>
Overrides:
isSelected in class AbstractTreeModel<TreeNode<E>>

removeFromSelection

public boolean removeFromSelection(java.lang.Object child)
Description copied from interface: Selectable
Remove the specified object from selection.

If this represents a change to the current selection then notify each ListDataListener, including UI.

Specified by:
removeFromSelection in interface Selectable<TreeNode<E>>
Overrides:
removeFromSelection in class AbstractTreeModel<TreeNode<E>>
Parameters:
child - the object to be remove from selection.
Returns:
whether it is removed successfully

isObjectOpened

public boolean isObjectOpened(java.lang.Object child)
Description copied from interface: Openable
Returns whether the specified object be opened.

Specified by:
isObjectOpened in interface Openable<TreeNode<E>>
Overrides:
isObjectOpened in class AbstractTreeModel<TreeNode<E>>

removeOpenObject

public boolean removeOpenObject(java.lang.Object child)
Description copied from interface: Openable
Remove the specified object from selection.

Specified by:
removeOpenObject in interface Openable<TreeNode<E>>
Overrides:
removeOpenObject in class AbstractTreeModel<TreeNode<E>>
Parameters:
child - the object to be remove from selection.
Returns:
whether it is removed successfully

sort

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

Notice: it invokes AbstractTreeModel.beforeSort() and AbstractTreeModel.afterSort(java.lang.Object) to save and restore the selection and open states. If you prefer not to preserve objects and prefer to save the paths, you can override AbstractTreeModel.beforeSort() to do nothing but returning null. If you prefer to clear the selection, you can override AbstractTreeModel.beforeSort() to clear AbstractTreeModel._selection and return null.

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

addSelection

public void addSelection(java.lang.Object obj)
Deprecated. As of release 6.0.0, replaced with AbstractTreeModel.addToSelection(E).


removeSelection

public void removeSelection(java.lang.Object obj)
Deprecated. As of release 6.0.0, replaced with removeFromSelection(java.lang.Object).


setOpen

public void setOpen(TreeNode<E> child,
                    boolean open)
Deprecated. As of release 6.0.0, replaced with AbstractTreeModel.addOpenObject(E) and removeOpenObject(java.lang.Object).


isOpen

public boolean isOpen(java.lang.Object child)
Deprecated. As of release 6.0.0, replaced with isObjectOpened(java.lang.Object).


getSortDirection

public java.lang.String getSortDirection(java.util.Comparator<TreeNode<E>> cmpr)
Description copied from interface: Sortable
Returns the sort direction of this model for the given comparator. It must be one of "ascending", "descending" and "natural".

Default: "natural".

Specified by:
getSortDirection in interface Sortable<TreeNode<E>>

clone

public java.lang.Object clone()
Overrides:
clone in class AbstractTreeModel<TreeNode<E>>


Copyright © 2005-2011 Potix Corporation. All Rights Reserved. SourceForge.net Logo