13 Replies Latest reply on Apr 8, 2009 10:40 AM by elf

    create tree programmatically

    elf

      http://livedemo.exadel.com/richfaces-demo/richfaces/tree.jsf?tab=model&cid=3217852

      Hi
      According to this live demo I tried to create rich tree programmatically, but I do not know what should I use instead of

      nodeSelectListener="#{simpleTreeBean.processSelection}"


      Please help me, how I can programmatically assign nodeSelectListener to tree.
      Thanks in advance.

        • 1. Re: create tree programmatically
          nbelaevski

          Hello,

          You can find all necessary information in this class: org.richfaces.taglib.TreeListenersTagHandler

          • 2. Re: create tree programmatically
            elf

            Could you please provide me simple example how to use this class.
            I can not find sample of using :(

            • 3. Re: create tree programmatically
              nbelaevski

              It's easy. applyMetadata() method does exactly what you have to do creating component programmatically:

              return new Metadata() {
               public void applyMetadata(FaceletContext context, Object object) {
               MethodBinding binding = context.getFacesContext().getApplication().createMethodBinding(attribute.getValue(), new Class[] {NodeSelectedEvent.class});
               ((UIComponent) object).getAttributes().put(NODE_SELECT_LISTENER, binding);
               }
               };


              • 4. Re: create tree programmatically
                elf

                I am so sorry. How can I use your example with my code. Please help me.

                HtmlTree tree = new HtmlTree();
                tree.setId("MenuTreeId");
                tree.setSwitchType("client");
                tree.setStyle("min-height:100%; min-width:100%; width:100%");
                tree.setReRender("selectedNode");
                tree.setValueExpression("value", eF.createValueExpression(eC, "#{treebean.treeNode}", TreeNode.class) );
                


                • 5. Re: create tree programmatically
                  nbelaevski

                  No problem. Try this:

                  tree.getAttributes().put("nodeSelectListener", context.getFacesContext().getApplication().createMethodBinding("#{bean.method}", new Class[] {NodeSelectedEvent.class}));


                  • 6. Re: create tree programmatically
                    elf

                    Please help me. What is wrong in my code:

                     tree.getAttributes().put("nodeSelectListener",
                     eF.createMethodExpression(eC, "#{treebean.processSelection}",
                     null, new Class[] {NodeSelectedEvent.class}));
                    

                    and
                     public void processSelection(NodeSelectedEvent event){
                    

                    I have java.lang.reflect.InvocationTargetException: argument type mismatch.
                    What is the correct way to use this?
                    Thanks in advance.

                    • 7. Re: create tree programmatically
                      nbelaevski

                      "nodeSelectListener" is of type MethodBinding and you use MethodExpression. Either switch to MethodBinding or create adaptor to adapt MethodExpression to FacesListener (see examples in JSF code) and use it.

                      • 8. Re: create tree programmatically
                        elf

                        It looks like work with

                        FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{"treebean.processSelection}", new Class[] {NodeSelectedEvent.class})
                        

                        but FacesContext.getCurrentInstance().getApplication().createMethodBinding( is deprecated for my environment. Is it correct to use this code?

                        • 9. Re: create tree programmatically
                          nbelaevski

                           

                          "nbelaevski" wrote:
                          "... or create adaptor to adapt MethodExpression to FacesListener (see examples in JSF code) and use it.
                          MethodBidning is deprecated, but left for back compatibility. You can use JSF 1.2 approach described here, so that you won't need MethodBinding.

                          • 10. Re: create tree programmatically
                            elf

                            Could you please post simple example here how I can do it.

                            • 11. Re: create tree programmatically
                              nbelaevski

                              Take a look at sources of javax.faces.event.MethodExpressionActionListener. Just replace Action* stuff with NodeSelect* stuff. The add created listener using addNodeSelectListener()

                              • 12. Re: create tree programmatically
                                elf

                                It works.
                                Thank you

                                • 13. Re: create tree programmatically
                                  elf

                                  Is there way to put selection on particular node when tree is loading (loadTree method in livedemo http://livedemo.exadel.com/richfaces-demo/richfaces/tree.jsf?tab=model&cid=3587377) ?
                                  May be I should bind my tree to request bean or something else ?