1 24 package org.riotfamily.forms.element; 25 26 import java.io.PrintWriter ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 30 import org.riotfamily.common.markup.Html; 31 import org.riotfamily.common.markup.TagWriter; 32 import org.riotfamily.forms.CompositeElement; 33 import org.riotfamily.forms.Element; 34 import org.riotfamily.forms.TemplateUtils; 35 36 37 40 public class TemplateElement extends CompositeElement { 41 42 private Map renderModel = new HashMap (); 43 44 private String template; 45 46 public TemplateElement() { 47 this("element"); 48 } 49 50 public TemplateElement(String modelKey) { 51 template = TemplateUtils.getTemplatePath(this); 52 setAttribute(modelKey, this); 53 } 54 55 protected void addComponent(String key, Element element) { 56 addComponent(element); 57 setAttribute(key, element); 58 } 59 60 protected Map getRenderModel() { 61 return renderModel; 62 } 63 64 public void setAttribute(String key, Object value) { 65 renderModel.put(key,value); 66 } 67 68 public Object getAttribute(String key) { 69 return renderModel.get(key); 70 } 71 72 75 protected final String getTemplate() { 76 return template; 77 } 78 79 86 public final void setTemplate(String name) { 87 this.template = name; 88 } 89 90 protected void renderComponents(PrintWriter writer) { 91 if (isSurroundBySpan()) { 92 TagWriter spanTag = new TagWriter(writer); 93 spanTag.start(Html.SPAN).attribute(Html.COMMON_ID, getId()).body(); 94 renderTemplate(writer); 95 spanTag.end(); 96 } 97 else { 98 renderTemplate(writer); 99 } 100 } 101 102 protected void renderTemplate(PrintWriter writer) { 103 getFormContext().getTemplateRenderer().render( 104 template, renderModel, writer); 105 } 106 } 107 | Popular Tags |