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.event.AfterEncodeEvent; 27 import com.sun.enterprise.tools.jsfext.event.BeforeEncodeEvent; 28 import com.sun.enterprise.tools.jsfext.event.EncodeEvent; 29 import com.sun.enterprise.tools.jsfext.event.handlers.Handler; 30 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerContext; 31 import com.sun.enterprise.tools.jsfext.event.handlers.HandlerContextImpl; 32 33 import com.sun.web.ui.util.RenderingUtilities; 34 35 import java.io.IOException ; 36 import java.util.ArrayList ; 37 import java.util.EventObject ; 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.Map ; 42 43 import javax.faces.component.UIComponent; 44 import javax.faces.context.FacesContext; 45 46 47 54 public abstract class LayoutElementBase implements LayoutElement { 55 56 62 protected LayoutElementBase(LayoutElement parent, String id) { 63 setParent(parent); 64 _id = id; 65 } 66 67 68 75 public void addChildLayoutElement(LayoutElement element) { 76 _layoutElements.add(element); 77 } 78 79 80 85 public List getChildLayoutElements() { 86 return _layoutElements; 87 } 88 89 90 97 public LayoutDefinition getLayoutDefinition() { 98 LayoutElement cur = this; 100 while (cur.getParent() != null) { 101 cur = cur.getParent(); 102 } 103 104 return (LayoutDefinition) cur; 106 } 107 108 109 114 public LayoutElement getParent() { 115 return _parent; 116 } 117 118 119 124 protected void setParent(LayoutElement parent) { 125 _parent = parent; 126 } 127 128 129 137 private String getId() { 138 if (_id == null) { 139 return ""; 140 } 141 return _id; 142 } 143 144 151 public String getUnevaluatedId() { 152 return _id; 153 } 154 155 168 public String getId(FacesContext context, UIComponent parent) { 169 Object value = resolveValue(context, parent, getId()); 171 172 return (value == null) ? "" : value.toString(); 174 } 175 176 188 public Object resolveValue(FacesContext context, UIComponent parent, String value) { 189 return ComponentUtil.resolveValue(context, this, parent, value); 190 } 191 192 203 protected abstract boolean encodeThis(FacesContext context, UIComponent component) throws IOException ; 204 205 219 public void encode(FacesContext context, UIComponent component) throws IOException { 220 Object result = dispatchHandlers(context, BEFORE_ENCODE, 223 new BeforeEncodeEvent(component)); 224 225 boolean renderChildren = encodeThis(context, component); 227 228 231 234 if (renderChildren) { 236 result = dispatchHandlers(context, ENCODE, 237 new EncodeEvent(component)); 238 239 LayoutElement childElt = null; 241 Iterator it = getChildLayoutElements().iterator(); 242 while (it.hasNext()) { 243 childElt = (LayoutElement) it.next(); 244 childElt.encode(context, component); 245 } 246 } 247 248 result = dispatchHandlers(context, AFTER_ENCODE, 250 new AfterEncodeEvent(component)); 251 } 252 253 254 270 public Object dispatchHandlers(FacesContext context, String eventType, EventObject event) { 271 Object eventObj = event.getSource(); 273 if (!(eventObj instanceof UIComponent)) { 274 eventObj = null; 275 } 276 List handlers = getHandlers(eventType, (UIComponent) eventObj); 277 278 if (handlers == null) { 280 return null; 281 } 282 283 HandlerContext handlerContext = 285 createHandlerContext(context, event, eventType); 286 287 return dispatchHandlers(handlerContext, handlers); 289 } 290 291 296 public Object dispatchHandlers(HandlerContext handlerCtx, List handlers) { 297 Object retVal = null; 298 Object result = null; 299 Handler handler = null; 300 Iterator it = handlers.iterator(); 301 while (it.hasNext()) { 302 try { 303 handler = (Handler) it.next(); 305 handlerCtx.setHandler(handler); 306 307 retVal = handler.invoke(handlerCtx); 309 310 if (retVal != null) { 312 result = retVal; 313 } 314 } catch (Exception ex) { 315 throw new RuntimeException ( 316 ex.getClass().getName() + " while attempting to " 317 + "process a '" + handlerCtx.getEventType() 318 + "' event for '" + getId() + "'.", ex); 319 } 320 } 321 322 return result; 324 } 325 326 334 protected HandlerContext createHandlerContext(FacesContext context, EventObject event, String eventType) { 335 return new HandlerContextImpl(context, this, event, eventType); 336 } 337 338 345 public List getHandlers(String type) { 346 return (List ) _handlersByType.get(type); 347 } 348 349 353 public Map getHandlersByTypeMap() { 354 return _handlersByType; 355 } 356 357 364 public void setHandlersByTypeMap(Map map) { 365 if (map != null) { 366 _handlersByType = map; 367 } 368 } 369 370 378 public List getHandlers(String type, UIComponent comp) { 379 List handlers = getHandlers(type); 381 382 389 return handlers; 390 } 391 392 398 public void setHandlers(String type, List handlers) { 399 _handlersByType.put(type, handlers); 400 } 401 402 412 public static void encodeChild(FacesContext context, UIComponent component) throws IOException { 413 442 443 RenderingUtilities.renderComponent(component, context); 444 } 445 446 447 450 private List _layoutElements = new ArrayList (); 451 452 453 456 private LayoutElement _parent = null; 457 458 461 private Map _handlersByType = new HashMap (); 462 463 466 private String _id = null; 467 468 472 public static final String AFTER_ENCODE = "afterEncode"; 473 474 478 public static final String BEFORE_ENCODE = "beforeEncode"; 479 480 485 public static final String ENCODE = "encode"; 486 } 487 | Popular Tags |