2 Replies Latest reply on Jul 21, 2009 5:28 AM by Matt Kohanek

    NodeSelectListener issues (please help)

    Matt Kohanek Novice

      Alright, I have a tree that display a list of projects, and under each project a list of devices that belong to it. I need to have things change when a user clicks on a device or on a project. I have it to the point where if a user clicks on a project, it will do what I want. But when a user clicks on a device under the project I get this error:

      WARNING: /disposable/test.xhtml @47,99 nodeSelectListener="#{treeClickBean.projectSelection}": java.lang.ClassCastException: java.lang.String cannot be cast to nodesContainer.ProjectHolder

      I have a List of Objects bound to my rich:tree (the Object is called ProjectHolder). So for my rich tree I did this for the nodeSelectListener:

      <rich:tree nodeSelectListener="#{treeClickBean.projectSelection}" reRender="selectedNode"
       switchType="client" ajaxSubmitSelection="true" ajaxKeys="#{null}">
       <rich:treeNodesAdaptor nodes="#{nodesBean.projects}" var="project">
       <rich:treeNode>
       <h:outputText value="#{project.projectName}" />
       </rich:treeNode>
       <rich:recursiveTreeNodesAdaptor roots="#{project.devicesList}" var="device">
       <rich:treeNode>
       <h:outputText value="#{device}" />
       </rich:treeNode>
       </rich:recursiveTreeNodesAdaptor>
       </rich:treeNodesAdaptor>
       </rich:tree>


      This calls the following method:

      public void projectSelection(NodeSelectedEvent event){
       HtmlTree tree = (HtmlTree) event.getComponent();
       node = (ProjectHolder) tree.getRowData();
       name=node.getProjectId();
       }


      And then I have a h:outputText to display the value of that name variable. So when a user clicks on the project, the projectId is displayed as intended.

      But my problem is with when a user clicks on the device. If I do it as is, I get the error above saying cannot convert from String to ProjectHolder Object.

      I tried adding a nodeSelectListener attribute to the device treeNode, that uses String values instead of the ProjectHolder Object but it does the same. I would like to find a way to just manipulate that java code, and leave the tree as is, with only the one nodeSelectListener attribute in the first rich:tree tag.

      Oh and one other thing, this part of the tree:
      <rich:recursiveTreeNodesAdaptor roots="#{project.devicesList}" var="device">
       <rich:treeNode>
       <h:outputText value="#{device}" />
       </rich:treeNode>
       </rich:recursiveTreeNodesAdaptor>


      is actually referring to a List of devices, not just a String with one device.

      So Im sure there is a way to manipulate that projectSelection() method above where it can tell the difference between clicking on the project name or one of the devices from the List right? I was looking at stuff like (this is incomplete btw):

      selectedNodeChildren.clear();
       TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
       if (currentNode.isLeaf()){
       selectedNodeChildren.add((String)currentNode.getData());
       }else
       {
       Iterator<Map.Entry<Object, TreeNode>> it = currentNode.getChildren();
       while (it!=null &&it.hasNext()) {
       Map.Entry<Object, TreeNode> entry = it.next();
       selectedNodeChildren.add(entry.getValue().getData().toString());
       }
       }

      to add to the projectSelection method, but this has givem me no luck as of yet.

      Can anyone please give me some guidance on this one?