jaxx.runtime.swing.nav
Class NavHelper<M,U,B extends NavBridge<M,N>,N extends NavNode<M,N>>

java.lang.Object
  extended by jaxx.runtime.swing.nav.NavHelper<M,U,B,N>
Type Parameters:
U - Type of ui to bridge
Direct Known Subclasses:
NavTreeHelper, NavTreeTableHelper

public abstract class NavHelper<M,U,B extends NavBridge<M,N>,N extends NavNode<M,N>>
extends Object

Tree helper to deal with the build of trees and other usefull operations.

A helper acts as an handler for a tree. It owns the getModel() of the #tree.

Note: A helper can NOT be used to manage multi-trees.

Internal states

Internal model To create the model, use method createModel(NavNode ,Object...) given a root node.

To obtain the model, use method getModel().

Note: The helper internal model can be different from the tree model, but must be the lowest model, other models must listen nicely this model to obtain model modification and selection notifications.

Internal tree

As said before, a helper matches exactly one tree.

To register the tree, use method setUI(Object, boolean, TreeSelectionListener).

To obtain the tree, use method getUI().

Internal data provider

To populate childs nodes and render nodes, we use a NavDataProvider.

To register the data provider, use method setDataProvider(NavDataProvider).

To obtain the data provider, use method getDataProvider().

Internal listeners

Several listeners are used to manage the auto-loading of nodes in model :

expandListener

This listener will load node's childs before node expands if the node is not loaded.

See the NavNode.isLoaded() method.

treeModelListener

To listen modification of the model, it will mainly repopulate nodes when required.

See the method populateNode(NavNode , Object[], boolean).

selectionListener

To listen modification of the selection, it will mainly expand paths if required.

This is a requirement, since childs of a node should NOT be loaded, so when selects a node, always check the path from root to selected node are all fully loaded.

Model methods

The helper offers some methods to modify and query the internal tree model.

Model modification

Model selection modification

Model query

Child loadors factory

The class offers a factory of NavNodeChildLoador, use the method getChildLoador(Class) to obtain the correct child loador given his type.

Since:
2.1
Author:
tchemit
See Also:
NavNode, NavNodeChildLoador, AbstractNavTreeCellRenderer

Field Summary
protected static Set<? super NavNodeChildLoador<?,?,?,?,?>> childLoadors
          Cache of child loadors.
protected  NavDataProvider dataProvider
          The shared data provider used to obtain datas to populate nodes and render them.
protected  TreeWillExpandListener expandListener
          A TreeWillExpandListener used to listen when tree should expand.
protected  TreeSelectionListener selectionListener
          pour ouvrir les fils d'un noeud que l'on vient de sélectionner pour éviter d'avoir à faire des doubles clics.
protected  TreeModelListener treeModelListener
          pour recharger le rendu des noeuds (et charger les fils si nécessaires) lors d'une modification dans le modèle de l'arbre.
 
Constructor Summary
NavHelper(B bridge)
           
 
Method Summary
abstract  void addSelectionPath(TreePath path)
           
abstract  void addSelectionPaths(TreePath[] paths)
           
protected  boolean checkModel()
          Checks if internal model was created.
protected abstract  M createModel(N node, Object... extraArgs)
          Register a new root node.
abstract  void expandPath(TreePath pathToExpand)
           
 N findNode(N node, String... ids)
          Finds a node from the given root node, applying the path given by ids.
protected  B getBridge()
          Obtains the bridge .
 N getChild(N node, String id)
          Finds a node from the given root node, and return child searched
static
<L extends NavNodeChildLoador<?,?,?,?,?>>
L
getChildLoador(Class<L> type)
          Obtains the NavNodeChildLoador of the given type from internal cache.
protected static Set<? super NavNodeChildLoador<?,?,?,?,?>> getChildLoadors()
           
protected  NavDataProvider getDataProvider()
          Obtains the attached data provider used to populate and render nodes.
 M getModel()
          Obtains the model.
