1 16 package org.apache.myfaces.component.html.util; 17 18 import javax.faces.component.NamingContainer; 19 import javax.faces.component.UIComponent; 20 import javax.faces.component.UIData; 21 import javax.faces.context.FacesContext; 22 import javax.faces.render.Renderer; 23 24 import org.apache.myfaces.renderkit.JSFAttr; 25 26 33 public class HtmlComponentUtils 34 { 35 private static final String TRUE = "true"; 36 private static final String KEY_COMPONENT_ID_MAP = "KEY_COMPONENT_ID_MAP"; 37 38 41 private HtmlComponentUtils() 42 {} 43 44 55 public static String getClientId(UIComponent component, 56 Renderer renderer, 57 FacesContext context) 58 { 59 Boolean forceValue = (Boolean )component.getAttributes().get(JSFAttr.FORCE_ID_ATTR); 61 boolean forceId = false; 62 63 if (forceValue != null) 64 { 65 forceId = forceValue.booleanValue(); 66 } 67 68 if (forceId && component.getId() != null) 69 { 70 String clientId = component.getId(); 71 72 75 UIComponent parentContainer = HtmlComponentUtils.findParentNamingContainer(component, false); 76 if (parentContainer != null) 77 { 78 if (parentContainer instanceof UIData) 79 { 80 Boolean forceIdIndexValue = (Boolean )component.getAttributes().get(JSFAttr.FORCE_ID_INDEX_ATTR); 82 boolean forceIdIndex = true; 83 84 if (forceIdIndexValue != null) 85 { 86 forceIdIndex = forceIdIndexValue.booleanValue(); 87 } 88 89 if (forceIdIndex) 91 { 92 int rowIndex = ( (UIData) parentContainer).getRowIndex(); 93 if (rowIndex != -1) { 94 clientId = clientId + "[" + rowIndex + "]"; 95 } 96 } 97 } 98 } 99 100 if (renderer != null) 102 { 103 clientId = renderer.convertClientId(context, clientId); 104 } 105 106 return clientId; 107 } 108 else 109 { 110 return null; 111 } 112 } 113 114 123 public static UIComponent findParentNamingContainer(UIComponent component, 124 boolean returnRootIfNotFound) 125 { 126 UIComponent parent = component.getParent(); 127 if (returnRootIfNotFound && parent == null) 128 { 129 return component; 130 } 131 while (parent != null) 132 { 133 if (parent instanceof NamingContainer) return parent; 134 if (returnRootIfNotFound) 135 { 136 UIComponent nextParent = parent.getParent(); 137 if (nextParent == null) 138 { 139 return parent; } 141 parent = nextParent; 142 } 143 else 144 { 145 parent = parent.getParent(); 146 } 147 } 148 return null; 149 } 150 } 151 | Popular Tags |