KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > gui > controlx > impl > TemplatedRenderer


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.gui.controlx.impl;
14
15 import info.magnolia.cms.gui.controlx.Control;
16 import info.magnolia.cms.gui.controlx.Renderer;
17 import info.magnolia.cms.i18n.Messages;
18 import info.magnolia.cms.i18n.MessagesManager;
19 import info.magnolia.cms.util.FreeMarkerUtil;
20
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.apache.commons.lang.StringUtils;
25
26
27 /**
28  * Used the classname by default to get the template to render.
29  * @author Philipp Bracher
30  * @version $Revision: 6341 $ ($Author: philipp $)
31  */

32 public class TemplatedRenderer implements Renderer {
33
34     private String JavaDoc templateName;
35
36     /**
37      * Uses the controls class name to construct the template name
38      */

39     public TemplatedRenderer() {
40     }
41
42     /**
43      * @param templateName
44      */

45     public TemplatedRenderer(String JavaDoc templateName) {
46         this.templateName = templateName;
47     }
48
49     /**
50      * Render the using the template. The control is passed under the name 'this' and the renderer class is passed under
51      * the name 'renderer'
52      */

53     public String JavaDoc render(Control control) {
54         Map JavaDoc data = new HashMap JavaDoc();
55         data.put("this", control);
56         data.put("renderer", this);
57         return FreeMarkerUtil.process(this.getTemplateName(control), data);
58     }
59
60     /**
61      * Get the message from the renderer. Uses getMessages().
62      * @param key
63      * @return the string found, or the key if not found
64      */

65     public String JavaDoc getMessage(String JavaDoc key) {
66         return getMessages().getWithDefault(key, key);
67     }
68
69     /**
70      * Get the messages used for the rendering. By default the standard messages are returned.
71      * @return the messages
72      */

73     public Messages getMessages() {
74         return MessagesManager.getMessages();
75     }
76
77     /**
78      * @return
79      */

80     protected String JavaDoc getTemplateName(Control control) {
81         if (this.templateName == null) {
82             return "/" + StringUtils.replace(control.getClass().getName(), ".", "/") + ".html";
83         }
84
85         return this.templateName;
86     }
87
88     /**
89      * @return Returns the templateName.
90      */

91     public String JavaDoc getTemplateName() {
92         return this.templateName;
93     }
94
95     /**
96      * @param templateName The templateName to set.
97      */

98     public void setTemplateName(String JavaDoc templateName) {
99         this.templateName = templateName;
100     }
101
102 }
103
Popular Tags