1 23 package com.sun.enterprise.tools.jsfext.component.factory.basic; 24 25 import com.sun.enterprise.tools.jsfext.component.ComponentUtil; 26 import com.sun.enterprise.tools.jsfext.util.Util; 27 28 import com.sun.enterprise.tools.jsfext.component.factory.ComponentFactoryBase; 29 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.Properties ; 36 37 import javax.faces.context.FacesContext; 38 import javax.faces.component.UIComponent; 39 40 41 73 public class DynamicTreeNodeFactory extends ComponentFactoryBase { 74 75 78 public DynamicTreeNodeFactory() { 79 } 80 81 82 93 public UIComponent create(FacesContext context, LayoutComponent descriptor, UIComponent parent) { 94 TreeAdaptor treeAdaptor = getTreeAdaptor(context, descriptor, parent); 96 97 treeAdaptor.init(); 99 100 Object currentObj = treeAdaptor.getTreeNodeObject(); 102 103 return processNode(context, treeAdaptor, currentObj, parent); 105 } 106 107 113 protected TreeAdaptor getTreeAdaptor(FacesContext ctx, LayoutComponent desc, UIComponent parent) { 114 TreeAdaptor adaptor = null; 115 Object cls = desc.getEvaluatedOption(ctx, TREE_ADAPTOR_CLASS, parent); 116 if (cls == null) { 117 throw new IllegalArgumentException ("'" + TREE_ADAPTOR_CLASS 118 + "' must be specified!"); 119 } 120 try { 121 Class adaptorClass = Util.getClass(cls); 122 adaptor = (TreeAdaptor) adaptorClass.getMethod("getInstance", 123 (Class []) new Class [] {FacesContext.class, 124 LayoutComponent.class, UIComponent.class}). 125 invoke((Object ) null, 126 (Object []) new Object [] {ctx, 127 desc, parent}); 128 } catch (ClassNotFoundException ex) { 129 throw new RuntimeException (ex); 130 } catch (NoSuchMethodException ex) { 131 throw new RuntimeException (ex); 132 } catch (IllegalAccessException ex) { 133 throw new RuntimeException (ex); 134 } catch (InvocationTargetException ex) { 135 throw new RuntimeException (ex); 136 } 137 138 return adaptor; 140 } 141 142 147 protected UIComponent processNode(FacesContext ctx, TreeAdaptor adaptor, Object currentObj, UIComponent parent) { 148 String id = adaptor.getId(currentObj); 150 String factoryClass = adaptor.getFactoryClass(currentObj); 151 Map props = adaptor.getFactoryOptions(currentObj); 154 Properties properties = Util.mapToProperties(props); 155 156 UIComponent node = ComponentUtil.getChild( 158 (UIComponent) parent, id, factoryClass, properties); 159 160 if (parent != null) { 165 parent.getFacets().remove(id); 166 parent.getChildren().add(node); 167 } 168 169 configureTreeNode(ctx, adaptor, node, currentObj); 171 172 List children = adaptor.getChildTreeNodeObjects(currentObj); 174 if (children != null) { 175 Iterator it = children.iterator(); 176 while (it.hasNext()) { 177 currentObj = it.next(); 178 processNode(ctx, adaptor, currentObj, node); 181 } 182 } 183 184 return node; 186 } 187 188 191 protected void configureTreeNode(FacesContext ctx, TreeAdaptor adaptor, UIComponent treeNode, Object currentObj) { 192 Map facets = adaptor.getFacets(treeNode, currentObj); 194 Map treeNodeFacets = treeNode.getFacets(); 195 if (facets != null) { 196 Iterator it = facets.keySet().iterator(); 197 String facetName; 198 Object facetValue; 199 while (it.hasNext()) { 200 facetName = (String ) it.next(); 201 facetValue = facets.get(facetName); 202 if (facetValue != null) { 203 treeNodeFacets.put(facetName, facetValue); 204 } 205 } 206 } 207 208 Map handlersByType = adaptor.getHandlersByType(treeNode, currentObj); 210 if (handlersByType != null) { 211 Iterator it = handlersByType.keySet().iterator(); 212 if (it.hasNext()) { 213 String eventType = null; 214 Map compAttrs = treeNode.getAttributes(); 215 while (it.hasNext()) { 216 eventType = (String ) it.next(); 219 compAttrs.put(eventType, handlersByType.get(eventType)); 220 } 221 } 222 } 223 } 224 225 234 public static final String TREE_ADAPTOR_CLASS = "treeAdaptorClass"; 235 } 236 | Popular Tags |