KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > UIBean


1 package com.opensymphony.webwork.components;
2
3 import com.opensymphony.webwork.components.template.Template;
4 import com.opensymphony.webwork.components.template.TemplateEngine;
5 import com.opensymphony.webwork.components.template.TemplateEngineManager;
6 import com.opensymphony.webwork.components.template.TemplateRenderingContext;
7 import com.opensymphony.webwork.config.Configuration;
8 import com.opensymphony.xwork.config.ConfigurationException;
9 import com.opensymphony.xwork.util.OgnlValueStack;
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12
13 import javax.servlet.http.HttpServletRequest JavaDoc;
14 import javax.servlet.http.HttpServletResponse JavaDoc;
15 import java.io.Writer JavaDoc;
16
17 /**
18  * User: plightbo
19  * Date: Jul 1, 2005
20  * Time: 11:14:38 PM
21  */

22 public abstract class UIBean extends Component {
23     private static final Log LOG = LogFactory.getLog(UIBean.class);
24
25     protected HttpServletRequest JavaDoc request;
26     protected HttpServletResponse JavaDoc response;
27
28     public UIBean(OgnlValueStack stack, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
29         super(stack);
30         this.request = request;
31         this.response = response;
32     }
33
34     // The template to use, overrides the default one.
35
protected String JavaDoc template;
36
37     // templateDir and theme attributes
38
protected String JavaDoc templateDir;
39     protected String JavaDoc theme;
40
41     protected String JavaDoc cssClass;
42     protected String JavaDoc cssStyle;
43     protected String JavaDoc disabled;
44     protected String JavaDoc label;
45     protected String JavaDoc labelPosition;
46     protected String JavaDoc name;
47     protected String JavaDoc required;
48     protected String JavaDoc tabindex;
49     protected String JavaDoc value;
50
51     // HTML scripting events attributes
52
protected String JavaDoc onclick;
53     protected String JavaDoc ondblclick;
54     protected String JavaDoc onmousedown;
55     protected String JavaDoc onmouseup;
56     protected String JavaDoc onmouseover;
57     protected String JavaDoc onmousemove;
58     protected String JavaDoc onmouseout;
59     protected String JavaDoc onfocus;
60     protected String JavaDoc onblur;
61     protected String JavaDoc onkeypress;
62     protected String JavaDoc onkeydown;
63     protected String JavaDoc onkeyup;
64     protected String JavaDoc onselect;
65     protected String JavaDoc onchange;
66
67     public void end(Writer JavaDoc writer) {
68         evaluateParams();
69         try {
70             mergeTemplate(writer, buildTemplateName(template, getDefaultTemplate()));
71         } catch (Exception JavaDoc e) {
72             e.printStackTrace();
73         }
74
75         super.end(writer);
76     }
77
78     /**
79      * A contract that requires each concrete UI Tag to specify which template should be used as a default. For
80      * example, the CheckboxTab might return "checkbox.vm" while the RadioTag might return "radio.vm". This value
81      * <strong>not</strong> begin with a '/' unless you intend to make the path absolute rather than relative to the
82      * current theme.
83      *
84      * @return The name of the template to be used as the default.
85      */

86     protected abstract String JavaDoc getDefaultTemplate();
87
88     protected Template buildTemplateName(String JavaDoc myTemplate, String JavaDoc myDefaultTemplate) {
89         String JavaDoc template = myDefaultTemplate;
90
91         if (myTemplate != null) {
92             template = findString(myTemplate);
93         }
94
95         String JavaDoc templateDir = getTemplateDir();
96         String JavaDoc theme = getTheme();
97
98         return new Template(templateDir, theme, template);
99
100     }
101
102     protected void mergeTemplate(Writer JavaDoc writer, Template template) throws Exception JavaDoc {
103         final TemplateEngine engine = TemplateEngineManager.getTemplateEngine(template);
104         if (engine == null) {
105             throw new ConfigurationException("Unable to find a TemplateEngine for template " + template);
106         }
107
108         if (LOG.isDebugEnabled()) {
109             LOG.debug("Rendering template " + template);
110         }
111
112         final TemplateRenderingContext context = new TemplateRenderingContext(template, writer, getStack(), getParameters(), this);
113         engine.renderTemplate(context);
114     }
115
116     public String JavaDoc getTemplateDir() {
117         String JavaDoc templateDir = null;
118
119         if (this.templateDir != null) {
120             templateDir = findString(this.templateDir);
121         }
122
123         // If templateDir is not explicitly given,
124
// try to find attribute which states the dir set to use
125
if ((templateDir == null) || (templateDir.equals(""))) {
126             templateDir = (String JavaDoc) stack.findValue("#attr.templateDir");
127         }
128
129         // Default template set
130
if ((templateDir == null) || (templateDir.equals(""))) {
131             templateDir = Configuration.getString("webwork.ui.templateDir");
132         }
133
134         // Defaults to 'template'
135
if ((templateDir == null) || (templateDir.equals(""))) {
136             templateDir = "template";
137         }
138
139         return templateDir;
140     }
141
142     public String JavaDoc getTheme() {
143         String JavaDoc theme = null;
144
145         if (this.theme != null) {
146             theme = findString(this.theme);
147         }
148
149         // If theme set is not explicitly given,
150
// try to find attribute which states the theme set to use
151
if ((theme == null) || (theme.equals(""))) {
152             theme = (String JavaDoc) stack.findValue("#attr.theme");
153         }
154
155         // Default theme set
156
if ((theme == null) || (theme.equals(""))) {
157             theme = Configuration.getString("webwork.ui.theme");
158         }
159
160         return theme;
161     }
162
163     public void evaluateParams() {
164         addParameter("templateDir", getTemplateDir());
165         addParameter("theme", getTheme());
166
167         Object JavaDoc name = null;
168
169         if (this.name != null) {
170             name = findString(this.name);
171             addParameter("name", name);
172         }
173
174         if (label != null) {
175             addParameter("label", findString(label));
176         }
177
178         if (labelPosition != null) {
179             addParameter("labelposition", findString(labelPosition));
180         }
181
182         if (required != null) {
183             addParameter("required", findValue(required, Boolean JavaDoc.class));
184         }
185
186         if (disabled != null) {
187             addParameter("disabled", findValue(disabled, Boolean JavaDoc.class));
188         }
189
190         if (tabindex != null) {
191             addParameter("tabindex", findString(tabindex));
192         }
193
194         if (onclick != null) {
195             addParameter("onclick", findString(onclick));
196         }
197
198         if (ondblclick != null) {
199             addParameter("ondblclick", findString(ondblclick));
200         }
201
202         if (onmousedown != null) {
203             addParameter("onmousedown", findString(onmousedown));
204         }
205
206         if (onmouseup != null) {
207             addParameter("onmouseup", findString(onmouseup));
208         }
209
210         if (onmouseover != null) {
211             addParameter("onmouseover", findString(onmouseover));
212         }
213
214         if (onmousemove != null) {
215             addParameter("onmousemove", findString(onmousemove));
216         }
217
218         if (onmouseout != null) {
219             addParameter("onmouseout", findString(onmouseout));
220         }
221
222         if (onfocus != null) {
223             addParameter("onfocus", findString(onfocus));
224         }
225
226         if (onblur != null) {
227             addParameter("onblur", findString(onblur));
228         }
229
230         if (onkeypress != null) {
231             addParameter("onkeypress", findString(onkeypress));
232         }
233
234         if (onkeydown != null) {
235             addParameter("onkeydown", findString(onkeydown));
236         }
237
238         if (onkeyup != null) {
239             addParameter("onkeyup", findString(onkeyup));
240         }
241
242         if (onselect != null) {
243             addParameter("onselect", findString(onselect));
244         }
245
246         if (onchange != null) {
247             addParameter("onchange", findString(onchange));
248         }
249
250         if (cssClass != null) {
251             addParameter("cssClass", findString(cssClass));
252         }
253
254         if (cssStyle != null) {
255             addParameter("cssStyle", findString(cssStyle));
256         }
257
258         if (evaluateNameValue()) {
259             final Class JavaDoc valueClazz = getValueClassType();
260
261             if (valueClazz != null) {
262                 if (value != null) {
263                     addParameter("nameValue", findValue(value, valueClazz));
264                 } else if (name != null) {
265                     String JavaDoc expr = name.toString();
266                     if (ALT_SYNTAX) {
267                         expr = "%{" + expr + "}";
268                     }
269
270                     addParameter("nameValue", findValue(expr, valueClazz));
271                 }
272             } else {
273                 if (value != null) {
274                     addParameter("nameValue", findValue(value));
275                 } else if (name != null) {
276                     addParameter("nameValue", findValue(name.toString()));
277                 }
278             }
279         }
280
281         final Form form = (Form) findAncestor(Form.class);
282
283         if (id != null) {
284             addParameter("id", id);
285         } else if (form != null) {
286             addParameter("id", form.getParameters().get("id") + "_" + name);
287         }
288
289         if (form != null) {
290             addParameter("form", form.getParameters());
291         }
292
293         evaluateExtraParams();
294     }
295
296     protected void evaluateExtraParams() {
297     }
298
299     protected boolean evaluateNameValue() {
300         return true;
301     }
302
303     protected Class JavaDoc getValueClassType() {
304         return String JavaDoc.class;
305     }
306
307     public void setTheme(String JavaDoc theme) {
308         this.theme = theme;
309     }
310
311     public String JavaDoc getTemplate() {
312         return template;
313     }
314
315     public void setTemplate(String JavaDoc template) {
316         this.template = template;
317     }
318
319     public void setCssClass(String JavaDoc cssClass) {
320         this.cssClass = cssClass;
321     }
322
323     public void setCssStyle(String JavaDoc cssStyle) {
324         this.cssStyle = cssStyle;
325     }
326
327     public void setDisabled(String JavaDoc disabled) {
328         this.disabled = disabled;
329     }
330
331     public void setLabel(String JavaDoc label) {
332         this.label = label;
333     }
334
335     public void setLabelPosition(String JavaDoc labelPosition) {
336         this.labelPosition = labelPosition;
337     }
338
339     public void setName(String JavaDoc name) {
340         this.name = name;
341     }
342
343     public void setRequired(String JavaDoc required) {
344         this.required = required;
345     }
346
347     public void setTabindex(String JavaDoc tabindex) {
348         this.tabindex = tabindex;
349     }
350
351     public void setValue(String JavaDoc value) {
352         this.value = value;
353     }
354
355     public void setOnclick(String JavaDoc onclick) {
356         this.onclick = onclick;
357     }
358
359     public void setOndblclick(String JavaDoc ondblclick) {
360         this.ondblclick = ondblclick;
361     }
362
363     public void setOnmousedown(String JavaDoc onmousedown) {
364         this.onmousedown = onmousedown;
365     }
366
367     public void setOnmouseup(String JavaDoc onmouseup) {
368         this.onmouseup = onmouseup;
369     }
370
371     public void setOnmouseover(String JavaDoc onmouseover) {
372         this.onmouseover = onmouseover;
373     }
374
375     public void setOnmousemove(String JavaDoc onmousemove) {
376         this.onmousemove = onmousemove;
377     }
378
379     public void setOnmouseout(String JavaDoc onmouseout) {
380         this.onmouseout = onmouseout;
381     }
382
383     public void setOnfocus(String JavaDoc onfocus) {
384         this.onfocus = onfocus;
385     }
386
387     public void setOnblur(String JavaDoc onblur) {
388         this.onblur = onblur;
389     }
390
391     public void setOnkeypress(String JavaDoc onkeypress) {
392         this.onkeypress = onkeypress;
393     }
394
395     public void setOnkeydown(String JavaDoc onkeydown) {
396         this.onkeydown = onkeydown;
397     }
398
399     public void setOnkeyup(String JavaDoc onkeyup) {
400         this.onkeyup = onkeyup;
401     }
402
403     public void setOnselect(String JavaDoc onselect) {
404         this.onselect = onselect;
405     }
406
407     public void setOnchange(String JavaDoc onchange) {
408         this.onchange = onchange;
409     }
410 }
411
Popular Tags