7 Replies Latest reply on Mar 6, 2006 7:22 AM by 807575

    Dynamic Generation of Tree component contents.

    807575
      I would really like to be able to dynamically generate a Tree component.

      I have generated it by 'hand' in the jsCreator but do not know what I should do to generate the same results programatically.

      A sample of what I built is as follows
              <ui:tree binding="#{MenuBarFragment.menuBarTree}" clientSide="true" id="menuBarTree" style="height: 100%; left: 0px; top: 0px; position: absolute; text-align: center">
                  <ui:treeNode binding="#{MenuBarFragment.menuBarMyDetailsNode}" id="menuBarMyDetailsNode" style="text-align: left" text="My Details">
                      <f:facet name="image">
                          <ui:image binding="#{MenuBarFragment.image1}" icon="TREE_FOLDER" id="image1"/>
                      </f:facet>
                      <ui:treeNode action="#{MenuBarFragment.myDetailsViewAction}" binding="#{MenuBarFragment.menuBarMyDetailsViewNode}" id="menuBarMyDetailsViewNode"
                          style="text-align: left" text="View">
                          <f:facet name="image">
                              <ui:image binding="#{MenuBarFragment.image2}" icon="TREE_DOCUMENT" id="image2"/>
                          </f:facet>
                      </ui:treeNode>
                      <ui:treeNode action="#{MenuBarFragment.myDetailsUpdateAction}" binding="#{MenuBarFragment.menuBarMyDetailsUpdateNode}"
                          id="menuBarMyDetailsUpdateNode" style="text-align: left" text="Update">
                          <f:facet name="image">
                              <ui:image binding="#{MenuBarFragment.image3}" icon="TREE_DOCUMENT" id="image3"/>
                          </f:facet>
                      </ui:treeNode>
                  </ui:treeNode>
                  <ui:treeNode binding="#{MenuBarFragment.menuBarUsersNode}" id="menuBarUsersNode" style="text-align: left" text="Users">
                      <f:facet name="image">
                          <ui:image binding="#{MenuBarFragment.image4}" icon="TREE_FOLDER" id="image4"/>
                      </f:facet>
                      <ui:treeNode binding="#{MenuBarFragment.menuBarUsersViewNode}" id="menuBarUsersViewNode" style="text-align: left" text="View">
                          <f:facet name="image">
                              <ui:image binding="#{MenuBarFragment.image5}" icon="TREE_DOCUMENT" id="image5"/>
                          </f:facet>
                      </ui:treeNode>
                      <ui:treeNode binding="#{MenuBarFragment.menuBarUsersUpdateNode}" id="menuBarUsersUpdateNode" style="text-align: left" text="Update">
                          <f:facet name="image">
                              <ui:image binding="#{MenuBarFragment.image6}" icon="TREE_DOCUMENT" id="image6"/>
                          </f:facet>
                      </ui:treeNode>
                  </ui:treeNode>
                  <ui:treeNode action="#{MenuBarFragment.logoutAction}" binding="#{MenuBarFragment.menuBarLogoutNode}" id="menuBarLogoutNode" style="text-align: left" text="Logout">
                      <f:facet name="image">
                          <ui:image binding="#{MenuBarFragment.image11}" icon="TREE_DOCUMENT" id="image11"/>
                      </f:facet>
                  </ui:treeNode>
              </ui:tree>
      I figure the code would involve something similar to
      Tree menuTree = new Tree();
      TreeNode newNode = new TreeNode();
      newNode.setText("MainMenu");
      newNode.setId("MainMenu_"+idMainMenu);
      newNode.setAction(???action??);
      .. what else do I put here? ....
      menuTree.getChildren().add(newNode);
      Once I figure out how to correctly populate a given node, I think I can build the entire tree.

      Any assistance greatly appreciated.

      ...Lyall
        • 1. Re: Dynamic Generation of Tree component contents.
          807575
          hi,
          i am using such a method. i hope helps you


          public TreeNode treeNodeEkle(TreeNode tn ,String id,String url,String text, String dokkod,int uzanti, String detaylar) {
          TreeNode node = new TreeNode();
          ImageComponent nodeimage = new ImageComponent();

          if(uzanti==1)
          nodeimage.setUrl("resources\\pdf.gif");
          else if(uzanti==2)
          nodeimage.setUrl("resources\\word.gif");
          else
          nodeimage.setUrl("resources\\excel.gif");

          Hyperlink nodelink = new Hyperlink();
          nodelink.setUrlLang(detaylar);
          nodelink.setText(replace(text,"-", " "));

          Class[] signatures = {ActionEvent.class};
          MethodBinding mb1 = FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{Page1.processTreeNodeAction}", signatures);
          nodelink.setActionListener(mb1);

          nodelink.setUrlLang(detaylar);
          node.setId(id);
          nodelink.setToolTip(url);

          node.setUrl(dokkod);

          node.getFacets().put(node.IMAGE_FACET_KEY, nodeimage);
          node.getFacets().put(node.CONTENT_FACET_KEY, nodelink);
          node.setExpanded(true);
          tn.getChildren().add(node);

          return node;
          }


          public void processTreeNodeAction(ActionEvent ae) {
          try {
          Hyperlink currentlink = (Hyperlink) ae.getSource();
          textArea1.setText(currentlink.getToolTip());
          }catch(Exception ex) {
          }
          }
          • 2. Re: Dynamic Generation of Tree component contents.
            807575
            Thanks for that, the method binding bit helps a bit.

            What I am looking for is the ability to generate my menu tree from a database menu setup rather than having it hard coded in the jsp.

            To that end, known what I have to do to recreate that menu would be really nice, including where to find the icons, etc.

            I will continue playing in hope of more enlightenment.

            ...Lyall
            • 3. Re: Dynamic Generation of Tree component contents.
              807575
              Nobody can elaborate?
              Ideal answer would be a block of java code that duplicates the jsf xml above.

              ...Lyall
              • 4. Re: Dynamic Generation of Tree component contents.
                807575
                Why do I get always get an exception like this:

                com.sun.rave.web.ui.appbase.ApplicationException: #{Page1.treeNodeTest_action}: javax.faces.el.MethodNotFoundException: treeNodeTest_action

                Here is part of my code:

                Class[] signatures = {ActionEvent.class};
                MethodBinding mb1 = FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{Page1 .treeNodeTest_action}", signatures);
                nodelink.setAction(mb1);

                // here's the method im calling within the same class
                public String treeNodeTest_action() {
                // TODO: Replace with your code
                System.out.println("SUCCESS!!");
                return null;
                }

                Am I missing something?
                • 5. Re: Dynamic Generation of Tree component contents.
                  807575
                  Well, I tried this...
                          Class[] classSignatures = {ActionEvent.class};
                          MethodBinding methodBinding3 = FacesContext.getCurrentInstance().getApplication().createMethodBinding("#{Page1.menuTreeNode3_action}", classSignatures);
                  
                          TreeNode newNode = new TreeNode();
                          
                          newNode.setAction(methodBinding3);
                          newNode.setText("Manually added Node");
                          newNode.setId("MainMenu_"+(childCount+1));
                  
                          menuTree.getChildren().add(newNode);
                  with action methods like this...
                      public String menuTreeNode3_action(java.awt.event.ActionEvent ae) {
                          // TODO: Replace with your code
                          System.out.println("Here I am in the tree node3 action event");
                          return null;
                      }
                  which results in (from server.log)
                  [#|2006-03-04T16:04:03.334+1030|SEVERE|sun-appserver-pe8.1_02|javax.enterprise.system.container.web|_ThreadID=36;|StandardWrapperValve[Faces Servlet]: Servlet.service() for servlet Faces Servlet threw exception
                  com.sun.rave.web.ui.appbase.ApplicationException: wrong number of arguments
                       at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.destroy(ViewHandlerImpl.java:601)
                       at com.sun.rave.web.ui.appbase.faces.ViewHandlerImpl.renderView(ViewHandlerImpl.java:302)
                       at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
                       at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:221)
                  ...

                  Caused by: java.lang.IllegalArgumentException: wrong number of arguments
                       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)



                  Any suggestions?
                  • 6. Re: Dynamic Generation of Tree component contents.
                    807575
                    The tutorial on the tree component includes a section which describes how to add nodes dynamically: http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/sitemaptree.html#08

                    There is also a complete example of working with trees dynamically, in the sample applications section of the developer site: http://developers.sun.com/prodtech/javatools/jscreator/reference/code/sampleapps/index.html

                    // Gregory
                    • 7. Re: Dynamic Generation of Tree component contents.
                      807575
                      Thanks for replying.

                      Regarding the tutorial, it has no examples of attaching an action, it only demonstrates attaching a link.

                      The Dynamic Tree example application, now that looks useful.

                      Thanks :-)

                      ...Lyall