1 23 package com.sun.enterprise.tools.jsfext.layout.descriptor; 24 25 import com.sun.enterprise.tools.jsfext.component.ComponentUtil; 26 import com.sun.enterprise.tools.jsfext.component.ChildManager; 27 import com.sun.enterprise.tools.jsfext.el.VariableResolver; 28 import com.sun.enterprise.tools.jsfext.event.AfterCreateEvent; 29 import com.sun.enterprise.tools.jsfext.event.AfterEncodeEvent; 30 import com.sun.enterprise.tools.jsfext.event.BeforeCreateEvent; 31 import com.sun.enterprise.tools.jsfext.event.BeforeEncodeEvent; 32 33 import java.io.IOException ; 34 import java.util.ArrayList ; 35 import java.util.HashMap ; 36 import java.util.List ; 37 import java.util.Map ; 38 39 import javax.faces.context.FacesContext; 40 import javax.faces.component.UIComponent; 41 42 43 54 public class LayoutComponent extends LayoutElementBase implements LayoutElement { 55 56 59 public LayoutComponent(LayoutElement parent, String id, ComponentType type) { 60 super(parent, id); 61 _type = type; 62 } 63 64 67 public ComponentType getType() { 68 return _type; 69 } 70 71 76 public void setOverwrite(boolean value) { 77 _overwrite = value; 78 } 79 80 85 public boolean isOverwrite() { 86 return _overwrite; 87 } 88 89 101 public void addChildLayoutElement(LayoutElement element) { 102 if (!(element instanceof LayoutComponent) 103 && !(element instanceof LayoutFacet)) { 104 throw new IllegalArgumentException ("Only LayoutComponent and " 105 + "LayoutFacet LayoutElements may be added as children to " 106 + "a LayoutComponent!"); 107 } 108 super.addChildLayoutElement(element); 109 } 110 111 118 public void addOption(String name, Object value) { 119 _options.put(name, value); 120 } 121 122 129 public void addOptions(Map map) { 130 _options.putAll(map); 131 } 132 133 143 public Object getOption(String name) { 144 return _options.get(name); 145 } 146 147 159 public Object getEvaluatedOption(FacesContext ctx, String name, UIComponent component) { 160 Object value = getOption(name); 162 163 return VariableResolver.resolveVariables(ctx, this, component, value); 171 } 172 173 181 public boolean containsOption(String name) { 182 return _options.containsKey(name); 183 } 184 185 190 public void setOptions(Map options) { 191 _options = options; 192 } 193 194 200 public Map getOptions() { 201 return _options; 202 } 203 204 212 public void encode(FacesContext context, UIComponent parent) throws IOException { 213 if (isOverwrite()) { 215 String id = getId(context, parent); 216 if (parent.getFacets().remove(id) == null) { 217 UIComponent child = ComponentUtil.findChild(parent, id, null); 218 if (child != null) { 219 parent.getChildren().remove(child); 221 } 222 } 223 } 224 225 UIComponent childComponent = null; 228 if (parent instanceof ChildManager) { 229 childComponent = ((ChildManager) parent).getChild(context, this); 231 } else { 232 childComponent = getChild(context, parent); 234 } 235 236 Object result = dispatchHandlers(context, BEFORE_ENCODE, 237 new BeforeEncodeEvent(childComponent)); 238 239 encodeChild(context, childComponent); 241 242 result = dispatchHandlers(context, AFTER_ENCODE, 244 new AfterEncodeEvent(childComponent)); 245 } 246 247 252 public boolean encodeThis(FacesContext context, UIComponent parent) throws IOException { 253 return false; 254 } 255 256 271 public UIComponent getChild(FacesContext context, UIComponent parent) throws IOException { 272 UIComponent childComponent = null; 273 274 String id = this.getId(context, parent); 276 if ((id != null) && !(id.trim().equals(""))) { 277 childComponent = ComponentUtil.findChild(parent, id, id); 279 if (childComponent != null) { 280 return childComponent; 281 } 282 } 283 284 287 this.beforeCreate(context, parent); 289 290 childComponent = 292 ComponentUtil.createChildComponent(context, this, parent); 293 294 this.afterCreate(context, childComponent); 296 297 return childComponent; 299 } 300 301 311 public List getHandlers(String type, UIComponent comp) { 312 List handlers = null; 314 315 if (comp != null) { 317 List instHandlers = (List ) comp.getAttributes().get(type); 318 if ((instHandlers != null) && (instHandlers.size() > 0)) { 319 handlers = new ArrayList (instHandlers); 322 323 List defHandlers = getHandlers(type); 324 if (defHandlers != null) { 325 handlers.addAll(getHandlers(type)); 327 } 328 } 329 } 330 if (handlers == null) { 331 handlers = getHandlers(type); 332 } 333 334 return handlers; 335 } 336 337 346 public Object beforeCreate(FacesContext context, UIComponent parent) { 347 return dispatchHandlers( 349 context, BEFORE_CREATE, new BeforeCreateEvent(parent)); 350 } 351 352 361 public Object afterCreate(FacesContext context, UIComponent component) { 362 return dispatchHandlers( 364 context, AFTER_CREATE, new AfterCreateEvent(component)); 365 } 366 367 374 public boolean isFacetChild() { 375 return _isFacetChild; 376 } 377 378 385 public void setFacetChild(boolean facetChild) { 386 _isFacetChild = facetChild; 387 } 388 389 398 public boolean isNested() { 399 return _nested; 400 } 401 402 410 public void setNested(boolean value) { 411 _nested = value; 412 } 413 414 415 418 private ComponentType _type = null; 419 420 425 private boolean _overwrite = false; 426 427 430 private Map _options = new HashMap (); 431 432 435 private boolean _isFacetChild = true; 436 437 441 public static final String AFTER_CREATE = "afterCreate"; 442 443 447 public static final String BEFORE_CREATE = "beforeCreate"; 448 449 454 public static final String FACET_NAME = "_facetName"; 455 456 459 private boolean _nested = false; 460 } 461 | Popular Tags |