KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > util > BasicField


1 package fr.improve.struts.taglib.layout.util;
2
3 import java.util.List JavaDoc;
4 import java.util.MissingResourceException JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7 import javax.servlet.jsp.PageContext JavaDoc;
8
9 import org.apache.struts.Globals;
10
11 import fr.improve.struts.taglib.layout.field.AbstractLayoutFieldTag;
12 import fr.improve.struts.taglib.layout.skin.Skin;
13
14 /**
15  * Default implementation of the FieldInterface interface.
16  * @author jer80876
17  */

18 public class BasicField implements FieldInterface {
19     /**
20      * Default implementation uses 2 columns: one for the label, one for the value.
21      */

22     public int getColumnNumber() {
23         return 2;
24     }
25     
26     /**
27      * Start to display the field.
28      */

29     public void doStartField(StringBuffer JavaDoc in_buffer, AbstractLayoutFieldTag in_tag, String JavaDoc in_label, Object JavaDoc in_value) throws JspException JavaDoc {
30         // Start the label cell.
31
in_buffer.append("<th valign=\"top");
32         
33         // Set the css class
34
if (in_tag.getStyleClass()!=null) {
35             in_buffer.append("\" class=\"");
36             in_buffer.append(in_tag.getStyleClass());
37         }
38         
39         // Set the layout id of the cell.
40
String JavaDoc lc_layoutId = in_tag.getLayoutId();
41         if (lc_layoutId!=null) {
42             in_buffer.append("\" id=\"");
43             in_buffer.append(lc_layoutId);
44             in_buffer.append("L");
45         }
46         
47         // Render a span in_tag as some browser don't support to set the style in the <td> in_tag.
48
in_buffer.append("\"><span");
49         if (in_tag.getStyleClass()!=null) {
50             in_buffer.append(" class=\"");
51             in_buffer.append(in_tag.getStyleClass());
52             in_buffer.append("\"");
53         }
54         
55         // Render a tooltip.
56
if (in_tag.getHint()!=null) {
57             in_buffer.append(" title=\"");
58             in_buffer.append(LayoutUtils.getLabel(in_tag.getPageContext(), in_tag.getBundle(), in_tag.getHint(), null, false));
59             in_buffer.append("\"");
60         }
61         
62         // Maybe use a specific style for the label
63
if (in_tag.getStyle()!=null) {
64             in_buffer.append(" style=\"");
65             in_buffer.append(in_tag.getStyle());
66             in_buffer.append("\"");
67         }
68         in_buffer.append(">");
69         
70         // Render the label and end of the label cell.
71
if (in_label!=null) {
72             StringBuffer JavaDoc lc_label = new StringBuffer JavaDoc(in_label);
73             for (int i=0;i<lc_label.length();i++) {
74                 if (lc_label.charAt(i)==' ') lc_label.replace(i,i+1,"&nbsp;");
75             }
76             in_buffer.append(lc_label.toString());
77         } else {
78             in_buffer.append("&nbsp;");
79         }
80         in_buffer.append("</span></th>");
81
82         // Prepare to print the value: start the value cell.
83
in_buffer.append("<td valign=\"top\" class=\"");
84         in_buffer.append(in_tag.getStyleClass());
85         
86         // Set the layoutId of the cell.
87
if (lc_layoutId!=null) {
88             in_buffer.append("\" id=\"");
89             in_buffer.append(lc_layoutId);
90             in_buffer.append("F");
91         }
92         
93         // Maybe use extra style information.
94
String JavaDoc lc_style = LayoutUtils.getSkin(in_tag.getPageContext().getSession()).getFormUtils().getFieldValueStyle(in_tag.getPageContext());
95         if (lc_style!=null) {
96             in_buffer.append("\" style=\"");
97             in_buffer.append(lc_style);
98         }
99         in_buffer.append("\">");
100     }
101     
102     public void doEndField(StringBuffer JavaDoc in_buffer, AbstractLayoutFieldTag in_tag, Object JavaDoc in_value) throws JspException JavaDoc{
103         // Display an image if is Required is set.
104
String JavaDoc lc_property = in_tag.getProperty();
105         PageContext JavaDoc lc_pageContext = in_tag.getPageContext();
106         Skin lc_skin = LayoutUtils.getSkin(lc_pageContext.getSession());
107         
108         boolean lc_valueSet = in_tag.isFill(in_value);
109                 
110         if (in_tag.isRequired() && !lc_valueSet) {
111             in_buffer.append("<img name=\"" + lc_property + "required\" SRC=\"" + lc_skin.getImageDirectory(lc_pageContext.getRequest()) + "/ast.gif\" alt=\"required\">");
112         } else {
113             in_buffer.append("<img name=\"" + lc_property + "required\" SRC=\"" + lc_skin.getImageDirectory(lc_pageContext.getRequest()) + "/clearpixel.gif\" alt=\"required\">");
114         }
115         
116         // end the field
117
in_buffer.append("</td>");
118         in_tag = null;
119     }
120
121     public void doStartErrors(StringBuffer JavaDoc in_buffer, AbstractLayoutFieldTag in_tag, List JavaDoc in_errors) {
122         in_buffer.append("<table><tr><td class=\"");
123         in_buffer.append(in_tag.getErrorStyleClass());
124         in_buffer.append("\">");
125     }
126
127     public void doEndErrors(StringBuffer JavaDoc in_buffer, AbstractLayoutFieldTag in_tag, List JavaDoc in_errors) throws JspException JavaDoc{
128         boolean useErrorsMessages = false;
129         try {
130             useErrorsMessages = "true".equalsIgnoreCase(LayoutUtils.getSkin(in_tag.getPageContext().getSession()).getProperty("error.format"));
131         } catch (MissingResourceException JavaDoc e) {
132             // do nothing.
133
}
134         boolean headerPresent = useErrorsMessages && TagUtils.present(in_tag.getPageContext(), in_tag.getBundle(), Globals.LOCALE_KEY, "errors.header");
135         boolean footerPresent = useErrorsMessages && TagUtils.present(in_tag.getPageContext(), in_tag.getBundle(), Globals.LOCALE_KEY, "errors.footer");
136         boolean prefixPresent = useErrorsMessages && TagUtils.present(in_tag.getPageContext(), in_tag.getBundle(), Globals.LOCALE_KEY, "errors.prefix");
137         boolean suffixPresent = useErrorsMessages && TagUtils.present(in_tag.getPageContext(), in_tag.getBundle(), Globals.LOCALE_KEY, "errors.suffix");
138
139         if (headerPresent && in_errors.size()>0) {
140             in_buffer.append(TagUtils.message(in_tag.getPageContext(),in_tag.getBundle(),Globals.LOCALE_KEY,"errors.header"));
141         }
142         for (int i=0;i<in_errors.size();i++) {
143             if (prefixPresent) {
144                 in_buffer.append(TagUtils.message(in_tag.getPageContext(),in_tag.getBundle(),Globals.LOCALE_KEY,"errors.prefix"));
145             }
146             in_buffer.append(in_errors.get(i));
147             if (suffixPresent) {
148                 in_buffer.append(TagUtils.message(in_tag.getPageContext(),in_tag.getBundle(),Globals.LOCALE_KEY,"errors.suffix"));
149             } else {
150                 if (i<in_errors.size()) in_buffer.append("<br />");
151             }
152         }
153         if (suffixPresent && in_errors.size()>0) {
154             in_buffer.append(TagUtils.message(in_tag.getPageContext(),in_tag.getBundle(),Globals.LOCALE_KEY,"errors.footer"));
155         }
156         in_buffer.append("</td></tr></table>");
157     }
158 }
Popular Tags