1 23 28 package com.sun.enterprise.tools.jsfext.component; 29 30 import com.sun.enterprise.tools.jsfext.el.VariableResolver; 31 import com.sun.enterprise.tools.jsfext.layout.descriptor.ComponentType; 32 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent; 33 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement; 34 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.Map ; 38 import java.util.Properties ; 39 40 import javax.faces.component.UIComponent; 41 import javax.faces.context.FacesContext; 42 import javax.faces.el.ValueBinding; 43 import javax.faces.webapp.UIComponentTag; 44 45 46 51 public class ComponentUtil { 52 53 57 private ComponentUtil() { 58 } 59 60 71 public static UIComponent getChild(UIComponent parent, String id) { 72 return findChild(parent, id, id); 73 } 74 75 89 public static UIComponent findChild(UIComponent parent, String id, String facetName) { 90 if (parent == null) { 92 return null; 93 } 94 95 UIComponent child = null; 97 if (facetName != null) { 98 child = (UIComponent) parent.getFacets().get(facetName); 99 if (child != null) { 100 return child; 101 } 102 } 103 104 if (id != null) { 106 Iterator it = parent.getChildren().iterator(); 107 while (it.hasNext()) { 108 child = (UIComponent) it.next(); 109 if (id.equals(child.getId())) { 110 return (child); 111 } 112 } 113 } 114 115 return null; 117 } 118 119 148 public static UIComponent getChild(UIComponent parent, String id, String factoryClass) { 149 return getChild(parent, id, factoryClass, id); 150 } 151 152 166 public static UIComponent getChild(UIComponent parent, String id, String factoryClass, String facetName) { 167 return getChild(parent, id, getComponentType(factoryClass), 168 null, facetName); 169 } 170 171 199 public static UIComponent getChild(UIComponent parent, String id, String factoryClass, Properties properties) { 200 return getChild(parent, id, factoryClass, properties, id); 201 } 202 203 219 public static UIComponent getChild(UIComponent parent, String id, String factoryClass, Properties properties, String facetName) { 220 return getChild(parent, id, getComponentType(factoryClass), 221 properties, facetName); 222 } 223 224 242 private static UIComponent getChild(UIComponent parent, String id, ComponentType type, Properties properties, String facetName) { 243 LayoutComponent desc = new LayoutComponent(null, id, type); 244 if (properties != null) { 245 desc.setOptions(properties); 246 } 247 if (facetName != null) { 248 desc.addOption(LayoutComponent.FACET_NAME, facetName); 251 } 252 253 return getChild(parent, desc); 254 } 255 256 270 private static ComponentType getComponentType(String factoryClass) { 271 ComponentType type = (ComponentType) _types.get(factoryClass); 273 if (type == null) { 274 type = new ComponentType(factoryClass, factoryClass); 276 Map newMap = new HashMap (_types); 277 newMap.put(factoryClass, type); 278 _types = newMap; 279 } 280 281 return type; 283 } 284 285 339 public static UIComponent getChild(UIComponent parent, LayoutComponent descriptor) { 340 FacesContext context = FacesContext.getCurrentInstance(); 341 if (parent instanceof ChildManager) { 343 return ((ChildManager) parent).getChild( 344 context, descriptor); 345 } 346 347 String childId = descriptor.getId(context, parent); 349 UIComponent childComponent = findChild(parent, childId, 350 (String ) descriptor.getEvaluatedOption( 351 context, LayoutComponent.FACET_NAME, null)); 352 if (childComponent != null) { 353 return childComponent; 354 } 355 356 return createChildComponent(context, descriptor, parent); 358 } 359 360 386 public static UIComponent createChildComponent(FacesContext context, LayoutComponent descriptor, UIComponent parent) { 387 if (descriptor == null) { 389 throw new IllegalArgumentException ("'descriptor' cannot be null!"); 390 } 391 392 return descriptor.getType().getFactory().create( 394 context, descriptor, parent); 395 } 396 397 415 public static Object setOption(FacesContext context, String key, Object value, LayoutElement desc, UIComponent component) { 416 value = VariableResolver.resolveVariables( 424 context, desc, component, value); 425 if (value == null) { 426 return null; 428 } 429 430 String strVal = value.toString(); 432 if (UIComponentTag.isValueReference(strVal)) { 433 ValueBinding vb = 434 context.getApplication().createValueBinding(strVal); 435 if (component != null) { 436 component.setValueBinding(key, vb); 437 } 438 value = vb; 439 } else { 440 if (component != null) { 442 component.getAttributes().put(key, value); 443 } 444 } 445 return value; 446 } 447 448 461 public static Object resolveValue(FacesContext context, LayoutElement elt, UIComponent parent, String value) { 462 Object result = VariableResolver.resolveVariables( 469 context, elt, parent, value); 470 471 if (result != null) { 473 String strVal = result.toString(); 474 if (UIComponentTag.isValueReference(strVal)) { 475 ValueBinding vb = 476 context.getApplication().createValueBinding(strVal); 477 result = vb.getValue(context); 478 } 479 } 480 481 return result; 483 } 484 485 488 private static Map _types = new HashMap (); 489 } 490 | Popular Tags |