protected  N getNode(TreePath path)
          Convinient method to objet the casted node of a TreePath.
 N getRootNode()
           
 String[] getSelectedIds()
          Obtains the path of ids fro the root node to the selected node on the registred tree.
abstract  N getSelectedNode()
          Obtains the selected node of the registred tree.
abstract  List<N> getSelectedNodes()
          Obtains the selected nodes of the registred tree.
abstract  TreeSelectionModel getSelectionModel()
           
abstract  AbstractNavTreeCellRenderer<M,N> getTreeCellRenderer()
          Obtains the AbstractNavTreeCellRenderer renderer of the registred tree.
 U getUI()
          Obtains the ui associated with model in helper.
 void insertNode(N parentNode, N newNode)
          Inserts the given node to the given parentNode.
 void insertNode(N parentNode, N newNode, int position)
          Inserts the given node to the given parentNode.
abstract  boolean isExpanded(TreePath pathToExpand)
           
 void loadAllNodes(N node, NavDataProvider dataProvider)
          To load all nodes of a model.
 void moveNode(N parentNode, N node, int position)
          Moves the given node to the new position.
protected  void populateNode(N node, Object[] children, boolean recurse)
          Populates nodes.
 void refreshNode(N node, boolean deep)
          Refreshs the given node.
 N removeNode(N node)
          Removes the given node from the registred tree model and returns his parent.
abstract  void removeSelectionPath(TreePath path)
           
abstract  void removeSelectionPaths(TreePath[] paths)
           
abstract  void scrollPathToVisible(TreePath path)
           
 void selectNode(N node)
          Selects the given node in the registred tree.
 void selectNode(String... path)
          Selects the node described by his given path of ids.
 void selectNodes(List<N> nodes)
          Selects the given nodes in the registred tree.
 void selectParentNode()
          Selects the parent of the currently selected node.
 void setDataProvider(NavDataProvider dataProvider)
          Registers the dataProvider for the helper.
abstract  void setSelectionPath(TreePath path)
           
protected  void setUI(U ui)
           
 void setUI(U tree, boolean addExpandTreeListener)
          Registers the given tree for this helper.
 void setUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, TreeSelectionListener listener)
          Registers the given tree for this helper.
abstract  void setUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, TreeSelectionListener listener, TreeWillExpandListener willExpandListener)
          Registers the given tree for this helper.
 void setUI(U tree, boolean addExpandTreeListener, TreeSelectionListener listener)
          Registers the given tree for this helper.
 void unSelectNode(N node)
          Unselects the given node in the registred tree.
 void unSelectNodes(List<N> nodes)
          Selects the given nodes in the registred tree.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataProvider

protected NavDataProvider dataProvider
The shared data provider used to obtain datas to populate nodes and render them.


expandListener

protected TreeWillExpandListener expandListener
A TreeWillExpandListener used to listen when tree should expand.

If so, the listener will load selected node childs if required (says when the NavNode.isLoaded() is sets to false).


selectionListener

protected TreeSelectionListener selectionListener
pour ouvrir les fils d'un noeud que l'on vient de sélectionner pour éviter d'avoir à faire des doubles clics.


treeModelListener

protected TreeModelListener treeModelListener
pour recharger le rendu des noeuds (et charger les fils si nécessaires) lors d'une modification dans le modèle de l'arbre.


childLoadors

protected static Set<? super NavNodeChildLoador<?,?,?,?,?>> childLoadors
Cache of child loadors.

Constructor Detail

NavHelper

public NavHelper(B bridge)
Method Detail

scrollPathToVisible

public abstract void scrollPathToVisible(TreePath path)

setSelectionPath

public abstract void setSelectionPath(TreePath path)

addSelectionPath

public abstract void addSelectionPath(TreePath path)

addSelectionPaths

public abstract void addSelectionPaths(TreePath[] paths)

removeSelectionPath

public abstract void removeSelectionPath(TreePath path)

removeSelectionPaths

public abstract void removeSelectionPaths(TreePath[] paths)

getSelectionModel

public abstract TreeSelectionModel getSelectionModel()

