com.alee.extended.tree
Class ExTreeModel<E extends UniqueNode>

java.lang.Object
  extended by javax.swing.tree.DefaultTreeModel
      extended by com.alee.laf.tree.WebTreeModel<E>
          extended by com.alee.extended.tree.ExTreeModel<E>
All Implemented Interfaces:
java.io.Serializable, javax.swing.tree.TreeModel

public class ExTreeModel<E extends UniqueNode>
extends WebTreeModel<E>

Author:
Mikle Garin
See Also:
WebExTree, ExTreeDataProvider, Serialized Form

Field Summary
protected  java.lang.Object cacheLock
          Lock object for cache changes.
protected  ExTreeDataProvider<E> dataProvider
          Ex tree data provider.
protected  DoubleMap<java.lang.String,E> nodeById
          Direct nodes cache (node ID -> node).
protected  java.util.Map<java.lang.String,java.lang.Boolean> nodeCached
          Nodes cached states (parent ID -> childs cached state).
protected  java.util.Map<java.lang.String,java.util.List<E>> rawNodeChildsCache
          Cache for childs nodes returned by data provider (parent ID -> list of raw child nodes).
protected  E rootNode
          Root node cache.
protected  WebTree<E> tree
          Ex tree that uses this model.
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Constructor Summary
ExTreeModel(WebTree<E> tree, ExTreeDataProvider<E> dataProvider)
          Constructs default ex tree model using custom data provider.
 
Method Summary
 void addChildNodes(E parent, java.util.List<E> childs)
          Adds child nodes for the specified node.
 boolean areChildsLoaded(E node)
          Returns whether childs for the specified node are already loaded or not.
protected  void cacheNodeById(E node)
          Caches node by its IDs.
protected  void cacheNodesById(java.util.List<E> nodes)
          Caches nodes by their IDs.
protected  void clearNodeChildsCache(E[] nodes, boolean clearNodes)
          Clears nodes childs cached states.
protected  void clearNodeChildsCache(E node, boolean clearNode)
          Clears node and all of its child nodes childs cached states.
protected  void clearNodeChildsCache(java.util.List<E> nodes, boolean clearNodes)
          Clears nodes childs cached states.
protected  java.util.List<E> filterAndSort(E parentNode, java.util.List<E> childs)
          Performs raw childs filtering and sorting before they can be passed into real tree and returns list of filtered and sorted childs.
 E findNode(java.lang.String nodeId)
          Looks for the node with the specified ID in the tree model and returns it or null if it was not found.
 E getChild(java.lang.Object parent, int index)
          Returns child node for parent node at the specified index.
 int getChildCount(java.lang.Object parent)
          Returns childs count for specified node.
 ExTreeDataProvider<E> getDataProvider()
          Returns ex tree data provider.
 DoubleMap<java.lang.String,E> getNodesCache()
          Returns nodes cache map copy.
 E getRoot()
          Returns tree root node.
 void insertNodeInto(javax.swing.tree.MutableTreeNode newChild, javax.swing.tree.MutableTreeNode parent, int index)
          Inserts new child node into parent node at the specified index.
protected  void insertNodeIntoImpl(E child, E parent, int index)
          Inserts a child node into parent node.
 void insertNodesInto(E[] children, E parent, int index)
          Inserts an array of child nodes into parent node.
 void insertNodesInto(java.util.List<E> children, E parent, int index)
          Inserts a list of child nodes into parent node.
protected  void insertNodesIntoImpl(E[] children, E parent, int index)
          Inserts an array of child nodes into parent node.
protected  void insertNodesIntoImpl(java.util.List<E> children, E parent, int index)
          Inserts a list of child nodes into parent node.
 boolean isLeaf(java.lang.Object node)
          Returns whether the specified node is leaf or not.
protected  int loadChildsCount(E parent)
          Loads (or reloads) node childs and returns zero or childs count if async mode is off.
protected  void loadTreeData(E node)
          Forces model to cache the whole structure so any node can be accessed right away.
protected  void performSortingAndFiltering(E parentNode, boolean recursively)
          Updates node childs using current comparator and filter.
protected  void performSortingAndFilteringImpl(E parentNode)
          Updates node childs recursively using current comparator and filter.
protected  void performSortingAndFilteringRecursivelyImpl(E parentNode)
          Updates node childs using current comparator and filter.
 void reload(javax.swing.tree.TreeNode node)
          Reloads node childs.
 void removeNodeFromParent(javax.swing.tree.MutableTreeNode node)
          Removes specified node from parent node.
 void setChildNodes(E parent, java.util.List<E> childs)
          Sets child nodes for the specified node.
 void updateSortingAndFiltering()
          Updates nodes sorting and filtering for all nodes.
 void updateSortingAndFiltering(E parentNode)
          Updates sorting and filtering for the specified node childs.
 void updateSortingAndFiltering(E parentNode, boolean recursively)
          Updates sorting and filtering for the specified node childs.
 
