Package jaxx.runtime.swing.nav
Class NavHelper<M,U,B extends NavBridge<M,N>,N extends NavNode<M,N>>
java.lang.Object
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 java.lang.Object
Tree helper to deal with the build of trees and other usefull operations.
A helper acts as an handler for a
This listener will load node's childs before node expands if the node is not loaded.
See the
To listen modification of the model, it will mainly repopulate nodes when
required.
See the method
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.
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 methodcreateModel(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 methodsetUI(Object, boolean, TreeSelectionListener).
To obtain the tree, use method getUI().
Internal data provider
To populate childs nodes and render nodes, we use aNavDataProvider.
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
createModel(NavNode, Object...)insertNode(NavNode, NavNode)removeNode(NavNode)moveNode(NavNode, NavNode, int)refreshNode(NavNode, boolean)loadAllNodes(NavNode, NavDataProvider)
Model selection modification
Model query
Child loadors factory
The class offers a factory ofNavNodeChildLoador, use the method
getChildLoador(Class) to obtain the correct child loador given his type.- Since:
- 2.1
- Author:
- Tony Chemit - chemit@codelutin.com
- See Also:
NavNode,NavNodeChildLoador,AbstractNavTreeCellRenderer
-
Field Summary
Fields Modifier and Type Field Description protected static java.util.Set<? super NavNodeChildLoador<?,?,?,?,?>>childLoadorsCache of child loadors.protected NavDataProviderdataProviderThe shared data provider used to obtain datas to populate nodes and render them.protected javax.swing.event.TreeWillExpandListenerexpandListenerATreeWillExpandListenerused to listen when tree should expand.protected javax.swing.event.TreeSelectionListenerselectionListenerpour ouvrir les fils d'un noeud que l'on vient de sélectionner pour éviter d'avoir à faire des doubles clics.protected javax.swing.event.TreeModelListenertreeModelListenerpour 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
-
Method Summary
Modifier and Type Method Description abstract voidaddSelectionPath(javax.swing.tree.TreePath path)abstract voidaddSelectionPaths(javax.swing.tree.TreePath[] paths)protected booleancheckModel()Checks if internal model was created.protected abstract McreateModel(N node, java.lang.Object... extraArgs)Register a new root node.abstract voidexpandPath(javax.swing.tree.TreePath pathToExpand)NfindNode(N node, java.lang.String... ids)Finds a node from the given rootnode, applying the path given byids.protected BgetBridge()Obtains the bridge .NgetChild(N node, java.lang.String id)Finds a node from the given rootnode, and return child searchedstatic <L extends NavNodeChildLoador<?, ?, ?, ?, ?>>
LgetChildLoador(java.lang.Class<L> type)Obtains theNavNodeChildLoadorof the giventypefrom internal cache.protected static java.util.Set<? super NavNodeChildLoador<?,?,?,?,?>>getChildLoadors()protected NavDataProvidergetDataProvider()Obtains the attached data provider used to populate and render nodes.MgetModel()Obtains the model.protected NgetNode(javax.swing.tree.TreePath path)Convinient method to objet the casted node of aTreePath.NgetRootNode()java.lang.String[]getSelectedIds()Obtains the path of ids fro the root node to the selected node on the registred tree.abstract NgetSelectedNode()Obtains the selected node of the registred tree.abstract java.util.List<N>getSelectedNodes()Obtains the selected nodes of the registred tree.abstract javax.swing.tree.TreeSelectionModelgetSelectionModel()abstract AbstractNavTreeCellRenderer<M,N>getTreeCellRenderer()Obtains theAbstractNavTreeCellRendererrenderer of the registred tree.UgetUI()Obtains the ui associated with model in helper.voidinsertNode(N parentNode, N newNode)Inserts the given node to the givenparentNode.voidinsertNode(N parentNode, N newNode, int position)Inserts the given node to the givenparentNode.abstract booleanisExpanded(javax.swing.tree.TreePath pathToExpand)voidloadAllNodes(N node, NavDataProvider dataProvider)To load all nodes of a model.voidmoveNode(N parentNode, N node, int position)Moves the givennodeto the newposition.protected voidpopulateNode(N node, java.lang.Object[] children, boolean recurse)Populates nodes.voidrefreshNode(N node, boolean deep)Refreshs the givennode.NremoveNode(N node)Removes the givennodefrom the registred tree model and returns his parent.abstract voidremoveSelectionPath(javax.swing.tree.TreePath path)abstract voidremoveSelectionPaths(javax.swing.tree.TreePath[] paths)abstract voidscrollPathToVisible(javax.swing.tree.TreePath path)voidselectNode(java.lang.String... path)Selects the node described by his givenpathof ids.voidselectNode(N node)Selects the givennodein the registred tree.voidselectNodes(java.util.List<N> nodes)Selects the givennodesin the registred tree.voidselectParentNode()Selects the parent of the currently selected node.voidsetDataProvider(NavDataProvider dataProvider)Registers thedataProviderfor the helper.abstract voidsetSelectionPath(javax.swing.tree.TreePath path)protected voidsetUI(U ui)voidsetUI(U tree, boolean addExpandTreeListener)Registers the giventreefor this helper.voidsetUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, javax.swing.event.TreeSelectionListener listener)Registers the giventreefor this helper.abstract voidsetUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, javax.swing.event.TreeSelectionListener listener, javax.swing.event.TreeWillExpandListener willExpandListener)Registers the giventreefor this helper.voidsetUI(U tree, boolean addExpandTreeListener, javax.swing.event.TreeSelectionListener listener)Registers the giventreefor this helper.voidunSelectNode(N node)Unselects the givennodein the registred tree.voidunSelectNodes(java.util.List<N> nodes)Selects the givennodesin the registred tree.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
dataProvider
The shared data provider used to obtain datas to populate nodes and render them. -
expandListener
protected javax.swing.event.TreeWillExpandListener expandListenerATreeWillExpandListenerused to listen when tree should expand. If so, the listener will load selected node childs if required (says when theNavNode.isLoaded()is sets tofalse). -
selectionListener
protected javax.swing.event.TreeSelectionListener selectionListenerpour ouvrir les fils d'un noeud que l'on vient de sélectionner pour éviter d'avoir à faire des doubles clics. -
treeModelListener
protected javax.swing.event.TreeModelListener treeModelListenerpour recharger le rendu des noeuds (et charger les fils si nécessaires) lors d'une modification dans le modèle de l'arbre. -
childLoadors
Cache of child loadors.
-
-
Constructor Details
-
NavHelper
-
-
Method Details
-
scrollPathToVisible
public abstract void scrollPathToVisible(javax.swing.tree.TreePath path) -
setSelectionPath
public abstract void setSelectionPath(javax.swing.tree.TreePath path) -
addSelectionPath
public abstract void addSelectionPath(javax.swing.tree.TreePath path) -
addSelectionPaths
public abstract void addSelectionPaths(javax.swing.tree.TreePath[] paths) -
removeSelectionPath
public abstract void removeSelectionPath(javax.swing.tree.TreePath path) -
removeSelectionPaths
public abstract void removeSelectionPaths(javax.swing.tree.TreePath[] paths) -
getSelectionModel
public abstract javax.swing.tree.TreeSelectionModel getSelectionModel() -
isExpanded
public abstract boolean isExpanded(javax.swing.tree.TreePath pathToExpand) -
expandPath
public abstract void expandPath(javax.swing.tree.TreePath pathToExpand) -
createModel
Register a new root node. If internalgetModel()does not exists, creates a new one from his given rootnode, otherwise just set the new root on the existing model. Note: As a side-effect, the model will be keep in fieldgetModel()and thetreeModelListenerwill be registred on this model.- Parameters:
node- the root node of the new modelextraArgs- extra args to create initial model- Returns:
- the new model
-
getTreeCellRenderer
Obtains theAbstractNavTreeCellRendererrenderer 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
Obtains the selected node of the registred tree.- Returns:
- the selected tree or
nullif no registred tree nor selection empty.
-
getSelectedNodes
Obtains the selected nodes of the registred tree.- Returns:
- the selected tree or
nullif no registred tree nor selection empty.
-
setUI
public abstract void setUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, javax.swing.event.TreeSelectionListener listener, javax.swing.event.TreeWillExpandListener willExpandListener)Registers the giventreefor this helper. Note: as a side-effect, it will register (if required) theexpandListenerlistener and (if required) theselectionListener. Note : as a second side-effect, it will register the givenwillExpandListener(if not null) for the ui and do it BEFORE theexpandListener. The main idea here is to be able to block any expand (or collapse).- Parameters:
tree- the tree to registeraddExpandTreeListener- a flag to add expand listeneraddOneClickSelectionListener- a flag to expend when selectionlistener- the optional selection listener to addwillExpandListener- the optional will expand listener to add BEFORE the default expand tree listener (if he was required)- Since:
- 2.1.2
-
getChildLoadors
-
getChildLoador
public static <L extends NavNodeChildLoador<?, ?, ?, ?, ?>> L getChildLoador(java.lang.Class<L> type)Obtains theNavNodeChildLoadorof the giventypefrom 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
Obtains the attached data provider used to populate and render nodes.- Returns:
- the attached data provider
-
getModel
Obtains the model.- Returns:
- the internal tree model or
nullif none was created.
-
getBridge
Obtains the bridge .- Returns:
- the internal bridge used by helper.
-
getUI
Obtains the ui associated with model in helper.- Returns:
- the ui (or
nullif no ui attached)
-
getRootNode
-
getSelectedIds
public java.lang.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
Registers the giventreefor this helper. Note: as a side-effect, it will register (if required) theexpandListenerlistener and theselectionListener.- Parameters:
tree- the tree to registeraddExpandTreeListener- a flag to add expand listener
-
setUI
public void setUI(U tree, boolean addExpandTreeListener, javax.swing.event.TreeSelectionListener listener)Registers the giventreefor this helper. Note: as a side-effect, it will register (if required) theexpandListenerlistener and theselectionListener.- Parameters:
tree- the tree to registeraddExpandTreeListener- a flag to add expand listenerlistener- the optional selection listener to add
-
setUI
public void setUI(U tree, boolean addExpandTreeListener, boolean addOneClickSelectionListener, javax.swing.event.TreeSelectionListener listener)Registers the giventreefor this helper. Note: as a side-effect, it will register (if required) theexpandListenerlistener and (if required) theselectionListener.- Parameters:
tree- the tree to registeraddExpandTreeListener- a flag to add expand listeneraddOneClickSelectionListener- a flag to expend when selectionlistener- the optional selection listener to add
-
insertNode
Inserts the given node to the givenparentNode. 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
Inserts the given node to the givenparentNode. 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 insertposition- position of node is inserted
-
removeNode
Removes the givennodefrom the registred tree model and returns his parent.- Parameters:
node- the node to remove- Returns:
- the parent node of the removed node.
-
moveNode
Moves the givennodeto the newposition.- Parameters:
parentNode- the parent nodenode- the node to moveposition- the new position of the node
-
refreshNode
Refreshs the givennode. If flagdeepis set totrue, then it will refresh recursively children nodes. Note:As a side-effect, evvery node involved will becomedirty.- Parameters:
node- the node to refreshdeep- un flag pour activer la repainte de la descendance du noeud- See Also:
NavNode.isDirty()
-
selectParentNode
public void selectParentNode() throws java.lang.NullPointerExceptionSelects the parent of the currently selected node. Note: If selection is empty, then throws a NPE.- Throws:
java.lang.NullPointerException- if selection is empty
-
selectNode
Selects the givennodein the registred tree.- Parameters:
node- the node to select
-
selectNodes
Selects the givennodesin the registred tree.- Parameters:
nodes- the nodes to select
-
unSelectNode
Unselects the givennodein the registred tree.- Parameters:
node- the node to select
-
unSelectNodes
Selects the givennodesin the registred tree.- Parameters:
nodes- the nodes to select
-
selectNode
public void selectNode(java.lang.String... path)Selects the node described by his givenpathof ids.- Parameters:
path- the absolute path of ids from root node to node to select.
-
findNode
Finds a node from the given rootnode, applying the path given byids.- Parameters:
node- the starting nodeids- the path of ids to apply on the node.- Returns:
- the find node or
nullif no node matchs.
-
getChild
Finds a node from the given rootnode, and return child searched- Parameters:
node- the starting nodeid- id of searched child- Returns:
- the find node or
nullif no node matchs.
-
checkModel
protected boolean checkModel()Checks if internal model was created.- Returns:
trueif model was created,falseotherwise.
-
populateNode
Populates nodes. Ifnodeis notnull, then populate it. Ifchildrenis notnull, then populate them, moreover ifrecurseis set totruethen do a recurse refresh on children.- Parameters:
node- the parent node to populate (optional)children- the child nodes to populate (optional)recurse- flag sets totrueif should do recurse refresh on givenchildrennodes.
-
getNode
Convinient method to objet the casted node of aTreePath.- Parameters:
path- the path contaning the node.- Returns:
- the casted node from the path.
-
setUI
-