4 Replies Latest reply on Feb 26, 2014 11:07 AM by feedo123

    Dynamic drop down menu RF 4.0

    feedo123

      Hi I am creating a drop down menu dynamically, the menu items are database driven and I am able to see my drop downs and menu items in debugger. I am talking to the tune of 380 menu items within various sub groups.

       

      We are migrating from Panel menu to drop down menu.

       

      I  checked a few posts in this community to overcome and i have adopted the solution given in this link.

       

      dynamically creating dropDownMenu in Richfaces4

       

       

      the jsp rendering the page is

      <h:form id="form">

      <rich:toolbar height="26px" >

      <rich:dropDownMenu binding="#{dropdownMenuBean.ABC}" />

      <rich:dropDownMenu binding="#{dropdownMenuBean.DEF}" />

      ......

      </rich:toolbar>

      </h:form>

      </ui:composition>

       

      I have customised a drop down menu bean which iterates and creates the groups and menu items and attaches it to the tool bar.

       

      @ManagedBean

      @ViewScoped

      public class DropdownMenuBean {

       

       

          private UIDropDownMenu abc, def, ghi, jkl, mno;

          private FacesContext app;

          private UIToolbar .......

       

          public UIDropDownMenu getAbc() {

      return Abc;

          }

          public void setAbc(UIDropDownMenu abc) {

      this.abc = abc;

          }

      ...

      public DropdownMenuBean() {

      app = FacesContext.getCurrentInstance();

      menuBar = (UIToolbar) app.getApplication().createComponent(app,UIToolbar.COMPONENT_TYPE,"org.richfaces.ToolbarRenderer");

          }

      //this is called while we are logging in to create menus to avoid menu's being created multiple times

      public void createDropDownMenu() {

       

      menuBar = (UIToolbar) app.getApplication().createComponent(app,UIToolbar.COMPONENT_TYPE,"org.richfaces.ToolbarRenderer");

              abc= (UIDropDownMenu) app.getApplication().createComponent(app,UIDropDownMenu.COMPONENT_TYPE, "org.richfaces.DropDownMenuRenderer");

              label  = (HtmlOutputText) app.getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE); 

              label.setValue("some constant");

              abc.getFacets().put(UIDropDownMenu.Facets.label.name(), label);

              abc.setMode(Mode.ajax); 

              abc.setHideDelay(0);

              createMenu(abc, "same constant");  //this create menu creates the hierarchy, groups, menu items.

              menuBar.getChildren().add(abc);

                 }

       

        private void createMenu() {

      does stuff

          }

       

       

          private void createGroup() {

      creates groups

      UIMenuGroup group = (UIMenuGroup) app.getApplication().createComponent(app,UIMenuGroup.COMPONENT_TYPE, "org.richfaces.MenuGroupRenderer");

          }

       

       

          private void createItem(UIComponentBase parent, ......) {

      creates items

      menuItem = (UIMenuItem) app.getApplication().createComponent(app, UIMenuItem.COMPONENT_TYPE, "org.richfaces.MenuItemRenderer");

      addLink()..........

          }

       

       

          private void addLink(AbstractCommandLink comp, String action .....) {

      ..............

      if (action != null) {

      comp.setAction(FacesContext.getCurrentInstance().getApplication().

      createMethodBinding(action, new Class[0]));

      }

      ..............

          }

      I  can see in the debugger the tool bar has a drop down - multiple menugroups - with menu items but for some reason its not rendering in the ui.


      Pls help. (alternatives if i setup the component in the getter / setter of the dropdown, the dropdown with menu groups and items is being rendered. but this is not a solution for me.)

        • 1. Re: Dynamic drop down menu RF 4.0
          bleathem

          I'd start with a PoC (proof-of-concept) using a facelet file and the JST c:forEach tags.  When you get that working, try implementing it in code as you have shown above.  This will help you to make sure you get the component hierarchy correct.

          • 2. Re: Dynamic drop down menu RF 4.0
            feedo123

            Hi Brian

            The drop down is dynamic purely database driven and subject to constant changes and is as big as nearly 400 menu items. hence having logic in jsp/xhtml is not an option.  We are using JSF, RF4, Spring, Oracle 12g etc. I have worked on an example and using spring DI i am trying to load the menu. Following is the code. I am calling the createMenu when the user logs into the app where i am setting the menu. (doesnt work). If I call the createMenu() in the constructor of the DropDownMenu it works. (but this is not an ideal scenario in my case). Any ideas to make this application work to display the DD menu. Debugging shows it has toolbar -> dropdownmenu -> MenuItem etc for some reason from the jsp/xhtml using EL it is unable to display the dd menu. pls suggest

             

            dropDown.jsp

             

            <h:form id="form"> 

                     <rich:toolbar height="26px" > 

                            <rich:dropDownMenu execute="@this" binding="#{dropDownMenuBean.dropDownMenu}" />

                           

                        </rich:toolbar>

                </h:form>

             

             

            DropMenuMenuBean.java

            @ManagedBean(name = "dropMenuBean", eager=true)

            @SessionScoped

            public class DropMenuBean implements Serializable{ 

               

                private static final long serialVersionUID = 1L;

                //private String current; 

                private UIToolbar menuBar; 

                private  UIDropDownMenu dropMenu;

             

             

                public UIDropDownMenu getDropMenu() {

                    return dropMenu;

                }

             

             

                public void setDropMenu(UIDropDownMenu dropMenu) {

                    this.dropMenu = dropMenu;

                }

                private HtmlOutputText label;

                private FacesContext ctx;

                private UIMenuItem menItm;

             

             

                public DropMenuBean(){

                   //createMenu();

                }

               

              

             

             

                public void createMenu( ) { 

                   

                    ctx = FacesContext.getCurrentInstance(); 

                    menuBar = (UIToolbar) ctx.getApplication().createComponent(ctx, UIToolbar.COMPONENT_TYPE, "org.richfaces.ToolbarRenderer"); 

                    dropMenu = (UIDropDownMenu) ctx.getApplication().createComponent(ctx, UIDropDownMenu.COMPONENT_TYPE, "org.richfaces.DropDownMenuRenderer"); 

                    label = (HtmlOutputText) ctx.getApplication().createComponent(HtmlOutputText.COMPONENT_TYPE); 

                    label.setValue("File");

                    dropMenu.getFacets().put(UIDropDownMenu.Facets.label.name(), label); 

                    dropMenu.setMode(Mode.ajax); 

                    createItem(dropMenu);

                    menuBar.getChildren().add(dropMenu);

                } 

               

                public void createItem(UIDropDownMenu drop){

                     menItm = (UIMenuItem) ctx.getApplication().createComponent(ctx, UIMenuItem.COMPONENT_TYPE, "org.richfaces.MenuItemRenderer"); 

                    menItm.setLabel("New");

                    drop.getChildren().add(menItm);

                }

               

                public void setMenuBar(UIToolbar menuBar) { 

                    this.menuBar = menuBar; 

                } 

               

                public UIToolbar getMenuBar(){

                    return this.menuBar;

                }

              

            }

            • 3. Re: Dynamic drop down menu RF 4.0
              bleathem

              I'd have to play with it to see.  Can you file a jira with some working sample code we can use to investigate?

              • 4. Re: Dynamic drop down menu RF 4.0
                feedo123
                Solved.

                 

                 

                The problem was with the view side in the xhtml. Even though the java is building the drop downs correctly it was unable to render as it was unable to find a reference of menu group and menu item in the xhtml. The issue was corrected as follows

                 

                 

                the jsp rendering the page is

                <h:form id="form">

                <rich:toolbar width="100px" height="22px" id="menuToolbar"  >

                            <rich:dropDownMenu id="menu1"   binding="#{bean.createMenu1}" />

                                <rich:menuGroup id="mg1" rendered="false" />

                                <rich:menuItem id="mi1" rendered="false" />

                        </rich:toolbar>

                 

                 

                </h:form>

                </ui:composition>