15 October 2010

Disable an af:menu when all of its children are disabled


If you have a  requirement that a menu which has all of its children (menu items , go menu items , sub menus) disabled then the menu should also be disabled, then instead of writing the same conditions that you had to the children to the menu (boring and error prone) you can just use the following.


1. 
bind the af:menu to the backing bean, that is the binding prop eg. 
binding="#{myBacking.myMenu}"

2. 
set the disable property of the af:menu to a backing bean property e.g. 
disabled="#{myBacking.myMenuDisabled}"
where the the backing bean method is 
public boolean isMyMenuDisabled() {
return isMenuDisabled(this.myMenu);
}

3.
In the backing property method use the following method (you can decide if it should be placed to a generic class eg Backing beans Super class or in a Utils class as a statis method (e.g. like methods in ADFUtils))
/**

* In order to be disabled ALL children (&&) must be disabled
* @param menu
* @return
*/
protected boolean isMenuDisabled(RichMenu menu) {
 boolean isDisabled = true;
 List children = menu.getChildren();
 logger.debug("parse children ");
 Iterator iter = children.iterator();
 while (iter.hasNext()) {
  Object o = iter.next();
  if (o instanceof RichMenu) {
   RichMenu childMenu = (RichMenu)o;
   logger.trace("Found submenu with id: "+childMenu.getId());
   boolean isChildMenuDisabledProperty = childMenu.isDisabled();
   if (isChildMenuDisabledProperty ) {
    isDisabled = isDisabled &&    isChildMenuDisabledProperty;
    logger.trace("submenu with id:"+childMenu.getId()+" returned  (prop based) : " +
    isChildMenuDisabledProperty);
   }
  else {
   boolean isChildMenuIsDisabled = isMenuDisabled(childMenu);
   isDisabled = isDisabled && isChildMenuIsDisabled;
   logger.trace("submenu with id:"+childMenu.getId()+" returned   (parsed) : " + isChildMenuIsDisabled);
  }
 } else if (o instanceof RichCommandMenuItem) {
   RichCommandMenuItem menuItem = (RichCommandMenuItem)o;
   isDisabled = isDisabled && menuItem.isDisabled();
   logger.debug("Found menuItem with id: " + menuItem.getId() + " and disabled condition: " +
   menuItem.isDisabled());
 } else if (o instanceof RichGoMenuItem) {
   RichGoMenuItem goMenuItem = (RichGoMenuItem)o;
   isDisabled = isDisabled && goMenuItem.isDisabled();
   logger.debug("Found gomenuItem with id: " + goMenuItem.getId() + " and disabled condition: " +
  goMenuItem.isDisabled());
 } else if (o instanceof RichSeparator) {
  logger.debug("separator found. ingnoring...");
 } else {
   logger.info("Not Supported. Menu Item can only have: menu,   menuItem, goMenuItem ot separator.");
   }
  }
 return isDisabled;
}
It supports that the menu can have as its children:
° Sub menus
° menuItems
° goMenuItems

You can check other props also like visible and rendered prop.

PS: this was a repost from my other blog

No comments:

Post a Comment

You might also like:

Related Posts Plugin for WordPress, Blogger...