1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.io.*; 23 import java.util.*; 24 25 import org.apache.log4j.*; 26 import org.w3c.dom.*; 27 import org.w3c.dom.html.*; 28 29 import org.enhydra.barracuda.core.comp.renderer.*; 30 import org.enhydra.barracuda.core.comp.renderer.html.*; 31 import org.enhydra.barracuda.core.comp.renderer.xml.*; 32 import org.enhydra.barracuda.core.util.dom.*; 33 import org.enhydra.barracuda.core.view.*; 34 import org.enhydra.barracuda.plankton.data.*; 35 36 37 41 public class BComponent extends AbstractBComponent { 42 protected static final Logger logger = Logger.getLogger(BComponent.class.getName()); 44 45 public static final String VISIBILITY_MARKER = "visdom"; 47 48 protected String name = null; 55 protected boolean visible = true; 56 protected boolean enabled = true; 57 protected Map attrs = null; 58 protected RenderStrategy rs = null; 60 63 private View lnkView; 64 65 69 static { 70 HTMLRendererFactory rfHTML = new HTMLRendererFactory(); 71 installRendererFactory(rfHTML, BComponent.class, HTMLElement.class); 72 installRendererFactory(rfHTML, BComponent.class, HTMLDocument.class); 73 74 XMLRendererFactory rfXML = new XMLRendererFactory(); 75 installRendererFactory(rfXML, BComponent.class, Node.class); 76 } 77 78 81 static class HTMLRendererFactory implements RendererFactory { 82 public Renderer getInstance() {return new HTMLComponentRenderer();} 83 } 84 85 88 static class XMLRendererFactory implements RendererFactory { 89 public Renderer getInstance() {return new XMLComponentRenderer();} 90 } 91 92 93 94 108 public void setName(String iname) { 109 name = iname; 110 } 111 112 117 public String getName() { 118 return name; 119 } 120 121 126 public void setVisible(boolean val) { 127 setVisible(val, false); 128 } 129 130 136 public void setVisible(boolean val, boolean recurse) { 137 visible = val; 138 if (recurse) { 139 Iterator it = children.iterator(); 140 while (it.hasNext()) { 141 BContainer child = (BContainer) it.next(); 142 if (child!=null && child instanceof BComponent) { 143 BComponent wcomp = (BComponent) child; 144 wcomp.setVisible(val, recurse); 145 } 146 } 147 } 148 invalidate(); 149 } 150 151 156 public boolean isVisible() { 157 return visible; 158 } 159 160 165 public void setEnabled(boolean val) { 166 setEnabled(val, false); 167 } 168 169 175 public void setEnabled(boolean val, boolean recurse) { 176 enabled = val; 177 if (recurse) { 178 Iterator it = children.iterator(); 179 while (it.hasNext()) { 180 BContainer child = (BContainer) it.next(); 181 if (child!=null && child instanceof BComponent) { 182 BComponent wcomp = (BComponent) child; 183 wcomp.setEnabled(val, recurse); 184 } 185 } 186 } 187 invalidate(); 188 } 189 190 195 public boolean isEnabled() { 196 return enabled; 197 } 198 199 206 public void setView(View view) { 207 removeAllViews(); 208 addView(view); 209 } 210 211 216 public void addView(View view) { 217 if (view==null || (views!=null && views.contains(view))) return; 219 if (views==null) { 220 views = new ArrayList(); 221 } 222 views.add(view); 224 invalidate(); 225 } 226 227 232 public boolean removeView(View view) { 233 if (view==null) return false; 234 invalidate(); 235 return (views!=null && views.remove(view)); 237 } 238 239 242 public void removeAllViews() { 243 if (views!=null) views.clear(); 244 invalidate(); 245 } 246 247 253 public List getViews() { 254 if (views!=null) { 265 return new ArrayList(views); 266 } 267 return null; 268 } 270 271 protected View getFirstView() { 272 if (views!=null && views.size()>0) { 274 return (View) views.get(0); 275 } 276 277 View firstView = null; 279 if (children!=null) { 280 Iterator it = children.iterator(); 281 while (it.hasNext()) { 282 Object o = it.next(); 283 if (o instanceof BComponent) { 284 firstView = ((BComponent) o).getFirstView(); 285 if (firstView!=null) break; 286 } 287 } 288 } 289 return firstView; 290 } 291 292 293 294 306 public void setAttr(Object attr, Object val) { 307 if (attrs==null) attrs = new TreeMap(); 309 attrs.put(attr,val); 310 invalidate(); 311 } 312 313 327 public Object getAttr(Object attr) { 328 if (attrs==null) return null; 329 return attrs.get(attr); 330 } 331 332 337 public Map getAttrMap() { 338 if (attrs==null) return null; 339 return new TreeMap(attrs); 341 } 342 343 360 public void setRenderStrategy(RenderStrategy irs) { 361 rs = irs; 362 } 363 364 370 public RenderStrategy getRenderStrategy() { 371 if (rs!=null) return rs; 372 else if (parent==null) return RenderStrategy.DEFAULT_RENDER_STRATEGY; 373 else if (parent instanceof BComponent) return ((BComponent) parent).getRenderStrategy(); 374 else return RenderStrategy.DEFAULT_RENDER_STRATEGY; 375 } 376 377 385 public void render(ViewContext vc) throws RenderException { 386 render(vc, 0); 387 } 388 389 protected void render(ViewContext vc, int depth) throws RenderException { 390 391 Iterator it = null; 393 if (!validated) { 394 try { 395 if (logger.isInfoEnabled()) logger.info("preparing to render comp "+this.toRef()); 397 preRender(vc, depth); 399 400 if (logger.isInfoEnabled()) logger.info("adding in temp views "+this.toRef()); 402 if (tempViews!=null) { 403 if (views==null) views = new ArrayList(); 405 views.addAll(tempViews); 406 } 407 408 if (logger.isDebugEnabled()) logger.debug("rendering our own view in comp "+this.toRef()); 410 View view = null; 411 it = views.iterator(); 412 while (it.hasNext()) { 413 view = (View) it.next(); 414 if (view==null) continue; 415 try { 416 if (view.getNode() instanceof Element) { 417 Element el = (Element) view.getNode(); 418 419 if (isVisible()) { 421 el.removeAttribute(VISIBILITY_MARKER); 422 } else { 423 el.setAttribute(VISIBILITY_MARKER, "false"); 424 return; } 426 427 if (attrs!=null) { 429 Iterator it2 = attrs.keySet().iterator(); 430 while (it2.hasNext()) { 431 Object key = it2.next(); 432 Object val = attrs.get(key); 433 if (val==null) { 449 el.removeAttribute(key.toString().trim()); 450 } else { 451 el.setAttribute(key.toString().trim(), val.toString()); 452 } 453 } 455 } 456 } 457 458 vc.putState(ViewContext.ELEMENT_FACTORY, view.getElementFactory()); 460 renderView(view, vc, depth+1); 461 } catch (RenderException e) { 462 logger.warn("RenderException:"+e+" for View:"+view, e); 464 } 465 } 466 467 if (logger.isDebugEnabled()) logger.debug("rendering our children in comp "+this.toRef()+(this.hasChildren() ? "" : "(n/a)")); 469 BContainer child = null; 470 it = children.iterator(); 471 while (it.hasNext()) { 472 child = (BContainer) it.next(); 473 if (child==null) continue; 474 try { 475 if (child instanceof BComponent) { 476 BComponent wcomp = (BComponent) child; 477 if (wcomp.supports(vc)) { 478 wcomp.render(vc, depth+1); 479 } 480 } 481 } catch (RenderException e) { 482 logger.warn("RenderException:"+e+" for Child:"+child, e); 484 } 485 } 486 } finally { 487 if (logger.isDebugEnabled()) logger.debug("cleaning up after render in comp "+this.toRef()+(this.hasChildren() ? "" : "(n/a)")); 489 postRender(vc, depth); 491 492 removeAllStepChildren(); 494 495 if (tempViews!=null) 497 if (tempViews!=null) { 498 it = tempViews.iterator(); 499 while (it.hasNext()) { 500 View tempView = (View) it.next(); 501 this.removeView(tempView); 502 } 503 tempViews = null; 504 } 505 506 validated = true; 508 } 509 } 510 511 if (logger.isInfoEnabled()) logger.info("rendering complete in comp "+this.toRef()); 512 } 513 514 518 522 protected void preRender(ViewContext vc, int depth) { 523 526 } 528 529 536 protected final void renderView (View view, ViewContext vc) throws RenderException { 537 renderView(view, vc, 0); 538 } 539 540 protected void renderView (View view, ViewContext vc, int depth) throws RenderException { 541 if (logger.isInfoEnabled()) logger.info("rendering view: "+view); 545 546 try { 548 Renderer r = getRenderer(view); 549 r.renderComponent(this, view, vc); 550 551 } catch (DOMException e) { 552 logger.warn("DOM Error:", e); 553 throw new DOMAccessException("Error rendering component in view:"+e, e); 554 } 555 } 557 558 563 567 protected void postRender(ViewContext vc, int depth) { 568 571 } 573 574 581 public boolean supports(ViewContext vc) { 582 return true; 583 } 584 585 } 586
| Popular Tags
|