1 14 15 package com.sun.facelets.tag.jsf; 16 17 import java.io.IOException ; 18 import java.util.logging.Level ; 19 import java.util.logging.Logger ; 20 21 import javax.el.ELException; 22 import javax.el.MethodExpression; 23 import javax.el.ValueExpression; 24 import javax.faces.FacesException; 25 import javax.faces.application.Application; 26 import javax.faces.component.ActionSource; 27 import javax.faces.component.ActionSource2; 28 import javax.faces.component.EditableValueHolder; 29 import javax.faces.component.UIComponent; 30 import javax.faces.component.UIViewRoot; 31 import javax.faces.component.ValueHolder; 32 import javax.faces.context.FacesContext; 33 import javax.faces.convert.Converter; 34 import javax.faces.el.ValueBinding; 35 import javax.faces.event.ActionEvent; 36 import javax.faces.event.MethodExpressionActionListener; 37 import javax.faces.event.MethodExpressionValueChangeListener; 38 import javax.faces.event.ValueChangeEvent; 39 import javax.faces.validator.MethodExpressionValidator; 40 41 import com.sun.facelets.FaceletContext; 42 import com.sun.facelets.el.ELAdaptor; 43 import com.sun.facelets.el.LegacyMethodBinding; 44 import com.sun.facelets.el.LegacyValueBinding; 45 import com.sun.facelets.tag.MetaTagHandler; 46 import com.sun.facelets.tag.TagAttribute; 47 import com.sun.facelets.tag.Metadata; 48 import com.sun.facelets.tag.TagException; 49 import com.sun.facelets.tag.TagHandler; 50 import com.sun.facelets.tag.MetaRuleset; 51 import com.sun.facelets.util.FacesAPI; 52 53 60 public class ComponentHandler extends MetaTagHandler { 61 62 private final static Logger log = Logger 63 .getLogger("facelets.tag.component"); 64 65 private final TagAttribute binding; 66 67 private final String componentType; 68 69 private final TagAttribute id; 70 71 private final String rendererType; 72 73 public ComponentHandler(ComponentConfig config) { 74 super(config); 75 this.componentType = config.getComponentType(); 76 this.rendererType = config.getRendererType(); 77 this.id = this.getAttribute("id"); 78 this.binding = this.getAttribute("binding"); 79 } 80 81 113 public final void apply(FaceletContext ctx, UIComponent parent) 114 throws IOException , FacesException, ELException { 115 if (parent == null) { 117 throw new TagException(this.tag, "Parent UIComponent was null"); 118 } 119 120 String id = ctx.generateUniqueId(this.tagId); 122 123 UIComponent c = ComponentSupport.findChildByTagId(parent, id); 125 boolean componentFound = false; 126 if (c != null) { 127 componentFound = true; 128 if (log.isLoggable(Level.FINE)) { 130 log.fine(this.tag 131 + " Component["+id+"] Found, marking children for cleanup"); 132 } 133 ComponentSupport.markForDeletion(c); 134 } else { 135 c = this.createComponent(ctx); 136 if (log.isLoggable(Level.FINE)) { 137 log.fine(this.tag + " Component["+id+"] Created: " 138 + c.getClass().getName()); 139 } 140 this.setAttributes(ctx, c); 141 142 c.getAttributes().put(ComponentSupport.MARK_CREATED, id); 144 145 if (this.id != null) { 147 c.setId(this.id.getValue(ctx)); 148 } else { 149 UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent); 150 if (root != null) { 151 String uid = root.createUniqueId(); 152 c.setId(uid); 153 } 154 } 155 156 if (this.rendererType != null) { 157 c.setRendererType(this.rendererType); 158 } 159 160 this.onComponentCreated(ctx, c, parent); 162 } 163 164 this.applyNextHandler(ctx, c); 166 167 if (componentFound) { 169 ComponentSupport.finalizeForDeletion(c); 170 parent.getChildren().remove(c); 171 } 172 173 174 this.onComponentPopulated(ctx, c, parent); 175 176 parent.getChildren().add(c); 180 } 181 182 197 protected UIComponent createComponent(FaceletContext ctx) { 198 UIComponent c = null; 199 FacesContext faces = ctx.getFacesContext(); 200 Application app = faces.getApplication(); 201 if (this.binding != null) { 202 ValueExpression ve = this.binding.getValueExpression(ctx, 203 Object .class); 204 if (FacesAPI.getVersion() >= 12) { 205 c = app.createComponent(ve, faces, this.componentType); 206 if (c != null) { 207 if (FacesAPI.getComponentVersion(c) >= 12) { 209 c.setValueExpression("binding", ve); 210 } else { 211 ValueBinding vb = new LegacyValueBinding(ve); 212 c.setValueBinding("binding", vb); 213 } 214 215 } 216 } else { 217 ValueBinding vb = new LegacyValueBinding(ve); 218 c = app.createComponent(vb, faces, this.componentType); 219 if (c != null) { 220 c.setValueBinding("binding", vb); 221 } 222 } 223 } else { 224 c = app.createComponent(this.componentType); 225 } 226 return c; 227 } 228 229 238 protected String getId(FaceletContext ctx) { 239 if (this.id != null) { 240 return this.id.getValue(ctx); 241 } 242 return ctx.generateUniqueId(this.tagId); 243 } 244 245 protected MetaRuleset createMetaRuleset(Class type) { 246 MetaRuleset m = super.createMetaRuleset(type); 247 248 m.ignore("binding").ignore("id"); 250 251 m.addRule(ComponentRule.Instance); 253 254 if (ActionSource.class.isAssignableFrom(type)) { 256 m.addRule(ActionSourceRule.Instance); 257 } 258 259 if (ValueHolder.class.isAssignableFrom(type)) { 261 m.addRule(ValueHolderRule.Instance); 262 263 if (EditableValueHolder.class.isAssignableFrom(type)) { 265 m.ignore("submittedValue"); 266 m.ignore("valid"); 267 m.addRule(EditableValueHolderRule.Instance); 268 } 269 } 270 271 return m; 272 } 273 274 284 protected void onComponentCreated(FaceletContext ctx, UIComponent c, UIComponent parent) { 285 } 287 288 protected void onComponentPopulated(FaceletContext ctx, UIComponent c, UIComponent parent) { 289 } 291 292 protected void applyNextHandler(FaceletContext ctx, UIComponent c) 293 throws IOException , FacesException, ELException { 294 this.nextHandler.apply(ctx, c); 296 } 297 } 298 | Popular Tags |