001: /*
002: * de.jwic.renderer.velocity.BasicControlRenderer
003: * $Id: BasicControlRenderer.java,v 1.1 2006/01/16 08:31:12 lordsam Exp $
004: */
005: package de.jwic.renderer.velocity;
006:
007: import java.io.PrintWriter;
008: import java.util.Iterator;
009:
010: import org.apache.velocity.Template;
011: import org.apache.velocity.VelocityContext;
012:
013: import de.jwic.base.Control;
014: import de.jwic.base.IControlContainer;
015: import de.jwic.base.IControlRenderer;
016: import de.jwic.base.JWicRuntime;
017: import de.jwic.base.RenderContext;
018: import de.jwic.renderer.util.ChildRenderer;
019:
020: /**
021: *
022: * @author Florian Lippisch
023: * @version $Revision: 1.1 $
024: */
025: public class BasicControlRenderer extends BaseVelocityRenderer {
026:
027: /**
028: * @throws Exception
029: */
030: public BasicControlRenderer() throws Exception {
031: super ();
032: }
033:
034: /* (non-Javadoc)
035: * @see de.jwic.base.ControlRenderer#renderControl(de.jwic.base.Control, java.io.Writer)
036: */
037: public void renderControl(Control control, RenderContext context) {
038:
039: PrintWriter writer = context.getWriter();
040: try {
041:
042: control.setRequireRedraw(false);
043:
044: boolean isContainer = control instanceof IControlContainer;
045: String tplName = control.getTemplateName();
046: Template tpl = null;
047: if (tplName != null) {
048: tpl = getTemplate(tplName);
049: }
050: String ctrlDivID = "ctrl_" + control.getControlID();//.replace('.', '_');
051:
052: if (!control.isVisible()) {
053: // JBO 2005-09-06: render span tag for invisible controls, better ajax support
054: writer.print("<span><span id=\"" + ctrlDivID + "\">");
055: writer.print("</span></span>");
056: return;
057: }
058:
059: if (tpl != null) {
060: log.debug("Rendering '" + control.getControlID()
061: + "' using template '" + tplName + "'");
062: VelocityContext vCtx = createContext(control);
063: vCtx.put("control", control);
064: if (isContainer) {
065: vCtx.put("insert", new ChildRenderer(
066: (IControlContainer) control, context));
067: }
068:
069: // FLI 2005-05-24: To emulate the outerHTML setter method in a mozilla
070: // browser, a <span> tag requires a single parent, wich is another span tag.
071:
072: writer.print("<span><span id=\"" + ctrlDivID + "\">");
073: tpl.merge(vCtx, writer);
074: writer.print("</span></span>");
075: } else {
076: if (!isContainer) {
077: writer.print("[ Template '" + tplName
078: + "' not found for: "
079: + control.getControlID() + "]");
080: } else {
081: // no template -> render all controls "one by one"
082: log.debug("Rendering '" + control.getControlID()
083: + "' without template.");
084: writer.print("<span><span id=\"" + ctrlDivID
085: + "\">");
086: for (Iterator it = ((IControlContainer) control)
087: .getControls(); it.hasNext();) {
088: Control child = (Control) it.next();
089: IControlRenderer renderer = JWicRuntime
090: .getRenderer(child.getRendererId());
091: renderer.renderControl(child, context);
092: }
093: writer.print("</span></span>");
094: }
095: }
096:
097: } catch (Exception e) {
098: writer.print("ERR (" + e + ")");
099: }
100:
101: }
102: }
|