7 Replies Latest reply on Jul 27, 2009 3:15 PM by meghiddo

    little help on nodeSelectListener for different treeNodes

      I have a tree that has projects, devices, and channels. I have two beans for this tree, one called DeviceChannels that holds each device and list of its channels, with these variables:

      private String deviceName;
      private String deviceId;
      private String channelId;
      List channelsList = new ArrayList();

      Then I have ProjectHolder class to hold the name of each projects, and a List of this DeviceChannels class. Here are the variables:

      private String projectName;
      private String projectId;
      List deviceAndChannels = new ArrayList();


      In this way I am able to bind only the ProjectHolder class to the rich:tree, but since it has the List of DeviceChannels Objects I am able to get access to both Objects and only having to bind ProjectHolder to the rich:tree.

      I am working on nodeSelectListener methods, but I have trouble with clicking any node other than the top project name node. Clicking that node I can have my code do whatever I need, such as pulling the IDs from that project node and the device cnd channel nodes beneath it.

      But if I try to click on the second or third ndoes, I get these types of errors:

      nodeSelectListener="#{treeClickBean.projectSelection}": java.lang.ClassCastException: nodesContainer.DeviceChannels cannot be cast to nodesContainer.TreeHolder

      Here is the tree as it is now and the projectSelection method that nodeSelectListener calls:




      <rich:tree nodeSelectListener="#{treeClickBean.projectSelection}" ajaxSubmitSelection="true" switchType="client"
       var="item" ajaxKeys="#{null}" reRender="projectForm">
       <rich:treeNodesAdaptor nodes="#{testNodesBean.projects}" var="projects">
       <rich:treeNode>
       <h:outputLink value="/NI/explorer_click/project.jsf">
       <h:outputText value="#{projects.projectName}" />
       </h:outputLink>
       </rich:treeNode>
       <rich:recursiveTreeNodesAdaptor roots="#{projects.deviceAndChannels}" var="device">
       <rich:treeNode>
       <h:outputLink value="/NI/explorer_click/device.jsf">
       <h:outputText value="#{device.deviceName}" />
       </h:outputLink>
       </rich:treeNode>
       <rich:recursiveTreeNodesAdaptor roots="#{device.channelsList}" var="channels">
       <rich:treeNode>
       <h:outputText value="#{channels}" />
       </rich:treeNode>
       </rich:recursiveTreeNodesAdaptor>
       </rich:recursiveTreeNodesAdaptor>
       </rich:treeNodesAdaptor>
       </rich:tree>
      
      
      
      
      
      
      
      
      
      
      public void projectSelection(NodeSelectedEvent event){
       HtmlTree tree = (HtmlTree) event.getComponent();
      
       projectNode = (TreeHolder) tree.getRowData();
       projectId=projectNode.getProjectId();
       projectName=projectNode.getProjectName();
      
       deviceChannelsObject=projectNode.getDeviceAndChannels().get(projectNode.getDeviceindex());
       deviceId=deviceChannelsObject.getDeviceId();
       deviceName=deviceChannelsObject.getDeviceName();
      
      
       }




      As you can see from the java code I can get the data from the child nodes just by clicking on the parent node, but I am having problems adjusting my java code to having the child nodes clicked on.

      Should I make a separate method from projectSelection() or should I handle it in that same class?

      Do I need to put separate nodeSelectListener attributes in the tree? Right now I just have one because calling that gives me access to all the data I need.

      It seems like this should be handled in the java code there, because when I try to add a different nodeSelectListener to the device treeNode tag, it still gives me the same error from above.

      I just need a little guidance on how to handle this.

        • 1. Re: little help on nodeSelectListener for different treeNode

          Someone just tell me this..

          Do I need to add another nodeSelectListener attribute that calls some other function to handle this on the rich:treeNode tag for device?

          Or since projectSelection already gives me access to the data, is there a way to tell it which node is being selected in the case that it isnt the root node.

          • 2. Re: little help on nodeSelectListener for different treeNode
            nbelaevski

            Hi,

            You can use instanceof operator:

            if (object instanceof type) {
             type new_name = (type) object;
            
             }


            • 3. Re: little help on nodeSelectListener for different treeNode

              Can you possibly clear it up a bit more? I have been trying variations on your advice, but havent seemed to get it right yet. I have till the end of the day to get this working so Im a little worried.

              Should I leave the method as it is, accepting NodeSelectedEvent event as the parameters, or should that be changed to something more general?

              public void projectSelection(NodeSelectedEvent event) {
              }


              it seems to me you are saying I only need the one nodeSelectListener attribute on my tree right? And it is going to call this method.

              I just need if statements covering whether the node selected is a TreeHolder Object or a DeviceChannels Object.

              So it seems like your saying I should do something like:

              public void projectSelection(NodeSelectedEvent event){
              
               if (projectNode instanceof NodeSelectedEvent){
               HtmlTree tree = (HtmlTree) event.getComponent();
               projectNode = (MeasurementProject) tree.getRowData();
               projectId=projectNode.getProjectId();
               projectName=projectNode.getName();
               }
              
               if (deviceNode instanceof NodeSelectedEvent){
               ...
               }
              
               }


              where projectNode is a TreeHolder Object instance and deviceNode is of DeviceChannels Object

              the above gives me errors though (Incompatible conditional operand types MeasurementProject and NodeSelectedEvent)

              and the other variations I have tried havent quite worked yet either.

              I would keep trying until I get it, but like I said I have till tonight to get this working correctly.

              • 4. Re: little help on nodeSelectListener for different treeNode

                Still no luck. Still trying, almost out of time though :(

                • 5. Re: little help on nodeSelectListener for different treeNode

                  Ill take one more chance
                  anyone know what I need to do to adjust my code to nbevalaeski's pseudo code?
                  I just cant seem to get it.

                  • 6. Re: little help on nodeSelectListener for different treeNode
                    nbelaevski

                    MeasurementProject is definitely not an instance of NodeSelectedEvent, they have no common ancestor; and NodeSelectedEvent is class, not interface.

                    • 7. Re: little help on nodeSelectListener for different treeNode

                      eh its too late now anyhow, my demo is in less than 2hrs and even if I did know exactly what to put in that if ()statement I doubt Id have time to implement it before then