Methods inherited from class com.alee.laf.tree.WebTreeModel
getRootNode, removeNodesFromParent, removeNodesFromParent, removeNodesFromParent, updateNode, updateNodeStructure, updateTree, valueForPathChanged
 
Methods inherited from class javax.swing.tree.DefaultTreeModel
addTreeModelListener, asksAllowsChildren, fireTreeNodesChanged, fireTreeNodesInserted, fireTreeNodesRemoved, fireTreeStructureChanged, getIndexOfChild, getListeners, getPathToRoot, getPathToRoot, getTreeModelListeners, nodeChanged, nodesChanged, nodeStructureChanged, nodesWereInserted, nodesWereRemoved, reload, removeTreeModelListener, setAsksAllowsChildren, setRoot
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tree

protected final WebTree<E extends UniqueNode> tree
Ex tree that uses this model.


dataProvider

protected final ExTreeDataProvider<E extends UniqueNode> dataProvider
Ex tree data provider.


rootNode

protected E extends UniqueNode rootNode
Root node cache. Cached when root is requested for the first time.


cacheLock

protected final java.lang.Object cacheLock
Lock object for cache changes.


nodeCached

protected final java.util.Map<java.lang.String,java.lang.Boolean> nodeCached
Nodes cached states (parent ID -> childs cached state). If child nodes for some parent node are cached then this map contains "true" value under that parent node ID as a key.


rawNodeChildsCache

protected final java.util.Map<java.lang.String,java.util.List<E extends UniqueNode>> rawNodeChildsCache
Cache for childs nodes returned by data provider (parent ID -> list of raw child nodes). This map contains raw childs which weren't affected by sorting and filtering operations. If childs needs to be re-sorted or re-filtered they are simply taken from the cache and re-organized once again.


nodeById

protected final DoubleMap<java.lang.String,E extends UniqueNode> nodeById
Direct nodes cache (node ID -> node). Used for quick node search within the tree.

Constructor Detail

ExTreeModel

public ExTreeModel(WebTree<E> tree,
                   ExTreeDataProvider<E> dataProvider)
Constructs default ex tree model using custom data provider.

Parameters:
tree - asynchronous tree
dataProvider - data provider
Method Detail

getDataProvider

public ExTreeDataProvider<E> getDataProvider()
Returns ex tree data provider.

Returns:
data provider

getRoot

public E getRoot()
Returns tree root node.

Specified by:
getRoot in interface javax.swing.tree.TreeModel
Overrides:
getRoot in class javax.swing.tree.DefaultTreeModel
Returns:
root node

isLeaf

public boolean isLeaf(java.lang.Object node)
Returns whether the specified node is leaf or not.

Specified by:
isLeaf in interface javax.swing.tree.TreeModel
Overrides:
isLeaf in class javax.swing.tree.DefaultTreeModel
Parameters:
node - node
Returns:
true if node is leaf, false otherwise

getChildCount

public int getChildCount(java.lang.Object parent)
Returns childs count for specified node.

Specified by:
getChildCount in interface javax.swing.tree.TreeModel
Overrides:
getChildCount in class javax.swing.tree.DefaultTreeModel
Parameters:
parent - parent node
Returns:
childs count

getChild

public E getChild(java.lang.Object parent,
                  int index)
Returns child node for parent node at the specified index.

Specified by:
getChild in interface javax.swing.tree.TreeModel
Overrides:
getChild in class javax.swing.tree.DefaultTreeModel
Parameters:
parent - parent node
index - child node index
Returns:
child node

areChildsLoaded

public boolean areChildsLoaded(E node)
Returns whether childs for the specified node are already loaded or not.

Parameters:
node - node to process
Returns:
true if childs for the specified node are already loaded, false otherwise

loadTreeData

protected void loadTreeData(E node)
Forces model to cache the whole structure so any node can be accessed right away. Note that this might take some time in case tree structure is large. Though this doesn't force any repaints or other visual updates, so the speed depends only on ExTreeDataProvider.


reload

public void reload(javax.swing.tree.TreeNode node)
Reloads node childs.

Overrides:
reload in class javax.swing.tree.DefaultTreeModel
Parameters:
node - node

clearNodeChildsCache

protected void clearNodeChildsCache(E node,
                                    boolean clearNode)
Clears node and all of its child nodes childs cached states.

Parameters:
node - node to clear cache for
clearNode - whether should clear node cache or not

clearNodeChildsCache

protected void clearNodeChildsCache(java.util.List<E> nodes,
                                    boolean clearNodes)
Clears nodes childs cached states.

Parameters:
nodes - nodes to clear cache for
clearNodes - whether should clear nodes cache or not

clearNodeChildsCache

protected void clearNodeChildsCache(E[] nodes,
                                    boolean clearNodes)
Clears nodes childs cached states.

Parameters:
nodes - nodes to clear cache for
clearNodes - whether should clear nodes cache or not

cacheNodeById

protected void cacheNodeById(E node)
Caches node by its IDs.

