StringBuffer buffer = new StringBuffer();
Set<String> itemNames = new HashSet<String>();
UIPanelMenu panelMenu = (UIPanelMenu)component;
Map<String, Integer> levels = new HashMap<String, Integer>();
List<UIComponent> flatList = new LinkedList<UIComponent>();
flatten(component.getChildren(), flatList, levels, 0);
for (UIComponent child : flatList) {
if (!(child instanceof UIPanelMenuItem || child instanceof UIPanelMenuGroup)) {
continue;
}
boolean parentRendered = child.getParent().isRendered();
if (!parentRendered) {
child.getAttributes().put("rendered",Boolean.FALSE);
}
boolean childRendered = child.isRendered();
if (!childRendered || !parentRendered || isParentDisabled(child)/* || !isParentExpended(child)*/) {
continue;
}
boolean childDisabled = panelMenu.isDisabled()
|| child instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child).isDisabled() : ((UIPanelMenuItem)child).isDisabled();
String childName;
if(child instanceof UIPanelMenuGroup){
childName = ((UIPanelMenuGroup)child).getName();
} else {
childName = ((UIPanelMenuItem)child).getName();
}
if(itemNames.contains(childName)){
throw new RuntimeException("Attibute \"name\" with value \"" + childName + "\" is already used in PanelMenu. It must be unique for every group/item.");
} else {
itemNames.add(childName);
}
buffer.append("var params = new Object();");
if (child instanceof UIPanelMenuItem) {
for (String param : encodeParams(context, (UIPanelMenuItem)child)) {
buffer.append(param);
}
}
buffer.append("new PanelMenuItem(ids, params,")
.append("{myId:'").append(child.getClientId(context)).append("',")
.append("parentId:'").append(child.getParent().getClientId(context)).append("'},")
.append("{type:").append(child instanceof UIPanelMenuItem ? "\"item\"":"\"node\"").append(",")
.append("onopen:").append(getOnOpen(panelMenu, child)).append(",")
.append("onclose:").append(getNoClose(panelMenu, child)).append(",")
.append("event:\"").append(getEvent(panelMenu)).append("\",")
.append("mode:\"").append(getItemMode(child)).append("\",")
.append("disabled:").append(childDisabled).append(",")
.append("target:\"").append(getTarget(child)).append("\",")
.append("name:\"").append(childName).append("\"")
.append("},{").append(getHoveredStyle(panelMenu, child)).append("},")
.append(getHoverClass(panelMenu, child))
.append(levels.get(child.getClientId(context)))
.append(switchOnImagesIfNeeded(context,child));
addActionIfNeeded(context,child,buffer);
setExpandedIfNeeded(context,child,buffer);
addAjaxFunction(context,child,buffer);
addOnItemHover(panelMenu.getOnitemhover(), child, buffer);
String iconPos;
boolean isTopLevel = isTopLevel(child);
if(child instanceof UIPanelMenuGroup){
iconPos = isTopLevel ? panelMenu.getIconGroupTopPosition() : panelMenu.getIconGroupPosition();
} else {
iconPos = isTopLevel ? panelMenu.getIconItemTopPosition() : panelMenu.getIconItemPosition();
}
buffer.append(",\""+iconPos+'"');
addImages(buffer, context, child, component.getClientId(context).toString());