com.alee.extended.tree
Class AsyncTreeModel<E extends AsyncUniqueNode>

java.lang.Object
  extended by javax.swing.tree.DefaultTreeModel
      extended by com.alee.laf.tree.WebTreeModel<E>
          extended by com.alee.extended.tree.AsyncTreeModel<E>
Type Parameters:
E - custom node type
All Implemented Interfaces:
java.io.Serializable, javax.swing.tree.TreeModel

public class AsyncTreeModel<E extends AsyncUniqueNode>
extends WebTreeModel<E>

Special model for asynchronous tree that provides asynchronous data loading. This class also controls the loading animation in elements.

Author:
Mikle Garin
See Also:
Serialized Form

Field Summary
protected  boolean asyncLoading
          Whether to load childs asynchronously or not.
protected  java.util.List<AsyncTreeModelListener> asyncTreeModelListeners
          Asynchronous tree listeners.
protected  java.lang.Object busyLock
          Lock object for busy state changes.
protected  java.lang.Object cacheLock
          Lock object for cache changes.
protected  AsyncTreeDataProvider<E> dataProvider
          Asynchronous tree data provider.
protected  java.lang.Object modelListenersLock
          Lock object for asynchronous tree listeners.
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 static java.lang.String ROOT_CACHE
          Cache key for root node.
protected  E rootNode
          Root node cache.
protected  WebAsyncTree<E> tree
          Asynchronous tree that uses this model.
 
Fields inherited from class javax.swing.tree.DefaultTreeModel
asksAllowsChildren, listenerList, root
 
Constructor Summary
AsyncTreeModel(WebAsyncTree<E> tree, AsyncTreeDataProvider<E> dataProvider)
          Constructs default asynchronous tree model using custom data provider.
 
Method Summary
 void addAsyncTreeModelListener(AsyncTreeModelListener listener)
          Adds new asynchronous tree model listener.
 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.
protected  void fireChildsLoadCompleted(E parent, java.util.List<E> childs)
          Fires childs load complete event.
protected  void fireChildsLoadFailed(E parent, java.lang.Throwable cause)
          Fires childs load failed event.
protected  void fireChildsLoadStarted(E parent)
          Fires childs load start event.
 java.util.List<AsyncTreeModelListener> getAsyncTreeModelListeners()
          Returns list of all available asynchronous tree model listeners.
 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.
 AsyncTreeDataProvider<E> getDataProvider()
          Returns asynchronous 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.
 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(java.util.List<E> children, E parent, int index)
          Inserts a list of child nodes into parent node.
 boolean isAsyncLoading()
          Returns whether childs are loaded asynchronously or not.
 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 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.
protected  void registerObserver(E node)
          Registers image observer for loader icon of the specified node.
protected  void registerObservers(E[] nodes)
          Registers image observer for loader icons of the specified nodes.
protected  void registerObservers(java.util.List<E> nodes)
          Registers image observer for loader icons of the specified nodes.
 void reload(javax.swing.tree.TreeNode node)
          Reloads node childs.
 void removeAsyncTreeModelListener(AsyncTreeModelListener listener)
          Removes asynchronous tree model listener.
 void removeNodeFromParent(javax.swing.tree.MutableTreeNode node)
          Removes specified node from parent node.
 void setAsyncLoading(boolean asyncLoading)
          Sets whether to load childs asynchronously or not.
 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 loaded 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
 
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, valueForPathChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ROOT_CACHE

protected static final java.lang.String ROOT_CACHE
Cache key for root node.

See Also:
Constant Field Values

modelListenersLock

protected final java.lang.Object modelListenersLock
Lock object for asynchronous tree listeners.


asyncTreeModelListeners

protected final java.util.List<AsyncTreeModelListener> asyncTreeModelListeners
Asynchronous tree listeners.


tree

protected WebAsyncTree<E extends AsyncUniqueNode> tree
Asynchronous tree that uses this model.


asyncLoading

protected boolean asyncLoading
Whether to load childs asynchronously or not.


dataProvider

protected AsyncTreeDataProvider<E extends AsyncUniqueNode> dataProvider
Asynchronous tree data provider.


rootNode

protected E extends AsyncUniqueNode 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 AsyncUniqueNode>> 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 AsyncUniqueNode> nodeById
Direct nodes cache (node ID -> node). Used for quick node search within the tree.


busyLock

protected final java.lang.Object busyLock
Lock object for busy state changes.

Constructor Detail

AsyncTreeModel

public AsyncTreeModel(WebAsyncTree<E> tree,
                      AsyncTreeDataProvider<E> dataProvider)
Constructs default asynchronous tree model using custom data provider.

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

isAsyncLoading

public boolean isAsyncLoading()
Returns whether childs are loaded asynchronously or not.

Returns:
true if childs are loaded asynchronously, false otherwise

setAsyncLoading

public void setAsyncLoading(boolean asyncLoading)
Sets whether to load childs asynchronously or not.

Parameters:
asyncLoading - whether to load childs asynchronously or not

getDataProvider

public AsyncTreeDataProvider<E> getDataProvider()
Returns asynchronous 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

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 AsyncUniqueNode>
Parameters:
children - list of new child nodes
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. This method is used by model itself to add nodes when they are loaded so it doesn't update raw nodes cache.

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 AsyncUniqueNode>
Parameters:
children - array of new child nodes
parent - parent node
index - insert index

updateSortingAndFiltering

public void updateSortingAndFiltering()
Updates nodes sorting and filtering for all loaded 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

registerObservers

protected void registerObservers(java.util.List<E> nodes)
Registers image observer for loader icons of the specified nodes.

Parameters:
nodes - nodes

registerObservers

protected void registerObservers(E[] nodes)
Registers image observer for loader icons of the specified nodes.

Parameters:
nodes - nodes

registerObserver

protected void registerObserver(E node)
Registers image observer for loader icon of the specified node.

Parameters:
node - node

getAsyncTreeModelListeners

public java.util.List<AsyncTreeModelListener> getAsyncTreeModelListeners()
Returns list of all available asynchronous tree model listeners.

Returns:
asynchronous tree model listeners list

addAsyncTreeModelListener

public void addAsyncTreeModelListener(AsyncTreeModelListener listener)
Adds new asynchronous tree model listener.

Parameters:
listener - asynchronous tree model listener to add

removeAsyncTreeModelListener

public void removeAsyncTreeModelListener(AsyncTreeModelListener listener)
Removes asynchronous tree model listener.

Parameters:
listener - asynchronous tree model listener to remove

fireChildsLoadStarted

protected void fireChildsLoadStarted(E parent)
Fires childs load start event.

Parameters:
parent - node which childs are being loaded

fireChildsLoadCompleted

protected void fireChildsLoadCompleted(E parent,
                                       java.util.List<E> childs)
Fires childs load complete event.

Parameters:
parent - node which childs were loaded
childs - loaded child nodes

fireChildsLoadFailed

protected void fireChildsLoadFailed(E parent,
                                    java.lang.Throwable cause)
Fires childs load failed event.

Parameters:
parent - node which childs were loaded
cause - childs load failure cause