Parameters:
node - node to cache

cacheNodesById

protected void cacheNodesById(java.util.List<E> nodes)
Caches nodes by their IDs.

Parameters:
nodes - list of nodes to cache

loadChildsCount

protected int loadChildsCount(E parent)
Loads (or reloads) node childs and returns zero or childs count if async mode is off. This is base method that uses installed AsyncTreeDataProvider to retrieve tree node childs.

Parameters:
parent - node to load childs for
Returns:
zero or childs count if async mode is off
See Also:
AsyncTreeDataProvider.loadChilds(AsyncUniqueNode, ChildsListener)

setChildNodes

public void setChildNodes(E parent,
                          java.util.List<E> childs)
Sets child nodes for the specified node. This method might be used to manually change tree node childs without causing any structure corruptions.

Parameters:
parent - node to process
childs - new node childs

addChildNodes

public void addChildNodes(E parent,
                          java.util.List<E> childs)
Adds child nodes for the specified node. This method might be used to manually change tree node childs without causing any structure corruptions.

Parameters:
parent - node to process
childs - new node childs

removeNodeFromParent

public void removeNodeFromParent(javax.swing.tree.MutableTreeNode node)
Removes specified node from parent node.

Overrides:
removeNodeFromParent in class javax.swing.tree.DefaultTreeModel
Parameters:
node - node to remove

insertNodeInto

public void insertNodeInto(javax.swing.tree.MutableTreeNode newChild,
                           javax.swing.tree.MutableTreeNode parent,
                           int index)
Inserts new child node into parent node at the specified index.

Overrides:
insertNodeInto in class javax.swing.tree.DefaultTreeModel
Parameters:
newChild - new child node
parent - parent node
index - insert index

insertNodesInto

public void insertNodesInto(java.util.List<E> children,
                            E parent,
                            int index)
Inserts a list of child nodes into parent node.

Overrides:
insertNodesInto in class WebTreeModel<E extends UniqueNode>
Parameters:
children - list of new child nodes
parent - parent node
index - insert index

insertNodesInto

public void insertNodesInto(E[] children,
                            E parent,
                            int index)
Inserts an array of child nodes into parent node.

Overrides:
insertNodesInto in class WebTreeModel<E extends UniqueNode>
Parameters:
children - array of new child nodes
parent - parent node
index - insert index

insertNodeIntoImpl

protected void insertNodeIntoImpl(E child,
                                  E parent,
                                  int index)
Inserts a child node into parent node.

Parameters:
child - new child node
parent - parent node
index - insert index

insertNodesIntoImpl

protected void insertNodesIntoImpl(java.util.List<E> children,
                                   E parent,
                                   int index)
Inserts a list of child nodes into parent node.

Parameters:
children - list of new child nodes
parent - parent node
index - insert index

insertNodesIntoImpl

protected void insertNodesIntoImpl(E[] children,
                                   E parent,
                                   int index)
Inserts an array of child nodes into parent node.

Parameters:
children - array of new child nodes
parent - parent node
index - insert index

updateSortingAndFiltering

public void updateSortingAndFiltering()
Updates nodes sorting and filtering for all nodes.


updateSortingAndFiltering

public void updateSortingAndFiltering(E parentNode)
Updates sorting and filtering for the specified node childs.

Parameters:
parentNode - node which childs sorting and filtering should be updated

updateSortingAndFiltering

public void updateSortingAndFiltering(E parentNode,
                                      boolean recursively)
Updates sorting and filtering for the specified node childs.

Parameters:
parentNode - node which childs sorting and filtering should be updated
recursively - whether should update the whole childs structure recursively or not

performSortingAndFiltering

protected void performSortingAndFiltering(E parentNode,
                                          boolean recursively)
Updates node childs using current comparator and filter. Updates the whole node childs structure if recursive update requested.

Parameters:
parentNode - node which childs sorting and filtering should be updated
recursively - whether should update the whole childs structure recursively or not

performSortingAndFilteringRecursivelyImpl

protected void performSortingAndFilteringRecursivelyImpl(E parentNode)
Updates node childs using current comparator and filter.

Parameters:
parentNode - node to update

performSortingAndFilteringImpl

protected void performSortingAndFilteringImpl(E parentNode)
Updates node childs recursively using current comparator and filter.

Parameters:
parentNode - node to update

filterAndSort

protected java.util.List<E> filterAndSort(E parentNode,
                                          java.util.List<E> childs)
Performs raw childs filtering and sorting before they can be passed into real tree and returns list of filtered and sorted childs.

Parameters:
childs - childs to filter and sort
Returns:
list of filtered and sorted childs

findNode

public E findNode(java.lang.String nodeId)
Looks for the node with the specified ID in the tree model and returns it or null if it was not found.

Parameters:
nodeId - node ID
Returns:
node with the specified ID or null if it was not found

getNodesCache

public DoubleMap<java.lang.String,E> getNodesCache()
Returns nodes cache map copy.

Returns:
nodes cache map copy