2 Replies Latest reply on Apr 18, 2010 12:46 AM by sam_pc1611

    List dropDownMenu

    sam_pc1611

      hi all, i want display ddMenu with a list consulting a data base, but i can´t , i only have one menu with manyyyy submenu´s, but i want many menusss with many submenussss.   

       

      this is my code:

      in java:

      private HtmlDropDownMenu master;

       

          public HtmlDropDownMenu getMaster() {

              FacesContext facesContext = FacesContext.getCurrentInstance();

              master = new HtmlDropDownMenu();

              master.setValue("Busqueda");   

              master.setSubmitMode("ajax");

              master.setId("menu");

              master.setDirection("bottom-right");

              master.setJointPoint("tr");

       

              HtmlMenuItem htmlMenuItem = new HtmlMenuItem();

              htmlMenuItem.setValue("Busqueda de clientes");

              htmlMenuItem.setActionExpression(facesContext.getApplication().getExpressionFactory().createMethodExpression(facesContext.getELContext(), "#{ActionBean.CLIENTSEARCH}", String.class, new Class<?>[0]));

              htmlMenuItem.setReRender("jspPage");

       

              HtmlMenuItem htmlMenuItem2 = new HtmlMenuItem();

              htmlMenuItem2.setValue("Busqueda de prospectos");

              htmlMenuItem2.setActionExpression(facesContext.getApplication().getExpressionFactory().createMethodExpression(facesContext.getELContext(), "#{ActionBean.PROSPECTSEARCH}", String.class, new Class<?>[0]));

              htmlMenuItem2.setReRender("jspPage");

       

       

              master.getChildren().add(htmlMenuItem);

              master.getChildren().add(htmlMenuItem2);

       

      htmlMenuItem3.setActionExpression(facesContext.getApplication().getExpressionFactory().createMethodExpression(facesContext.getELContext(), "#{ActionBean.ORGANIZER}", String.class, new Class<?>[0]));

       

       

              return master;

          }

       

          /**

           * @param master the master to set

           */

          public void setMaster(HtmlDropDownMenu master) {

              this.master = master;

          }

       

      in jsp:

        <h:panelGrid width="100%">
                    <rich:dropDownMenu binding="#{NavigationMenuRich.master}" ></rich:dropDownMenu>
        </h:panelGrid>

       

      ddMenu.JPG

       

      somebody knows how create a dropDownMenu with many menus (lol) from java.

       

       

      thxx

        • 1. Re: List dropDownMenu
          akaine

          In my last project I used this method to bind the front with the dynamic menu where catProfile is a logging in user with the necessary info to get the menu structure from the database. In this particular case I used EJB3 so I could rely on the object introspection. Still the same effect can be acheived by using lists manually.

           

          @EJB private RelPerfilmenuitemFacadeLocal relPerfilmenuitemFL;
          private HtmlToolBar setToolbarByPerfil(CatProfile catProfile) {
              HtmlToolBar toolbar = new HtmlToolBar();
              
              List<RelPerfilmenuitem> relPerfilMenuitems = relPerfilmenuitemFL.findMenuByProfile(catProfile);
          
              List<CatMenuseccion> catSecciones = new ArrayList<CatMenuseccion>(0);
              List<CatMenuitem> catMenuitems = new ArrayList<CatMenuitem>(0);
              
              Hashtable<Integer,CatMenuseccion> secciones = new Hashtable<Integer,CatMenuseccion>();
              for(RelPerfilmenuitem relPerfilMenuitem : relPerfilMenuitems){
                  secciones.put(relPerfilMenuitem.getCatMenuitem().getCatMenuseccion().getIdcatmenuseccion(),relPerfilMenuitem.getCatMenuitem().getCatMenuseccion());
                  catMenuitems.add(relPerfilMenuitem.getCatMenuitem());
              }
              catSecciones = new ArrayList<CatMenuseccion>(secciones.values());
              
              int[] seccionIds = new int[catMenuitems.size()];
              for(int i=0; i<catMenuitems.size(); i++){
                  seccionIds[i] = catMenuitems.get(i).getCatMenuseccion().getIdcatmenuseccion();
                  catMenuitems.get(i).setCatMenuseccion(null);
              }
              
              for(int i=0; i<catSecciones.size(); i++){
                  catSecciones.get(i).setCatMenuitems(new ArrayList<CatMenuitem>(0));
                  for(int j=0; j<seccionIds.length; j++){
                      if(catSecciones.get(i).getIdcatmenuseccion().equals(seccionIds[j])){
                          catSecciones.get(i).getCatMenuitems().add(catMenuitems.get(j));
                      }
                  }
              }
              
              for(CatMenuseccion catSeccion : reorderSecciones(catSecciones)){
                  HtmlDropDownMenu menuComponent = new HtmlDropDownMenu();
                  
                  HtmlGraphicImage sectionIcon = new HtmlGraphicImage();
                  sectionIcon.setUrl("resource:///resources/images/mmenu/"+catSeccion.getIcono()+".png");
                  sectionIcon.setStyleClass("ddmIcon");
                  
                  HtmlOutputText sectionLabel = new HtmlOutputText();
                  sectionLabel.setValue(catSeccion.getDescripcion());
                  
                  HtmlPanelGroup htmlPanelGroup = new HtmlPanelGroup();
                  htmlPanelGroup.getChildren().add(sectionIcon);
                  htmlPanelGroup.getChildren().add(sectionLabel);
                  
                  menuComponent.getFacets().put("label",htmlPanelGroup);
                  
                  for(CatMenuitem catMenuitem : reorderMenuitems(catSeccion.getCatMenuitems())){
                      HtmlMenuItem htmlMenuItem = new HtmlMenuItem();
                      htmlMenuItem.setValue(catMenuitem.getDescripcion());
                      htmlMenuItem.setIcon("resource:///resources/images/mmenu/"+catMenuitem.getIcono()+".png");
                      htmlMenuItem.setSubmitMode("ajax");
                      htmlMenuItem.setDisabled(!catMenuitem.getHabilitado());
                      
                      Class<?>[] params = {};
                      FacesContext fc = FacesContext.getCurrentInstance();
                      Application app = fc.getApplication();
                      MethodExpression me = app.getExpressionFactory().createMethodExpression(fc.getELContext(),"#{"+catMenuitem.getAccion()+"}",String.class,params);
                      htmlMenuItem.setActionExpression(me);
                      menuComponent.getChildren().add(htmlMenuItem);
                  }
                  toolbar.getChildren().add(menuComponent);
              }
              return toolbar;
          }
          
          
          @SuppressWarnings("unchecked")
          private List<CatMenuseccion> reorderSecciones(List<CatMenuseccion> secciones){
              Collections.sort(secciones, new Comparator(){
                  public int compare(Object o1, Object o2){
                      CatMenuseccion menuSeccion1 = (CatMenuseccion)o1;
                      CatMenuseccion menuSeccion2 = (CatMenuseccion)o2;
                      return menuSeccion1.getOrden() - menuSeccion2.getOrden();
                  }
              });
              return secciones;
          }
          
          @SuppressWarnings("unchecked")
          private List<CatMenuitem> reorderMenuitems(List<CatMenuitem> menuitems){
              Collections.sort(menuitems, new Comparator(){
                  public int compare(Object o1, Object o2){
                      CatMenuitem menuitem1 = (CatMenuitem)o1;
                      CatMenuitem menuitem2 = (CatMenuitem)o2;
                      return menuitem1.getOrden() - menuitem2.getOrden();
                  }
              });
              return menuitems;
          }
          

           

          The template looks like this:

           

          <rich:toolBar binding="#{controller_sesion.htmlToolBar}" styleClass="menuMain" itemSeparator="line" />
          

           

          This is a 1-level submenu algorithm, if you want more levels I think you get the picture how it could be done (just add more menuitems as children of the desired menuitem).

           

          Here I used Hashtable for sections to avoid section duplicates because in my DB structure each menuitem object cantained the section object it corresponded to, so don't bother about some code chunks where I make some list purification. The actual toolbar building begins from the first big "for" loop.

           

          Also since in my case all section and menuitem objects had order ("orden") attribute I used reorderSecciones() and reorderMenuitems() methods to reorder the lists to make the menus dispay in the desired order. I've mentioned this one because it's almost always the second question asked when wrking with toolbars.

           

          In practice it looked like this:

           

          screen.png

           

          Hope this helps

          • 2. Re: List dropDownMenu
            sam_pc1611

            great, thank u brother