KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > field > TextFieldTag


1 package fr.improve.struts.taglib.layout.field;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import org.apache.struts.taglib.html.BaseHandlerTag;
6 import org.apache.struts.taglib.html.TextTag;
7 import org.apache.struts.util.ResponseUtils;
8
9 import fr.improve.struts.taglib.layout.el.Expression;
10 import fr.improve.struts.taglib.layout.formatter.FormatException;
11 import fr.improve.struts.taglib.layout.util.TagUtils;
12 /**
13  * A simple text field tag, but that can use a formatter to customized the displayed value.
14  * @author: Jean-Noël Ribette
15  */

16 public class TextFieldTag extends AbstractFieldTag {
17     private TextTag textFieldTag = new org.apache.struts.taglib.html.TextTag();
18     
19     /**
20      * Inspect formatter.
21      */

22     protected String JavaDoc format;
23     
24     /**
25      * Edit formater
26      */

27     protected String JavaDoc editFormat;
28     
29     protected String JavaDoc jspFormat;
30     protected String JavaDoc jspEditFormat;
31     
32     /**
33      * This method is called before displaying the value.
34      * This is the place to write something before the value, and set the fieldTag value.
35      * @return true - process the tag<br>
36      * false - skip the tag
37      */

38     protected boolean doBeforeValue() throws javax.servlet.jsp.JspException JavaDoc {
39         fieldTag = textFieldTag;
40         return true;
41     }
42     /**
43      * Return the value(s) that will be displayed.
44      */

45     protected java.lang.Object JavaDoc getFieldValue() throws JspException JavaDoc {
46         Object JavaDoc lc_value = super.getFieldValue();
47         if (lc_value != null) {
48             switch (getFieldDisplayMode()) {
49                 case MODE_EDIT:
50                     if (editFormat!=null) try {
51                         lc_value = getSkin().getFormatter().format(lc_value, editFormat, pageContext);
52                     } catch (FormatException fe) {
53                         TagUtils.saveException(pageContext, fe);
54                         throw new JspException JavaDoc("Format " + editFormat + "failed: " + fe.getMessage());
55                     }
56                     break;
57                 default:
58                     if (format!=null) try {
59                         lc_value = getSkin().getFormatter().format(lc_value, format, pageContext);
60                     } catch (FormatException fe) {
61                         TagUtils.saveException(pageContext, fe);
62                         throw new JspException JavaDoc("Format " + format + "failed: " + fe.getMessage());
63                     }
64                     break;
65             }
66         }
67         return lc_value;
68     }
69
70     public int doStartInspectField() throws JspException JavaDoc {
71         // Maybe do something before dispaying the value.
72
if (!doBeforeValue())
73             return SKIP_BODY;
74
75         Object JavaDoc lc_value = getFieldValue();
76         if (lc_value != null) {
77             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
78             buffer.append("<span class=\"");
79             buffer.append(styleClass);
80             buffer.append("\">");
81             if (format == null) {
82                 if (filter) {
83                     buffer.append(ResponseUtils.filter(lc_value.toString()));
84                 } else {
85                     buffer.append(lc_value.toString());
86                 }
87             } else {
88                 buffer.append(lc_value);
89             }
90             buffer.append("</span>");
91             TagUtils.write(pageContext, buffer.toString());
92         }
93
94         doAfterValue();
95
96         return EVAL_BODY_INCLUDE;
97     }
98
99     public void release() {
100         super.release();
101         format = null;
102         editFormat = null;
103     }
104     public void setType(String JavaDoc type) {
105         format = type;
106     }
107     public void setEditType(String JavaDoc type) {
108         editFormat = type;
109     }
110     public String JavaDoc getType() {
111         return format;
112     }
113     
114     protected void copyProperties(BaseHandlerTag in_dest) throws JspException JavaDoc {
115         super.copyProperties(in_dest);
116         textFieldTag.setCols(getCols());
117         textFieldTag.setMaxlength(getMaxlength());
118         textFieldTag.setProperty(getProperty());
119         textFieldTag.setRows(getRows());
120         textFieldTag.setValue(getValue());
121         textFieldTag.setAccept(getAccept());
122         textFieldTag.setName(getName());
123     }
124
125     protected void initDynamicValues() {
126         super.initDynamicValues();
127         
128         // format is an EL.
129
jspFormat = format;
130         format = Expression.evaluate(format, pageContext);
131         
132         // editoFormat also.
133
jspEditFormat = editFormat;
134         editFormat = Expression.evaluate(editFormat, pageContext);
135     }
136     
137     protected void reset() {
138         // format is an EL
139
format = jspFormat;
140         jspFormat = null;
141         
142         // editoFormat also.
143
editFormat = jspEditFormat;
144         jspEditFormat = null;
145         
146         super.reset();
147     }
148 }
149
Popular Tags