1 16 package org.apache.myfaces.custom.navmenu; 17 18 import org.apache.commons.logging.LogFactory; 19 import org.apache.commons.logging.Log; 20 import org.apache.myfaces.component.UserRoleUtils; 21 22 import javax.faces.component.UIComponent; 23 import javax.faces.component.UISelectItems; 24 import javax.faces.context.FacesContext; 25 import java.util.*; 26 27 47 public class NavigationMenuUtils 48 { 49 private static final Log log = LogFactory.getLog(NavigationMenuUtils.class); 50 51 public static List getNavigationMenuItemList(UIComponent uiComponent) 52 { 53 List list = new ArrayList(uiComponent.getChildCount()); 54 for (Iterator children = uiComponent.getChildren().iterator(); children.hasNext(); ) 55 { 56 UIComponent child = (UIComponent)children.next(); 57 if (child instanceof UINavigationMenuItem) 58 { 59 NavigationMenuItem item; 60 Object value = ((UINavigationMenuItem)child).getValue(); 61 if (value != null) 62 { 63 if (!(value instanceof NavigationMenuItem)) 65 { 66 FacesContext facesContext = FacesContext.getCurrentInstance(); 67 throw new IllegalArgumentException ("Value binding of UINavigationMenuItem with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem"); 68 } 69 item = (NavigationMenuItem)value; 70 } 71 else 72 { 73 UINavigationMenuItem uiItem = (UINavigationMenuItem)child; 74 String label = uiItem.getItemLabel(); 75 if (label == null && uiItem.getItemValue() != null) 76 { 77 label = uiItem.getItemValue().toString(); 78 } 79 item = new NavigationMenuItem(uiItem.getItemValue(), 80 label, 81 uiItem.getItemDescription(), 82 uiItem.isItemDisabled() || ! UserRoleUtils.isEnabledOnUserRole(uiItem), 83 uiItem.isRendered(), 84 uiItem.getAction(), 85 uiItem.getIcon(), 86 uiItem.isSplit()); 87 } 88 list.add(item); 89 if (child.getChildCount() > 0) 90 { 91 List l = getNavigationMenuItemList(child); 92 item.setNavigationMenuItems((NavigationMenuItem[]) l.toArray(new NavigationMenuItem[l.size()])); 93 } 94 } 95 else if (child instanceof UISelectItems) 96 { 97 Object value = ((UISelectItems)child).getValue(); 98 if (value instanceof NavigationMenuItem) 99 { 100 list.add(value); 101 } 102 else if (value instanceof NavigationMenuItem[]) 103 { 104 for (int i = 0; i < ((NavigationMenuItem[])value).length; i++) 105 { 106 list.add(((NavigationMenuItem[])value)[i]); 107 } 108 } 109 else if (value instanceof Collection) 110 { 111 for (Iterator it = ((Collection)value).iterator(); it.hasNext();) 112 { 113 Object item = it.next(); 114 if (!(item instanceof NavigationMenuItem)) 115 { 116 FacesContext facesContext = FacesContext.getCurrentInstance(); 117 throw new IllegalArgumentException ("Collection referenced by UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not contain Objects of type NavigationMenuItem"); 118 } 119 list.add(item); 120 } 121 } 122 else 123 { 124 FacesContext facesContext = FacesContext.getCurrentInstance(); 125 throw new IllegalArgumentException ("Value binding of UINavigationMenuItems with id " + child.getClientId(facesContext) + " does not reference an Object of type NavigationMenuItem, NavigationMenuItem[], Collection or Map"); 126 } 127 } 128 else 129 { 130 FacesContext facesContext = FacesContext.getCurrentInstance(); 131 log.error("Invalid child with id " + child.getClientId(facesContext) + "of component with id : "+uiComponent.getClientId(facesContext) 132 +" : must be UINavigationMenuItem or UINavigationMenuItems, is of type : "+((child==null)?"null":child.getClass().getName())); 133 } 134 } 135 136 return list; 137 } 138 139 } 140 | Popular Tags |