0 Replies Latest reply on Oct 26, 2011 1:35 PM by mmulligan03

    HtmlDropDownMenu with click able parent label

    mmulligan03 Newbie

      I've created a class to build a drop down menu from a database.  The creation of the menu works as planned but what I can't figure out is how to make the parent item a link to a page. 

       

      For instance I might have a menu structured like so:

      1. My Account
        1. Billing
          1. Pay Bill
          2. Change Billing
        2. Profile

      etc...

       

      The way my current code works is I cannot click on Billing and have it bring me to the main Billing page

       

      I know that when writing this out using tags I can specify a <f:facet like so:

      <rich:dropDownMenu>
                  <facet name="label" >
                      <s:link view="/billing.xhtml" value="Billing" />
                  </facet> 
      ...
      

       

      and that will give me a click able link.  How can I do this in my code below? (the _parseSubMenu method is where the HtmlDropDownMenu is created)

       

      @Name("systemMenus")
      @Scope(ScopeType.SESSION)
      public class SystemMenus implements java.io.Serializable {
      
          private static final long serialVersionUID = 1579546151447504530L;
          private ArrayList<MenuItem> headerMenu;
          private HtmlToolBar navBar;
      
          @Logger
          Log log;
      
          @In(create = true)
          private MenuItemsManager menuItemsManager;
      
          @In
          UserIdentity identity;
      
          @Observer(Identity.EVENT_POST_AUTHENTICATE)
          public void loadMenus() {
              headerMenu = menuItemsManager.loadMenuItems(1);
          }
      
          public void setNavBar(HtmlToolBar navBar) {
              this.navBar = navBar;
          }
      
          /**
           * Build the navigation menu from the headerMenu property
           * 
           * @return
           */
          public UIToolBar getNavBar() {
      
              navBar = new HtmlToolBar();
      
              if (headerMenu == null) {
                  loadMenus();
              }
      
              for (MenuItem item : headerMenu) {
                  UIComponent menu = _parseSubMenu(item, 0);
                  if (menu != null) {
                      navBar.getChildren().add(menu);
                  }
              }
      
              if (log.isDebugEnabled())
                  log.debug("Count: " + navBar.getChildCount());
      
              if (!(navBar.getChildCount() > 0))
                  navBar = null;
      
              return navBar;
          }
      
          /**
           * From a MenuItem either create a HtmlMenuItem or a HtmlDropDownMenu
           * 
           * @param item
           *            MenuItem this is the parent item
           * @param level
           *            What level of the menu are we on. This is used for determinig
           *            direction of the menu past the first level
           * @return either a HtmlMenuItem or a HtmlDropDownMenu
           */
          private UIComponent _parseSubMenu(MenuItem item, int level) {
      
              // if the item pageView isn't null or empty check that for permission
              // if the item does not have a page view (eg. place holder for a sub
              // menu) then check the label against the permissions
              if (((item.getPageView() != null && (!item.getPageView().equals(""))) && !identity.hasPermission(
                      item.getPageView(), "render"))
                      || ((item.getPageView() == null || item.getPageView().equals("")) && !identity.hasPermission(
                              item.getLabel(), "render"))) {
                  if (log.isDebugEnabled())
                      log.debug("Denied to render: " + item.getLabel() + " - " + item.getPageView());
                  return null;
              }
      
              UIComponent ddm = null;
      
              if (item.getMenuItems().size() > 0) {
      
                  if (level > 0) {
                      ddm = new HtmlMenuGroup();
                      // if the level is greater than 0 we want the menu to appear to
                      // the right
                      ((HtmlMenuGroup) ddm).setDirection("bottom-right");
                      ((HtmlMenuGroup) ddm).setValue(item.getLabel());
                  } else {
                      ddm = new HtmlDropDownMenu();
                      ((HtmlDropDownMenu) ddm).setHideDelay(400);
                      ((HtmlDropDownMenu) ddm).setValue(item.getLabel());
                  }
                  for (MenuItem child : item.getMenuItems()) {
                      UIComponent ui = _parseSubMenu(child, (level + 1));
                      // if the children of this parent is not null add it to
                      // the menu item
                      // if it is null no need to show the parent so make the
                      // UIComponent null
                      if (ui != null)
                          ddm.getChildren().add(ui);
                      else
                          ddm = null;
                  }
              } else {
                  ddm = _createMenuItem(item);
              }
              return ddm;
          }
      
          /**
           * From a menu item create an HtmlMenuItem object
           * 
           * @param item
           * @return
           */
          private HtmlMenuItem _createMenuItem(MenuItem item) {
              HtmlMenuItem mi = new HtmlMenuItem();
              if (item.getPageView() != null && !item.getPageView().equals(""))
                  mi.setOnclick(String.format("window.location.href='%s'", _pageViewToUrl(item.getPageView())));
              mi.getChildren().add(_createLink(item));
              return mi;
          }
      
          /**
           * From a menu item create an HtmlLink object
           * 
           * @param item
           * @return
           */
          private HtmlLink _createLink(MenuItem item) {
              HtmlLink link = new HtmlLink();
              link.setView(item.getPageView());
              link.setValue(item.getLabel());
              return link;
          }
      
          /**
           * Take a viewId and transform it into a usable URL
           * 
           * @param viewId
           * @return URL string
           */
          private String _pageViewToUrl(String viewId) {
              String url = FacesContext.getCurrentInstance().getApplication().getViewHandler()
                      .getActionURL(FacesContext.getCurrentInstance(), viewId);
              if (log.isInfoEnabled())
                  log.info("ViewID: " + viewId + " URL: " + url);
              return url;
          }
      
          /**
           * @param headerMenu
           *            the headerMenu to set
           */
          public void setHeaderMenu(ArrayList<MenuItem> headerMenu) {
              this.headerMenu = headerMenu;
          }
      
          /**
           * @return the headerMenu
           */
          public ArrayList<MenuItem> getHeaderMenu() {
              return headerMenu;
          }
      }