001: /*
002: * de.jwic.renderer.velocity.OuterContainerRenderer
003: * $Id: OuterContainerRenderer.java,v 1.2 2007/04/19 12:41:43 lordsam Exp $
004: */
005: package de.jwic.renderer.velocity;
006:
007: import java.io.PrintWriter;
008:
009: import org.apache.velocity.Template;
010: import org.apache.velocity.VelocityContext;
011:
012: import de.jwic.base.Control;
013: import de.jwic.base.IControlContainer;
014: import de.jwic.base.IControlRenderer;
015: import de.jwic.base.IOuterLayout;
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.2 $
024: */
025: public class OuterContainerRenderer extends BaseVelocityRenderer {
026:
027: private IControlRenderer containerRenderer = null;
028:
029: public class OuterContentRenderer {
030:
031: private Control ctrl = null;
032: private RenderContext context = null;
033: IControlRenderer renderer = null;
034:
035: public OuterContentRenderer(Control control,
036: IControlRenderer renderer, RenderContext context) {
037: this .ctrl = control;
038: this .context = context;
039: this .renderer = renderer;
040: }
041:
042: /**
043: * Render the root control.
044: * @return
045: */
046: public void render() {
047:
048: renderer.renderControl(ctrl, context);
049:
050: }
051:
052: }
053:
054: /**
055: * Default constructor.
056: * @throws Exception
057: */
058: public OuterContainerRenderer() throws Exception {
059: super ();
060: }
061:
062: /* (non-Javadoc)
063: * @see de.jwic.base.ControlRenderer#renderControl(de.jwic.base.Control, java.io.Writer)
064: */
065: public void renderControl(Control control, RenderContext context) {
066:
067: PrintWriter writer = context.getWriter();
068: try {
069: if (control instanceof IOuterLayout) {
070: control.setRequireRedraw(false); // clear redraw flag
071: String ctrlDivID = "ctrl_" + control.getControlID();//.replace('.', '_');
072:
073: if (!control.isVisible()) {
074: // JBO 2005-09-06: render span tag for invisible controls, better ajax support
075: writer.print("<span><span id=\"" + ctrlDivID
076: + "\">");
077: writer.print("</span></span>");
078: return;
079: }
080:
081: boolean isContainer = control instanceof IControlContainer;
082: String tplName = ((IOuterLayout) control)
083: .getOuterTemplateName();
084: log.debug("Rendering control '"
085: + control.getControlID() + "' using template '"
086: + tplName + "'");
087: Template tpl = getTemplate(tplName);
088: VelocityContext vCtx = createContext(control);
089: vCtx.put("control", control);
090: vCtx.put("content", new OuterContentRenderer(control,
091: getContainerRenderer(), context));
092: if (isContainer) {
093: vCtx.put("insert", new ChildRenderer(
094: (IControlContainer) control, context));
095: }
096:
097: // FLI 2005-05-24: To emulate the outerHTML setter method in a mozilla
098: // browser, a <span> tag requires a single parent, wich is another span tag.
099:
100: writer.print("<span><span id=\"" + ctrlDivID + "\">");
101: tpl.merge(vCtx, writer);
102: writer.print("</span></span>");
103: } else {
104: writer.print("[Not instance of IOuterLayout: "
105: + control.getControlID() + "]");
106: }
107:
108: } catch (Exception e) {
109: writer.print("ERR (" + e + ")");
110: }
111:
112: }
113:
114: /**
115: * @return Returns the containerRenderer.
116: */
117: public IControlRenderer getContainerRenderer() {
118: if (containerRenderer == null) {
119: return JWicRuntime.getRenderer("jwic.renderer.Default");
120: }
121: return containerRenderer;
122: }
123:
124: /**
125: * @param containerRenderer The containerRenderer to set.
126: */
127: public void setContainerRenderer(IControlRenderer containerRenderer) {
128: this.containerRenderer = containerRenderer;
129: }
130: }
|