KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.field;
2
3 import java.util.List JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6
7 import org.apache.struts.taglib.html.Constants;
8 import org.apache.struts.taglib.html.FormTag;
9
10 import fr.improve.struts.taglib.layout.el.Expression;
11 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
12 import fr.improve.struts.taglib.layout.event.ErrorEvent;
13 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
14 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
15 import fr.improve.struts.taglib.layout.util.LayoutUtils;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17 /**
18  * Abstract class defining the layout of the field tags.<br>
19  * The tag renders html code looking like: &lt;tr&gt;&lt;th&gt; input field title &lt;/th&gt;&lt;td&gt; input field &lt;/td&gt;&lt;/tr&gt;.<br>
20  * <br>
21  * This class brings the support of an help message to the field that will be displayed under the field with the default css class "HELP",<br>
22  * the notion of mandatory field, and displays the error messages associated with the field.
23  *
24  * @author: Jean-Noël Ribette
25  */

26 public abstract class AbstractLayoutFieldTag extends AbstractModeFieldTag implements LayoutEventListener {
27     /**
28      * Help message key.
29      */

30     private String JavaDoc help = null;
31
32     /**
33      * Help message styleClass
34      */

35     private String JavaDoc helpStyleClass = "HELP";
36
37     /**
38      * Mandatory value. If true, display a red start after the tag.
39      */

40     private String JavaDoc isRequired = "false";
41     
42     /**
43      * Saved jsp isRequired value.
44      */

45     private String JavaDoc jspIsRequired;
46
47     /**
48      * Error styleClass
49      */

50     private String JavaDoc errorStyleClass = "ERROR";
51     
52     /**
53      * Hint message
54      */

55     private String JavaDoc hint = null;
56     
57     /**
58      * Generate the tag layout: default is true.
59      */

60     private boolean layout = true;
61     /**
62      * Tooltip for this field
63      */

64     private String JavaDoc tooltip = null;
65     
66     /**
67      * Model
68      */

69     private String JavaDoc model = null;
70     
71     /**
72      * Constant for the request attribute indicating
73      * the first error field has been focused.
74      */

75     public static final String JavaDoc FIRST_ERROR_FIELD_FOCUS = "fr.improve.struts.taglib.layout.field.AbstractLayoutFieldTag.FIRST_ERROR_FIELD_FOCUS";
76     
77 /**
78  * Prepare to display the errors.
79  */

80 protected void beginFieldError(List JavaDoc in_errors) throws JspException JavaDoc {
81     if (in_errors.size()!=0 && getSkin().getDisplayErrorMessage()) {
82         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
83         getSkin().getFieldInterface(model).doStartErrors(lc_buffer, this, in_errors);
84         TagUtils.write(pageContext, lc_buffer.toString());
85     }
86 }
87 /**
88  * Start to print the field: display the field title.
89  */

90 protected final void beginFieldLayout() throws JspException JavaDoc {
91     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92     
93     if (Boolean.FALSE.equals(new StartLayoutEvent(this, null).send())) {
94         // For compatibiliy reason, print a <tr> tag if the tag is not nested in a layout tag.
95
buffer.append("<tr>");
96     }
97     
98     // Start to display the field.
99
getSkin().getFieldInterface(model).doStartField(buffer, this, getLabel(), getFieldValue());
100         
101     // Write the buffer.
102
TagUtils.write(pageContext, buffer.toString());
103 }
104
105     protected abstract int doEndEditField() throws JspException JavaDoc;
106 protected int doEndEditMode() throws JspException JavaDoc {
107     int lc_result = doEndEditField();
108     if (layout) {
109         endFieldLayout();
110     }
111     return lc_result;
112 }
113     protected abstract int doEndInspectField() throws JspException JavaDoc;
114 protected final int doEndInspectMode() throws JspException JavaDoc {
115     int lc_result = doEndInspectField();
116     if (layout) {
117         endFieldLayout();
118     }
119     return lc_result;
120 }
121     protected abstract int doStartEditField() throws JspException JavaDoc;
122 protected int doStartEditMode() throws JspException JavaDoc {
123     List JavaDoc lc_errors = computeErrors();
124     if (layout) {
125         beginFieldLayout();
126         beginFieldError(lc_errors);
127     }
128     int lc_result = doStartEditField();
129     if (layout) {
130         endFieldError(lc_errors);
131     }
132     return lc_result;
133 }
134
135     /**
136      * Calcule et retourne la liste des erreurs associées au champ courant.
137      * S'il existe des erreurs, lance un événement ErrorEvent.
138      */

139     protected List JavaDoc computeErrors() throws JspException JavaDoc {
140          // Get the error if any
141
List JavaDoc lc_errors = retrieveErrors();
142         
143         if (lc_errors.size()>0) {
144             new ErrorEvent(this, lc_errors).send();
145             if (getSkin().getFocusFirstErrorField()) {
146                 if (pageContext.getRequest().getAttribute(FIRST_ERROR_FIELD_FOCUS)==null) {
147                     FormTag lc_formTag = (FormTag) pageContext.findAttribute(Constants.FORM_KEY);
148                     lc_formTag.setFocus(getProperty());
149                     pageContext.getRequest().setAttribute(FIRST_ERROR_FIELD_FOCUS, getProperty());
150                 }
151             }
152         }
153         
154         // Return the errors.
155
return lc_errors;
156     }
157
158
159     protected abstract int doStartInspectField() throws JspException JavaDoc;
160 protected final int doStartInspectMode() throws JspException JavaDoc {
161     if (layout) {
162         beginFieldLayout();
163     }
164     if (MODE_INSPECT==getFieldDisplayMode()) {
165         printHiddenValue(getFieldValue());
166     }
167     return doStartInspectField();
168 }
169
170 protected void endFieldError(List JavaDoc in_errors) throws JspException JavaDoc {
171     if (in_errors.size()!=0) {
172         if (getSkin().getDisplayErrorMessage()) {
173             StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
174             getSkin().getFieldInterface(model).doEndErrors(lc_buffer, this, in_errors);
175             TagUtils.write(pageContext, lc_buffer.toString());
176         }
177     }
178 }
179 /**
180  * End the field (close the html tags)
181  */

182 protected final void endFieldLayout() throws JspException JavaDoc {
183     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
184     
185     getSkin().getFieldInterface(model).doEndField(buffer, this, getFieldValue());
186
187     // Write the buffer.
188
TagUtils.write(pageContext, buffer.toString());
189     
190     // End the layout.
191
if (Boolean.FALSE.equals(new EndLayoutEvent(this, null).send())) {
192         // For compatiblity reason write a </tr> if the tag is not nested in a layout tag.
193
TagUtils.write(pageContext, "</tr>");
194     }
195     
196
197     if (help!=null) {
198         buffer.setLength(0);
199         // PENDING the text is displayed on the right if the tag is in a line tag.
200
if (Boolean.FALSE.equals(new StartLayoutEvent(this, null).send())) {
201             buffer.append("<tr>");
202         }
203         buffer.append("<td colspan=\"").append(getSkin().getFieldInterface(model).getColumnNumber()).append("\" class=\"");
204         buffer.append(helpStyleClass);
205         buffer.append("\">");
206         buffer.append(LayoutUtils.getLabel(pageContext, help, null));
207         buffer.append("</td>");
208         TagUtils.write(pageContext, buffer.toString());
209         if (Boolean.FALSE.equals(new EndLayoutEvent(this, null).send())) {
210             TagUtils.write(pageContext, "</tr>");
211         }
212     }
213 }
214
215 /**
216  * Insert the method's description here.
217  * Creation date: (01/12/2001 00:52:02)
218  * @return java.lang.String
219  */

220 public java.lang.String JavaDoc getErrorStyleClass() {
221     return errorStyleClass;
222 }
223 public String JavaDoc getHelp() {
224     return help;
225 }
226 /**
227  * Insert the method's description here.
228  * Creation date: (01/12/2001 00:04:23)
229  * @return java.lang.String
230  */

231 public java.lang.String JavaDoc getHelpStyleClass() {
232     return helpStyleClass;
233 }
234 /**
235  * Insert the method's description here.
236  * Creation date: (01/12/2001 01:26:01)
237  * @return boolean
238  */

239
240 public void release() {
241     super.release();
242     help = null;
243     hint = null;
244     tooltip = null;
245     helpStyleClass = "HELP";
246     isRequired = "false";
247     errorStyleClass = "ERROR";
248     layout = true;
249     model = null;
250 }
251
252     protected void reset() {
253         super.reset();
254         isRequired = jspIsRequired;
255         jspIsRequired = null;
256     }
257
258 public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
259     return Boolean.FALSE;
260 }
261 public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) {
262     return Boolean.FALSE;
263 }
264 /**
265  * Retrieve the errors associated with the current property if there are any.
266  */

