views:

1215

answers:

1

I've finally managed to receive events if the user expands a node of my client side handled tree using the following method:

public void processExpansion(NodeExpandedEvent event) throws AbortProcessingException {
 if (event != null && event.getSource() != null && event.getSource() instanceof HtmlTree) {
  this.expandedNodes.add(((UITree) event.getSource()).getRowData());
 }
}

I had to use #getRowData() because of the recursiveTreeNodesAdaptor.

How can I get notification of nodes the user collapses again? I could not find an appropriate listener.

A: 

You could use ChangeExpandListener

The <rich:changeExpandListener> represents an action listener method that is notified on an expand/collapse event on the node.

EDIT:

First changeExpandListener has to be delared as attribute for <rich:treeNode>, if is declared for entired tree works as you write (event is processed after selection event). So:

<rich:tree value="#{simpleTreeBean.treeNode}" var="item" >
    <rich:treeNode changeExpandListener="#{simpleTreeBean.processExpansion}">
         <h:outputText value="#{item}" />
    </rich:treeNode>                   
</rich:tree>

processExpansion method must take NodeExpandedEvent as a parameter but there is no need to implement interface org.richfaces.event.NodeExpandedListener.

public void processExpansion(NodeExpandedEvent evt) {

  Object source = evt.getSource();
  if (source instanceof HtmlTreeNode) {
    UITree tree = ((HtmlTreeNode) source).getUITree();
    if (tree == null) {
      return;
    }
    // get the row key i.e. id of the given node.
    Object rowKey = tree.getRowKey();
    // get the model node of this node.
    TreeRowKey key = (TreeRowKey) tree.getRowKey();

    TreeState state = (TreeState) tree.getComponentState();
    if (state.isExpanded(key)) {
      System.out.println(rowKey + " - expanded");
    } else {
      System.out.println(rowKey + " - collapsed");
    }
  }
}

That's should help

cetnar
How can I distinguish collapse from expand?I use client side collapsing and the event listener is triggered upon submit of the tree - for example when changing the selection. I get one event per expanded node at this point... could not see any for collapsed nodes so far.
Daniel Bleisteiner
Thanks! That will help.
Daniel Bleisteiner