KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > forms > element > TemplateElement


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.forms.element;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
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 /**
38  * CompositeElement that is rendered using a template.
39  */

40 public class TemplateElement extends CompositeElement {
41     
42     private Map JavaDoc renderModel = new HashMap JavaDoc();
43     
44     private String JavaDoc template;
45     
46     public TemplateElement() {
47         this("element");
48     }
49     
50     public TemplateElement(String JavaDoc modelKey) {
51         template = TemplateUtils.getTemplatePath(this);
52         setAttribute(modelKey, this);
53     }
54         
55     protected void addComponent(String JavaDoc key, Element element) {
56         addComponent(element);
57         setAttribute(key, element);
58     }
59     
60     protected Map JavaDoc getRenderModel() {
61         return renderModel;
62     }
63     
64     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
65         renderModel.put(key,value);
66     }
67     
68     public Object JavaDoc getAttribute(String JavaDoc key) {
69         return renderModel.get(key);
70     }
71     
72     /**
73      * Returns the name of the template that is used to render the element.
74      */

75     protected final String JavaDoc getTemplate() {
76         return template;
77     }
78
79     /**
80      * Sets the name of the template that is used to render the element.
81      *
82      * @param name name of the template to use
83      * @see #renderInternal(PrintWriter)
84      * @see org.riotfamily.forms.TemplateRenderer
85      */

86     public final void setTemplate(String JavaDoc name) {
87         this.template = name;
88     }
89     
90     protected void renderComponents(PrintWriter JavaDoc 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 JavaDoc writer) {
103         getFormContext().getTemplateRenderer().render(
104                 template, renderModel, writer);
105     }
106 }
107
Popular Tags