267 private List JavaDoc retrieveErrors() throws JspException JavaDoc {
268     return LayoutUtils.retrieveErrors(pageContext, bundle, property);
269 }
270 /**
271  * Insert the method's description here.
272  * Creation date: (01/12/2001 00:52:02)
273  * @param newErrorStyleClass java.lang.String
274  */

275 public void setErrorStyleClass(java.lang.String JavaDoc newErrorStyleClass) {
276     errorStyleClass = newErrorStyleClass;
277 }
278
279     protected void initDynamicValues() {
280         super.initDynamicValues();
281         jspIsRequired = isRequired;
282         isRequired = Expression.evaluate(jspIsRequired, pageContext);
283     }
284
285 /**
286  * Set the help message key.
287  */

288 public void setHelp(String JavaDoc help) {
289     this.help = help;
290 }
291 /**
292  * Set the tooltip
293  */

294 public void setTooltip(String JavaDoc tooltip) {
295     this.tooltip = tooltip;
296 }
297 /**
298  * Get the tooltip
299  */

300 public String JavaDoc getTooltip() {
301     return tooltip;
302 }
303 /**
304  * Insert the method's description here.
305  * Creation date: (01/12/2001 00:04:23)
306  * @param newHelpStyleClass java.lang.String
307  */

308 public void setHelpStyleClass(java.lang.String JavaDoc newHelpStyleClass) {
309     helpStyleClass = newHelpStyleClass;
310 }
311 /**
312  * If set to "TRUE" a red star is added after the field when the value is null
313  * A piece of Javascript adds or removes the start when the value changes
314  */

315 public void setIsRequired(String JavaDoc newIsRequired) {
316     isRequired = newIsRequired;
317 }
318
319 public String JavaDoc getIsRequired() {
320     return isRequired;
321 }
322
323     public boolean isRequired() {
324         return "true".equalsIgnoreCase(isRequired);
325     }
326
327
328     public void setHint(String JavaDoc in_hint) {
329         hint = in_hint;
330     }
331     public String JavaDoc getHint() {
332         return hint;
333     }
334
335     /**
336      * Returns the layout.
337      * @return boolean
338      */

339     public boolean isLayout() {
340         return layout;
341     }
342
343     /**
344      * Sets the layout.
345      * @param layout The layout to set
346      */

347     public void setLayout(boolean layout) {
348         this.layout = layout;
349     }
350     
351     public String JavaDoc getModel() {
352         return model;
353     }
354     public void setModel(String JavaDoc in_string) {
355         model = in_string;
356     }
357
358     /**
359      * Returns an empty label if layout is set to false.
360      */

361     protected String JavaDoc getLabel() throws JspException JavaDoc {
362         if (isLayout()) {
363             return super.getLabel();
364         } else {
365             return "";
366         }
367     }
368
369 }
370
Popular Tags