List flatList = new LinkedList();
Map levels = new HashMap();
Set itemNames = new HashSet();
UIPanelMenu parentMenu = (UIPanelMenu)component;
boolean expandSingle = parentMenu.isExpandSingle();
String selectedChild = parentMenu.getSelectedName();
flatten(component.getChildren(), flatList, levels, 0);
panelMenu.append("new PanelMenu('")
.append(component.getClientId(context).toString())
.append("',")
.append(new Boolean(expandSingle).toString())
.append(",").append("'").append(selectedChild).append("'")
.append(");\n");
for (Iterator iter = flatList.iterator(); iter.hasNext();) {
UIComponent child = (UIComponent) iter.next();
if ((child instanceof UIPanelMenuItem)||(child instanceof UIPanelMenuGroup)) {
boolean childDisabled;
if (!((UIPanelMenu)component).isDisabled())
childDisabled = child instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child).isDisabled() : ((UIPanelMenuItem)child).isDisabled();
else
childDisabled = true;
boolean childRendered = child instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child).isRendered() : ((UIPanelMenuItem)child).isRendered();
boolean parentRendered = true;
if (! (child.getParent() instanceof UIPanelMenu))
parentRendered = child.getParent() instanceof UIPanelMenuGroup ? ((UIPanelMenuGroup)child.getParent()).isRendered() : ((UIPanelMenuItem)child.getParent()).isRendered();
if (!parentRendered){
child.getAttributes().put("rendered",Boolean.FALSE);
}
//UIPanelMenu parentMenu = findMenu(child);
String event = parentMenu.getEvent();
if ("".equals(event))
event = "click";
else if (event.startsWith("on"))
event = event.substring(2);
String onopen = (child instanceof UIPanelMenuGroup) && !((UIPanelMenuGroup)child).isDisabled() && !isParentDisabled(child) ?
parentMenu.getOngroupexpand() + ";" + ((UIPanelMenuGroup)child).getOnexpand() : "";
String onclose = (child instanceof UIPanelMenuGroup) && !((UIPanelMenuGroup)child).isDisabled() && !isParentDisabled(child) ?
parentMenu.getOngroupcollapse() + ";" + ((UIPanelMenuGroup)child).getOncollapse() : "";
String hoveredStyle = (child instanceof UIPanelMenuGroup ?
parentMenu.getHoveredGroupStyle() : parentMenu.getHoveredItemStyle())
+ ";" + (child instanceof UIPanelMenuGroup ?
((UIPanelMenuGroup)child).getHoverStyle() : ((UIPanelMenuItem)child).getHoverStyle());
String hoveredClass = (child instanceof UIPanelMenuGroup ?
parentMenu.getHoveredGroupClass() : parentMenu.getHoveredItemClass())
+ " " + (child instanceof UIPanelMenuGroup ?
((UIPanelMenuGroup)child).getHoverClass() : ((UIPanelMenuItem)child).getHoverClass());
String [] hoveredStyles = hoveredStyle.split(";");
String [] hoveredClasses = hoveredClass.split(" ");
String mode = getItemMode(child);
Object target = child.getAttributes().get("target");
String targetString;
if (null == target)
targetString = "";
else
targetString = target.toString();
if (childRendered && parentRendered){
if ( !isParentDisabled(child) ){
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){
List paramsList = encodeParams(context, (UIPanelMenuItem)child);
if(!paramsList.isEmpty()){
for (Iterator iterator = paramsList.iterator(); iterator.hasNext();) {
buffer.append((String)iterator.next());
}
}
}
buffer
.append("new PanelMenuItem(params,{myId:'")
.append((String) child.getClientId(context))
.append("',parentId:'")
.append((String) child.getParent().getClientId(context))
.append("'},{type:" + (child instanceof UIPanelMenuItem ? "\"item\"":"\"node\""))
.append(",onopen:"+("".equals(onopen) ? "\"\"" : "\"" + onopen + "\"")+",onclose:"+("".equals(onclose) ? "\"\"" : "\"" + onclose + "\""))
.append(",event:\"" + event + "\"")
.append(",mode:\"" + mode + "\"")
.append(",target:\"" + targetString + "\"")
.append(",disabled:" + new Boolean(childDisabled).toString())
.append(",target:\"" + targetString + "\"")
.append(",name:\"" + childName + "\"")
.append("},{");
for (int i = 0; i < hoveredStyles.length; i++)
if (!"".equals(hoveredStyles[i])) {
String [] temp = hoveredStyles[i].split(":");
String cssName = temp[0].trim();
String cssValue = temp[1].trim();
buffer.append("\"" + cssName + "\": \"" + cssValue + "\"");
if (i != hoveredStyles.length - 1)
buffer.append(",");
}
buffer.append("},");
if (hoveredClasses.length > 0) {
buffer.append("new Array(");
for (int i = 0; i < hoveredClasses.length; i++)
if (!"".equals(hoveredClasses[i])) {;
buffer.append("\"" + hoveredClasses[i] + "\"");
if (i != hoveredClasses.length - 1)
buffer.append(",");
}
buffer.append("),");
} else
buffer.append("new Array(),");
buffer.append(levels.get(child.getClientId(context)));
switchOnImagesIfNeeded(context,child,buffer);
addActionIfNeeded(context,child,buffer);
setExpandedIfNeeded(context,child,buffer);
addAjaxFunction(context,child,buffer);
addOnItemHover(parentMenu.getOnitemhover(), child, buffer);
String iconPos = "left";
boolean isTopLevel = isTopLevel(child);
if(child instanceof UIPanelMenuGroup){
iconPos = isTopLevel ? parentMenu.getIconGroupTopPosition() : parentMenu.getIconGroupPosition();
} else {
iconPos = isTopLevel ? parentMenu.getIconItemTopPosition() : parentMenu.getIconItemPosition();
}
buffer.append(","+'"'+iconPos+'"');
addImages(buffer,context,child,component.getClientId(context).toString());