isExpanded

public abstract boolean isExpanded(TreePath pathToExpand)

expandPath

public abstract void expandPath(TreePath pathToExpand)

createModel

protected abstract M createModel(N node,
                                 Object... extraArgs)
Register a new root node.

If internal getModel() does not exists, creates a new one from his given root node, otherwise just set the new root on the existing model.

Note: As a side-effect, the model will be keep in field getModel() and the treeModelListener will be registred on this model.

Parameters:
node - the root node of the new model
extraArgs - extra args to create initial model
Returns:
the new model

getTreeCellRenderer

public abstract AbstractNavTreeCellRenderer<M,N> getTreeCellRenderer()
Obtains the AbstractNavTreeCellRenderer renderer of the registred tree.

Returns:
the renderer of the registred tree or null if no tree was registred nor the renderer is a AbstractNavTreeCellRenderer.

getSelectedNode

public abstract N getSelectedNode()
Obtains the selected node of the registred tree.

Returns:
the selected tree or null if no registred tree nor selection empty.

getSelectedNodes

public abstract List<N> getSelectedNodes()
Obtains the selected nodes of the registred tree.

Returns:
the selected tree or null if no registred tree nor selection empty.

setUI

public abstract void setUI(U tree,
                           boolean addExpandTreeListener,
                           boolean addOneClickSelectionListener,
                           TreeSelectionListener listener,
                           TreeWillExpandListener willExpandListener)
Registers the given tree for this helper.

Note: as a side-effect, it will register (if required) the expandListener listener and (if required) the selectionListener. Note : as a second side-effect, it will register the given willExpandListener (if not null) for the ui and do it BEFORE the expandListener. The main idea here is to be able to block any expand (or collapse).

Parameters:
tree - the tree to register
addExpandTreeListener - a flag to add expand listener
addOneClickSelectionListener - a flag to expend when selection
listener - the optional selection listener to add
willExpandListener - the optional will expand listener to add BEFORE the default expand tree listener (if he was required)
Since:
2.1.2

getChildLoadors

protected static Set<? super NavNodeChildLoador<?,?,?,?,?>> getChildLoadors()

getChildLoador

public static <L extends NavNodeChildLoador<?,?,?,?,?>> L getChildLoador(Class<L> type)
Obtains the NavNodeChildLoador of the given type from internal cache.

Note: The loador will be instanciated if not found, and push in cache.

Type Parameters:
L - the type of loador to get
Parameters:
type - the type of loador to get
Returns:
the loador from cache

getDataProvider

protected NavDataProvider getDataProvider()
Obtains the attached data provider used to populate and render nodes.

Returns:
the attached data provider

getModel

public M getModel()
Obtains the model.

Returns:
the internal tree model or null if none was created.

getBridge

protected B getBridge()
Obtains the bridge .

Returns:
the internal bridge used by helper.

getUI

public U getUI()
Obtains the ui associated with model in helper.

Returns:
the ui (or null if no ui attached)

getRootNode

public N getRootNode()

getSelectedIds

public String[] getSelectedIds()
Obtains the path of ids fro the root node to the selected node on the registred tree.

Returns:
the array of ids from root node to selected node.

setUI

public void setUI(U tree,
                  boolean addExpandTreeListener)
Registers the given tree for this helper.

Note: as a side-effect, it will register (if required) the expandListener listener and the selectionListener.

Parameters:
tree - the tree to register
addExpandTreeListener - a flag to add expand listener

setUI

public void setUI(U tree,
                  boolean addExpandTreeListener,
                  TreeSelectionListener listener)
Registers the given tree for this helper.

Note: as a side-effect, it will register (if required) the expandListener listener and the selectionListener.

Parameters:
tree - the tree to register
addExpandTreeListener - a flag to add expand listener
listener - the optional selection listener to add

setUI

public void setUI(U tree,
                  boolean addExpandTreeListener,
                  boolean addOneClickSelectionListener,
                  TreeSelectionListener listener)
Registers the given tree for this helper.

