-
1. Re: node selection in a tree
slcon1 Aug 25, 2008 7:54 AM (in response to maxime.renaux)In Java you can use the TreeStateAdvisor class:
public class SomeBean implements TreeStateAdvisor{ public Boolean adviseNodeOpened(UITree tree){ Object data = tree.getRowData(); YourTreeModel treeModel = (YourTreeModel) data; if(...){ // open node return Boolean.TRUE; } else if(...){ // close node return Boolean.FALSE; } // do not change treestate return null; } public Boolean adviseNodeSelected(UITree tree){ Object data = tree.getRowData(); ... } }
<rich:tree ... stateAdvisor="someBean" >
-
2. Re: node selection in a tree
maxime.renaux Aug 25, 2008 9:15 AM (in response to maxime.renaux)Thanks a lot for the answer but I'm confused in using this method. I have read some topic about the adviseNodeSelected method and it says
"MethodBinding pointing at a method accepting an org.richfaces.component.UITree with return of java.lang.Boolean type. If returned value is: java.lang.Boolean. TRUE, a particular treeNode is selected; java.lang.Boolean.FALSE, a particular treeNode is unselected; null, a particular treeNode saves the current state"
Where do i specify the node that hade to be selected?
Thanks -
3. Re: node selection in a tree
whitty69 Aug 25, 2008 9:37 AM (in response to maxime.renaux)I do it during by setting a _selectedNode object on selection and then by doing the following on the adviseNodeSelected routine.
Maybe you could do something similar.
BTW: Sorry for lack of comments in the code. Not good I know.
I am sure there are better ways, but this works great in my app.public Boolean adviseNodeSelected(UITree tree) { if (!PostbackPhaseListener.isPostback()) { Object key = tree.getRowData(); IConfigNode treeRowKey = (IConfigNode) key; if (treeRowKey == null) { return Boolean.FALSE; } if (_selectedNode == null) { _selectedNode = treeRowKey; return Boolean.FALSE; } return treeRowKey.getId() == _selectedNode.getId(); } return null; }
FYI: I also keep a map of the node states for ensuring that they don't loose the states on some refreshing instances.public Boolean adviseNodeOpened(UITree tree) { if (!PostbackPhaseListener.isPostback()) { Object key = tree.getRowKey(); TreeRowKey<?> treeRowKey = (TreeRowKey<?>) key; if (treeRowKey == null) { return Boolean.FALSE; } return getListenerBean().isExpanded(treeRowKey.getPath()); } return null; } public ListenerBean getListenerBean() { if (_listenerBean == null) { _listenerBean = new ListenerBean(); } return _listenerBean; } public class ListenerBean implements org.richfaces.event.NodeExpandedListener { private Map<String, Boolean> _statesMap = new HashMap<String, Boolean>(); public void processExpansion(NodeExpandedEvent event) throws AbortProcessingException { Object source = event.getComponent(); if (source instanceof HtmlTree) { source = ((HtmlTree) source).getRowData(); } if (source instanceof HtmlTreeNode) { HtmlTreeNode n = (HtmlTreeNode) source; // assume it was opened if it was already in the map Object o = n.getUITree().getRowKey(); if (o != null) { TreeRowKey<?> key = (TreeRowKey<?>) o; String path = key.getPath(); if (_statesMap.containsKey(path)) { _statesMap.put(path, !_statesMap.get(path)); } else { _statesMap.put(path, Boolean.TRUE); } } } } public void setExpanded(String treeNodeid) { _statesMap.put(treeNodeid, Boolean.TRUE); } public Boolean isExpanded(String treeNodeId) { // assume it was opened if it was already in the map if (_statesMap.containsKey(treeNodeId)) { return _statesMap.get(treeNodeId); } else { return Boolean.FALSE; } } }