Class AbstractTreeModel<E>
- All Implemented Interfaces:
Serializable,Openable<E>,Pageable,Selectable<E>,TreeOpenableModel,TreeSelectableModel,PageableModel,TreeModel<E>
- Direct Known Subclasses:
DefaultTreeModel
TreeModel.
AbstractTreeModel implements both TreeSelectableModel
and TreeOpenableModel. In other words, it stores the selection
and open states, such that Tree and other UI can interact with.
In additions, AbstractTreeModel also implements
Selectable and Openable to simplify the access
(and provides backward compatibility to ZK 5 and earlier).
However, these two interfaces are optional and designed for application.
Tree and all ZK core don't access it at all.
For introduction, please refer to ZK Developer's Reference: Tree Model.
- Since:
- 3.0.0
- Author:
- jumperchen, tomyeh
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA default selection control implementation forAbstractTreeModel, by default it assumes all elements are selectable.protected static classRepresents a tree path. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected Set<AbstractTreeModel.Path>The open information.protected Set<AbstractTreeModel.Path>The selection.Fields inherited from interface org.zkoss.zul.PageableModel
INTERNAL_EVENT -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanaddOpenObject(E child) Add the specified object into the collection of opened objects.booleanaddOpenPath(int[] path) Adds path to the current open.booleanaddOpenPaths(int[][] paths) Adds paths to the current Open.voidaddPagingEventListener(PagingListener listener) Adds a listener to the list of listeners to be notified when a PagingEvent happens outside of standard Paging componentbooleanaddSelectionPath(int[] path) Adds path to the current selection.booleanaddSelectionPaths(int[][] paths) Adds paths to the current selection.booleanaddToSelection(E child) Add the specified object into selection.voidAdd a listener to the tree that's notified each time a change to the data model occursprotected voidA utility that the deriving class can call to restore the states saved bybeforeSort()protected ObjectA utility that the deriving class can call to save the states before sorting the model.voidEmpties the current open path.voidEmpties the current selection.clone()voidfireEvent(int evtType, int[] path, int indexFrom, int indexTo) Fires aTreeDataEventfor all registered listenervoidfireEvent(int evtType, int[] path, int indexFrom, int indexTo, int[] affectedPath) Has the same functionality withfireEvent(int, int[], int, int), while this is used for node removal onlyvoidfireEvent(int evtType, int[] path, int indexFrom, int indexTo, int[][] affectedPaths) Has the same functionality withfireEvent(int, int[], int, int), while this is used for node removal onlyprotected voidfireOpenChanged(int[] path) Fires aTreeDataEventfor all registered listener when open status has changed.protected voidfireSelectionChanged(int[] path) Fires aTreeDataEventfor all registered listener when selection status has changed.intReturns the active page (starting from 0).getChild(int[] path) Returns the child at the given path where the path indicates the child is placed in the whole tree.intgetIndexOfChild(E parent, E child) Returns the index of child in parent.intReturns the number of paths that are opened.Returns the objects that are opened.int[]Returns the first path in the open.int[][]Returns the paths in the open.intReturns the number of pages.intReturns the number of items per page.int[]Returns the path from the specified child.getRoot()Return the root of the tree model.Returns the current selection.Returns the selection control for the selection model, if any.intReturns the number of paths that are selected.int[]Returns the first path in the selection.int[][]Returns the paths in the selection.intReturns the total number of items.Returns all listeners.booleanReturns whether the current selection mode is multiple.booleanisObjectOpened(Object child) Returns whether the specified object be opened.booleanReturns true if the open is currently empty.booleanisPathOpened(int[] path) Returns true if the path,path, is in the current open.booleanisPathSelected(int[] path) Returns true if the path,path, is in the current selection.booleanisSelected(Object child) Returns whether an object is selected.booleanReturns true if the selection is currently empty.booleanremoveFromSelection(Object child) Remove the specified object from selection.booleanremoveOpenObject(Object child) Remove the specified object from selection.booleanremoveOpenPath(int[] path) Removes path from the open.booleanremoveOpenPaths(int[][] paths) Removes paths from the open.voidremovePagingEventListener(PagingListener listener) Removes a listener from the list of listeners to be notified when a PagingEvent happens outside of standard Paging componentbooleanremoveSelectionPath(int[] path) Removes path from the selection.booleanremoveSelectionPaths(int[][] paths) Removes paths from the selection.voidRemove a listener to the tree that's notified each time a change to the data model occursvoidsetActivePage(int pg) Sets the active page (starting from 0).voidsetMultiple(boolean multiple) Sets the selection mode to be multiple.voidsetOpenObjects(Collection<? extends E> opened) Replace the current set of opened objects with the given set.voidsetPageSize(int size) Sets the number of items per page.voidsetSelection(Collection<? extends E> selection) Replace the current selection with the given set.voidSets the selection control for the selection modelMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.zkoss.zul.TreeModel
getChild, getChildCount, isLeaf
-
Field Details
-
_selection
The selection. -
_opens
The open information.
-
-
Constructor Details
-
AbstractTreeModel
Creates aAbstractTreeModel.- Parameters:
root- root of tree
-
-
Method Details
-
setSelectionControl
Description copied from interface:SelectableSets the selection control for the selection model- Specified by:
setSelectionControlin interfaceSelectable<E>
-
getSelectionControl
Description copied from interface:SelectableReturns the selection control for the selection model, if any.- Specified by:
getSelectionControlin interfaceSelectable<E>
-
getTreeDataListeners
Description copied from interface:TreeModelReturns all listeners.- Specified by:
getTreeDataListenersin interfaceTreeModel<E>
-
getRoot
Return the root of the tree model. -
fireEvent
public void fireEvent(int evtType, int[] path, int indexFrom, int indexTo) Fires aTreeDataEventfor all registered listenerNote: you can invoke this method only in an event listener.
- Since:
- 6.0.0
-
fireEvent
public void fireEvent(int evtType, int[] path, int indexFrom, int indexTo, int[] affectedPath) Has the same functionality withfireEvent(int, int[], int, int), while this is used for node removal only- Since:
- 7.0.5
-
fireEvent
public void fireEvent(int evtType, int[] path, int indexFrom, int indexTo, int[][] affectedPaths) Has the same functionality withfireEvent(int, int[], int, int), while this is used for node removal only- Since:
- 10.0.0
-
fireSelectionChanged
protected void fireSelectionChanged(int[] path) Fires aTreeDataEventfor all registered listener when selection status has changed.- Since:
- 6.0.0
-
fireOpenChanged
protected void fireOpenChanged(int[] path) Fires aTreeDataEventfor all registered listener when open status has changed.- Since:
- 6.0.0
-
getIndexOfChild
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
parentby invoking, and check ifchildis part of them. You could override it if you have a better algorithm.getChild(int[])- Specified by:
getIndexOfChildin interfaceTreeModel<E>- Parameters:
parent- a node in the tree, obtained from this data sourcechild- 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
- Since:
- 5.0.6
-
getChild
Description copied from interface:TreeModelReturns the child at the given path where the path indicates the child is placed in the whole tree. -
getPath
Returns the path from the specified child. This implementation looks for the child by traversing every possible child (deep-first). It is suggested to override this method for better performance, if there is a better algorithm. -
addTreeDataListener
Description copied from interface:TreeModelAdd a listener to the tree that's notified each time a change to the data model occurs- Specified by:
addTreeDataListenerin interfaceTreeModel<E>- Parameters:
l- the listener to add
-
removeTreeDataListener
Description copied from interface:TreeModelRemove a listener to the tree that's notified each time a change to the data model occurs- Specified by:
removeTreeDataListenerin interfaceTreeModel<E>- Parameters:
l- the listener to remove
-
setMultiple
public void setMultiple(boolean multiple) Description copied from interface:TreeSelectableModelSets the selection mode to be multiple.- Specified by:
setMultiplein interfaceSelectable<E>- Specified by:
setMultiplein interfaceTreeSelectableModel
-
isMultiple
public boolean isMultiple()Description copied from interface:TreeSelectableModelReturns whether the current selection mode is multiple.- Specified by:
isMultiplein interfaceSelectable<E>- Specified by:
isMultiplein interfaceTreeSelectableModel- See Also:
-
addSelectionPath
public boolean addSelectionPath(int[] path) Description copied from interface:TreeSelectableModelAdds path to the current selection. If path is not currently in the selection the TreeDataListeners are notified. This has no effect ifpathis null.- Specified by:
addSelectionPathin interfaceTreeSelectableModel- Parameters:
path- the new path to add to the current selection- Returns:
- whether it is added successfully
-
addSelectionPaths
public boolean addSelectionPaths(int[][] paths) Description copied from interface:TreeSelectableModelAdds paths to the current selection. If any of the paths in paths are not currently in the selection the TreeDataListeners are notified. This has no effect ifpathsis null.- Specified by:
addSelectionPathsin interfaceTreeSelectableModel- Parameters:
paths- the new paths to add to the current selection- Returns:
- whether it is added successfully
-
removeSelectionPath
public boolean removeSelectionPath(int[] path) Description copied from interface:TreeSelectableModelRemoves path from the selection. If path is in the selection The TreeDataListeners are notified. This has no effect ifpathis null.- Specified by:
removeSelectionPathin interfaceTreeSelectableModel- Parameters:
path- the path to remove from the selection- Returns:
- true if it was unselected successfully
-
removeSelectionPaths
public boolean removeSelectionPaths(int[][] paths) Description copied from interface:TreeSelectableModelRemoves paths from the selection. If any of the paths inpathsare in the selection, the TreeDataListeners are notified. This method has no effect ifpathsis null.- Specified by:
removeSelectionPathsin interfaceTreeSelectableModel- Parameters:
paths- the path to remove from the selection- Returns:
- true if one of the paths was unselected successfully
-
isPathSelected
public boolean isPathSelected(int[] path) Description copied from interface:TreeSelectableModelReturns true if the path,path, is in the current selection.- Specified by:
isPathSelectedin interfaceTreeSelectableModel
-
getSelectionPath
public int[] getSelectionPath()Description copied from interface:TreeSelectableModelReturns the first path in the selection. How first is defined is up to implementors.- Specified by:
getSelectionPathin interfaceTreeSelectableModel
-
getSelectionPaths
public int[][] getSelectionPaths()Description copied from interface:TreeSelectableModelReturns the paths in the selection. This will return null (or an empty array) if nothing is currently selected.- Specified by:
getSelectionPathsin interfaceTreeSelectableModel
-
getSelectionCount
public int getSelectionCount()Description copied from interface:TreeSelectableModelReturns the number of paths that are selected.- Specified by:
getSelectionCountin interfaceTreeSelectableModel
-
isSelectionEmpty
public boolean isSelectionEmpty()Description copied from interface:TreeSelectableModelReturns true if the selection is currently empty.- Specified by:
isSelectionEmptyin interfaceSelectable<E>- Specified by:
isSelectionEmptyin interfaceTreeSelectableModel
-
clearSelection
public void clearSelection()Description copied from interface:TreeSelectableModelEmpties the current selection. If this represents a change in the current selection, the selection listeners are notified.- Specified by:
clearSelectionin interfaceSelectable<E>- Specified by:
clearSelectionin interfaceTreeSelectableModel
-
addOpenPath
public boolean addOpenPath(int[] path) Description copied from interface:TreeOpenableModelAdds path to the current open. If path is not currently in the open the TreeDataListeners are notified. This has no effect ifpathis null.- Specified by:
addOpenPathin interfaceTreeOpenableModel- Parameters:
path- the new path to add to the current open- Returns:
- whether it is added successfully
-
addOpenPaths
public boolean addOpenPaths(int[][] paths) Description copied from interface:TreeOpenableModelAdds paths to the current Open. If any of the paths in paths are not currently in the Open the TreeDataListeners are notified. This has no effect ifpathsis null.- Specified by:
addOpenPathsin interfaceTreeOpenableModel- Parameters:
paths- the new paths to add to the current Open- Returns:
- whether an of the paths is added successfully
-
removeOpenPath
public boolean removeOpenPath(int[] path) Description copied from interface:TreeOpenableModelRemoves path from the open. If path is in the open The TreeDataListeners are notified. This has no effect ifpathis null.- Specified by:
removeOpenPathin interfaceTreeOpenableModel- Parameters:
path- the path to remove from the open- Returns:
- true if it was closed successfully
-
removeOpenPaths
public boolean removeOpenPaths(int[][] paths) Description copied from interface:TreeOpenableModelRemoves paths from the open. If any of the paths inpathsare in the open, the TreeDataListeners are notified. This method has no effect ifpathsis null.- Specified by:
removeOpenPathsin interfaceTreeOpenableModel- Parameters:
paths- the path to remove from the open- Returns:
- true if one of the paths was closed successfully
-
isPathOpened
public boolean isPathOpened(int[] path) Description copied from interface:TreeOpenableModelReturns true if the path,path, is in the current open.- Specified by:
isPathOpenedin interfaceTreeOpenableModel
-
getOpenPath
public int[] getOpenPath()Description copied from interface:TreeOpenableModelReturns the first path in the open. How first is defined is up to implementors.- Specified by:
getOpenPathin interfaceTreeOpenableModel
-
getOpenPaths
public int[][] getOpenPaths()Description copied from interface:TreeOpenableModelReturns the paths in the open. This will return null (or an empty array) if nothing is currently opened.- Specified by:
getOpenPathsin interfaceTreeOpenableModel
-
getOpenCount
public int getOpenCount()Description copied from interface:TreeOpenableModelReturns the number of paths that are opened.- Specified by:
getOpenCountin interfaceTreeOpenableModel
-
isOpenEmpty
public boolean isOpenEmpty()Description copied from interface:TreeOpenableModelReturns true if the open is currently empty.- Specified by:
isOpenEmptyin interfaceOpenable<E>- Specified by:
isOpenEmptyin interfaceTreeOpenableModel
-
clearOpen
public void clearOpen()Description copied from interface:TreeOpenableModelEmpties the current open path. If this represents a change in the current open, theTreeDataListenerlisteners are notified.- Specified by:
clearOpenin interfaceOpenable<E>- Specified by:
clearOpenin interfaceTreeOpenableModel
-
beforeSort
A utility that the deriving class can call to save the states before sorting the model.Default: saves the selection and open states.
For example,
DefaultTreeModel.sort(java.util.Comparator<org.zkoss.zul.TreeNode<E>>, boolean)invokes it to preserve the selection to the same objects (rather than the same paths).- Since:
- 6.0.0
-
afterSort
A utility that the deriving class can call to restore the states saved bybeforeSort()- Since:
- 6.0.0
-
getSelection
Description copied from interface:SelectableReturns the current selection. It is readonly. Don't modify it directly- Specified by:
getSelectionin interfaceSelectable<E>- Returns:
- the current selection.
-
setSelection
Description copied from interface:SelectableReplace the current selection with the given set.If this represents a change to the current selection then notify each ListDataListener, including UI.
- Specified by:
setSelectionin interfaceSelectable<E>
-
isSelected
Description copied from interface:SelectableReturns whether an object is selected.- Specified by:
isSelectedin interfaceSelectable<E>
-
addToSelection
Description copied from interface:SelectableAdd the specified object into selection.If this represents a change to the current selection then notify each ListDataListener, including UI.
- Specified by:
addToSelectionin interfaceSelectable<E>- Parameters:
child- the object to be as selection.- Returns:
- true if it is added successfully; false if
objis not part of the data, or was already selected.
-
removeFromSelection
Description copied from interface:SelectableRemove the specified object from selection.If this represents a change to the current selection then notify each ListDataListener, including UI.
- Specified by:
removeFromSelectionin interfaceSelectable<E>- Parameters:
child- the object to be remove from selection.- Returns:
- whether it is removed successfully
-
getOpenObjects
Description copied from interface:OpenableReturns the objects that are opened. It is readonly. Don't modify it directly- Specified by:
getOpenObjectsin interfaceOpenable<E>
-
setOpenObjects
Description copied from interface:OpenableReplace the current set of opened objects with the given set.- Specified by:
setOpenObjectsin interfaceOpenable<E>
-
isObjectOpened
Description copied from interface:OpenableReturns whether the specified object be opened.- Specified by:
isObjectOpenedin interfaceOpenable<E>
-
addOpenObject
Description copied from interface:OpenableAdd the specified object into the collection of opened objects.- Specified by:
addOpenObjectin interfaceOpenable<E>- Parameters:
child- the object to be as selection.- Returns:
- true if it is added successfully; false if
objis not part of the data, or was already opened.
-
removeOpenObject
Description copied from interface:OpenableRemove the specified object from selection.- Specified by:
removeOpenObjectin interfaceOpenable<E>- Parameters:
child- the object to be remove from selection.- Returns:
- whether it is removed successfully
-
clone
-
getPageSize
public int getPageSize()Description copied from interface:PageableReturns the number of items per page.Default: 20.
- Specified by:
getPageSizein interfacePageable
-
setPageSize
public void setPageSize(int size) throws org.zkoss.zk.ui.WrongValueException Description copied from interface:PageableSets the number of items per page.- Specified by:
setPageSizein interfacePageable- Throws:
org.zkoss.zk.ui.WrongValueException
-
getTotalSize
public int getTotalSize()Returns the total number of items.
Note: the entire tree will be traversed once to count the number of tree nodes, although the result will be cached until the model has changed (open/add/remove/etc.), it is still a VERY EXPENSIVE operation, please @Override to provide your own implementation for better performance- Specified by:
getTotalSizein interfacePageableModel- Returns:
- number of total size, or 0 if the model is empty
- Since:
- 10.0.0
-
getPageCount
public int getPageCount()Returns the number of pages. Note: there is at least one page even no item at all.
Note: the entire tree will be traversed once to count the number of tree nodes, although the result will be cached until the model has changed (open/add/remove/etc.), it is still a VERY EXPENSIVE operation, please @Override to provide your own implementation for better performance- Specified by:
getPageCountin interfacePageable- Returns:
- number of pages, or 1 if the model is empty
- Since:
- 8.0.0
-
getActivePage
public int getActivePage()Description copied from interface:PageableReturns the active page (starting from 0).- Specified by:
getActivePagein interfacePageable
-
setActivePage
public void setActivePage(int pg) throws org.zkoss.zk.ui.WrongValueException Description copied from interface:PageableSets the active page (starting from 0).- Specified by:
setActivePagein interfacePageable- Throws:
org.zkoss.zk.ui.WrongValueException
-
addPagingEventListener
Description copied from interface:PageableModelAdds a listener to the list of listeners to be notified when a PagingEvent happens outside of standard Paging component- Specified by:
addPagingEventListenerin interfacePageableModel
-
removePagingEventListener
Description copied from interface:PageableModelRemoves a listener from the list of listeners to be notified when a PagingEvent happens outside of standard Paging component- Specified by:
removePagingEventListenerin interfacePageableModel
-