Note: as a side-effect, it will register (if required) the expandListener listener and (if required) the selectionListener.

Parameters:
tree - the tree to register
addExpandTreeListener - a flag to add expand listener
addOneClickSelectionListener - a flag to expend when selection
listener - the optional selection listener to add

setDataProvider

public void setDataProvider(NavDataProvider dataProvider)
Registers the dataProvider for the helper.

Node: As a side-effect, the provider will be propagate to the renderer of the registred tree (if any).

Parameters:
dataProvider - the data provider to use

insertNode

public void insertNode(N parentNode,
                       N newNode)
Inserts the given node to the given parentNode.

The node will be added to his parent, then creation listeners will be fired.

Parameters:
parentNode - the parent node where to insert the new node *
newNode - the node to insert

insertNode

public void insertNode(N parentNode,
                       N newNode,
                       int position)
Inserts the given node to the given parentNode.

The node will be added to his parent, then creation listeners will be fired.

Parameters:
parentNode - the parent node where to insert the new node *
newNode - the node to insert
position - position of node is inserted

removeNode

public N removeNode(N node)
Removes the given node from the registred tree model and returns his parent.

Parameters:
node - the node to remove
Returns:
the parent node of the removed node.

moveNode

public void moveNode(N parentNode,
                     N node,
                     int position)
Moves the given node to the new position.

Parameters:
parentNode - the parent node
node - the node to move
position - the new position of the node

refreshNode

public void refreshNode(N node,
                        boolean deep)
Refreshs the given node.

If flag deep is set to true, then it will refresh recursively children nodes.

Note:As a side-effect, evvery node involved will become dirty.

Parameters:
node - the node to refresh
deep - un flag pour activer la repainte de la descendance du noeud
See Also:
NavNode.isDirty()

loadAllNodes

public void loadAllNodes(N node,
                         NavDataProvider dataProvider)
To load all nodes of a model.

Parameters:
node - the root node to load
dataProvider - the data provider used to populate nodes

selectParentNode

public void selectParentNode()
                      throws NullPointerException
Selects the parent of the currently selected node.

Note: If selection is empty, then throws a NPE.

Throws:
NullPointerException - if selection is empty

selectNode

public void selectNode(N node)
Selects the given node in the registred tree.

Parameters:
node - the node to select

selectNodes

public void selectNodes(List<N> nodes)
Selects the given nodes in the registred tree.

Parameters:
nodes - the nodes to select

unSelectNode

public void unSelectNode(N node)
Unselects the given node in the registred tree.

Parameters:
node - the node to select

unSelectNodes

public void unSelectNodes(List<N> nodes)
Selects the given nodes in the registred tree.

Parameters:
nodes - the nodes to select

selectNode

public void selectNode(String... path)
Selects the node described by his given path of ids.

Parameters:
path - the absolute path of ids from root node to node to select.

findNode

public N findNode(N node,
                  String... ids)
Finds a node from the given root node, applying the path given by ids.

Parameters:
node - the starting node
ids - the path of ids to apply on the node.
Returns:
the find node or null if no node matchs.

getChild

public N getChild(N node,
                  String id)
Finds a node from the given root node, and return child searched

Parameters:
node - the starting node
id - of child searched
Returns:
the find node or null if no node matchs.

checkModel

protected boolean checkModel()
Checks if internal model was created.

Returns:
true if model was created, false otherwise.

populateNode

protected void populateNode(N node,
                            Object[] children,
                            boolean recurse)
Populates nodes.

If node is not null, then populate it.

If children is not null, then populate them, moreover if recurse is set to true then do a recurse refresh on children.

Parameters:
node - the parent node to populate (optional)
children - the child nodes to populate (optional)
recurse - flag sets to true if should do recurse refresh on given children nodes.

getNode

protected N getNode(TreePath path)
Convinient method to objet the casted node of a TreePath.

Parameters:
path - the path contaning the node.
Returns:
the casted node from the path.

setUI

protected void setUI(U ui)


Copyright © 2008-2010 CodeLutin. All Rights Reserved.