KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.field;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Iterator JavaDoc;
5
6 import javax.servlet.jsp.JspException JavaDoc;
7
8 import org.apache.struts.util.ResponseUtils;
9
10 import fr.improve.struts.taglib.layout.FormTag;
11 import fr.improve.struts.taglib.layout.LabelledTag;
12 import fr.improve.struts.taglib.layout.el.Expression;
13 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
14 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
15 import fr.improve.struts.taglib.layout.policy.AbstractPolicy;
16 import fr.improve.struts.taglib.layout.util.LayoutUtils;
17 import fr.improve.struts.taglib.layout.util.NestedHelper;
18 import fr.improve.struts.taglib.layout.util.TagUtils;
19
20 /**
21  * Base class for the tags dealing with form input.<br>
22  * <br>
23  * This class brings the support of display modes, which applies to the fields tag and the form tag.<br>
24  * The form tag has 3 display modes: Create, Edit and Inspect.<br>
25  * The field tag has also 3 display modes: Edit, Inspect, and Not displayed.<br>
26  * One field display mode can be specified for each form mode.<br>
27  * Have a look at the setMode methods to see how to specified the field display modes.<br>
28  *
29  * Moreover, the field display modes can be modified at run time by applying a display policy.
30  *
31  * @author: Jean-No?l Ribette
32  */

33 public abstract class AbstractModeFieldTag extends LabelledTag {
34     /**
35      * Display mode for this field in the different form editing mode (CREATE, EDIT, INSPECT)
36      * 2: the field will be editable, 1: the field will read-only, 0: the field will not be display at all
37      */

38     private String JavaDoc mode = "E,E,I";
39     /**
40      * the actual display mode, computed by computeDisplaymode()
41      */

42     private short fieldDisplayMode = MODE_EDIT;
43
44     /**
45      * The saved styleClass.
46      */

47     private String JavaDoc jspStyleClass = null;
48     
49     /**
50      * The saved style.
51      */

52     private String JavaDoc jspStyle = null;
53     
54     /**
55      * The saved property.
56      */

57     private String JavaDoc jspProperty = null;
58     
59     /**
60      * The saved mode.
61      */

62     private String JavaDoc jspMode = null;
63     
64     /**
65      * The saved styleId.
66      */

67     private String JavaDoc jspStyleId = null;
68     
69     protected boolean disabledAsBoolean = false;
70     protected String JavaDoc disabled = "false";
71     protected boolean readonly = false;
72     
73     protected Boolean JavaDoc jspReadonly;
74     
75
76     /**
77      * Do not display the field at all. No hidden field generated.
78      */

79     public static final short MODE_NODISPLAY = 0;
80     
81     /**
82      * Display the field read only. An hidden field is also genreated.
83      */

84     public static final short MODE_INSPECT = 1;
85     
86     /**
87      * Display the field writable.
88      */

89     public static final short MODE_EDIT = 2;
90     
91     /**
92      * Do not display the field at all, but generates an hidden field.
93      */

94     public static final short MODE_HIDDEN = 3;
95     
96     /**
97      * Display the field value read only, no hidden field generated.
98      */

99     public static final short MODE_INSPECT_ONLY = 4;
100
101     /**
102      * Display the field value read only, only if the value is not null. No hidden field generated.
103      */

104     public static final short MODE_INSPECT_PRESENT = 5;
105     
106     /**
107      * Display the field value read only.
108      */

109     public static final short MODE_READONLY = 6;
110     
111     /**
112      * Display the field value disabled.
113      */

114     public static final short MODE_DISABLED = 7;
115     
116     /**
117      * Display an empty cell instead of the field
118      */

119     public static final short MODE_CELL = 8;
120
121     /**
122      * The name of the policy to apply.
123      */

124     private String JavaDoc policy = null;
125     protected String JavaDoc styleId;
126             
127     /**
128      * Set fieldDisplayMode to the correct display mode (create, edit or inspect).
129      */

130     protected void computeDisplayMode(Integer JavaDoc in_overrideFieldDisplayMode) {
131         if (in_overrideFieldDisplayMode == null) {
132             // If no display mode was specified in the Struts action, compute the mode from this tag mode attribute.
133
fieldDisplayMode = getSkin().getFormUtils().computeFieldDisplayMode(this);
134         } else {
135             // Else, just use the value set in the action.
136
fieldDisplayMode = in_overrideFieldDisplayMode.shortValue();
137         }
138         
139         // Now, check the policy.
140
switch (fieldDisplayMode) {
141             case MODE_EDIT :
142             case MODE_READONLY :
143             case MODE_DISABLED :
144                 // Edit, readonly and disabled generate a visible input field.
145
fieldDisplayMode = getEditAuthorizedMode();
146                 break;
147             case MODE_INSPECT :
148             case MODE_INSPECT_ONLY:
149             case MODE_INSPECT_PRESENT:
150             case MODE_CELL :
151                 // Inspect, inspect only and inspect present do NOT generate a visible input field.
152
fieldDisplayMode = getInspectAuthorizedMode(fieldDisplayMode);
153                 break;
154         }
155         
156         // To finish, map readonly and disabled modes to edit mode.
157
switch (fieldDisplayMode) {
158             case MODE_READONLY:
159                 fieldDisplayMode = MODE_EDIT;
160                 jspReadonly = readonly ? Boolean.TRUE : Boolean.FALSE;
161                 readonly = true;
162                 break;
163             case MODE_DISABLED:
164                 fieldDisplayMode = MODE_EDIT;
165                 disabledAsBoolean = true;
166                 break;
167         }
168     }
169     /**
170      * End a in edit field.
171      */

172     protected abstract int doEndEditMode() throws JspException JavaDoc;
173     /**
174      * End a field in inspect mode.
175      */

176     protected abstract int doEndInspectMode() throws JspException JavaDoc;
177     /**
178      * End the tag.
179      */

180     public int doEndLayoutTag() throws JspException JavaDoc {
181         int lc_ret = EVAL_PAGE;
182         switch (fieldDisplayMode) {
183             case MODE_NODISPLAY :
184             case MODE_HIDDEN :
185             case MODE_CELL :
186                 break;
187             case MODE_INSPECT_ONLY:
188             case MODE_INSPECT :
189             case MODE_INSPECT_PRESENT:
190                 lc_ret = doEndInspectMode();
191                 break;
192             case MODE_EDIT :
193                 lc_ret = doEndEditMode();
194                 break;
195         }
196         return lc_ret;
197     }
198     
199     /**
200      * Display a field in cell mode.
201      */

202     protected int doCellMode() throws JspException JavaDoc {
203         new StartLayoutEvent(this, null).send();
204         TagUtils.write(pageContext, "<th colspan=\"");
205         TagUtils.write(pageContext, String.valueOf(getSkin().getFieldInterface().getColumnNumber()));
206         if (styleClass!=null) {
207             TagUtils.write(pageContext, "\" class=\"");
208             TagUtils.write(pageContext, styleClass);
209         }
210         TagUtils.write(pageContext, "\">&nbsp;</th>");
211         new EndLayoutEvent(this, null).send();
212         return SKIP_BODY;
213     }
214     
215     
216     /**
217      * Start a field in edit mode.
218      */

219     protected abstract int doStartEditMode() throws JspException JavaDoc;
220     /**
221      * Start a field in inspect mode.
222      */

223     protected abstract int doStartInspectMode() throws JspException JavaDoc;
224     /**
225      * Start the tag.
226      */

227     public int doStartLayoutTag() throws JspException JavaDoc {
228         // Get the value to display.
229
Object JavaDoc lc_value = getFieldValue();
230
231         // Print the value in a hidden field if we are in hidden mode. In inspect mode AbstractLayoutFieldTag is doing this.
232
if (fieldDisplayMode == MODE_HIDDEN) {
233             printHiddenValue(lc_value);
234         }
235
236         // Skip the tag if the value is null and we are in inspect mode and we don't display null values, or if we are in mode_nodisplay.
237
if (!isFill(lc_value)
238             &&
239             (fieldDisplayMode == MODE_INSPECT && !getSkin().getDisplayNullFields()
240             ||
241             fieldDisplayMode == MODE_INSPECT_PRESENT
242             )) {
243             fieldDisplayMode = MODE_NODISPLAY;
244         }
245
246         // Start to display something for the user.
247
switch (fieldDisplayMode) {
248             case MODE_INSPECT :
249             case MODE_INSPECT_ONLY :
250             case MODE_INSPECT_PRESENT :
251                 return doStartInspectMode();
252             case MODE_EDIT :
253                 return doStartEditMode();
254             case MODE_NODISPLAY :
255             case MODE_HIDDEN :
256                 return SKIP_BODY;
257             case MODE_CELL :
258                 return doCellMode();
259         }
260
261         return EVAL_BODY_INCLUDE;
262     }
263     
264     protected void initDynamicValues() {
265         // Compute the property.
266
jspProperty = property;
267         property = Expression.evaluate(property, pageContext);
268         property = NestedHelper.getAdjustedProperty(property, pageContext);
269         
270         // Evaluate disabled value.
271
// Do it before computing the mode, which can modify the disabled attribute.
272
disabledAsBoolean = "true".equalsIgnoreCase(Expression.evaluate(disabled, pageContext)) ? true : false;
273         
274         // Set the mode.
275
jspMode = mode;
276         mode = Expression.evaluate(mode, pageContext);
277         
278         // Compute the display mode.
279
Integer JavaDoc lc_userModeDisplay = getSkin().getFormUtils().getFieldDisplayMode(pageContext, property);
280         computeDisplayMode(lc_userModeDisplay);
281         
282         // Save the style and styleClass to avoid tag pooling problems.
283
jspStyle = style;
284         jspStyleClass = styleClass;
285         // Set the default styleClass.
286
if (styleClass==null) {
287             styleClass = getSkin().getProperty("styleclass.label", null);
288         }
289         
290         // Set the styleClass.
291
String JavaDoc lc_userStyleClass = getSkin().getFormUtils().getFieldStyleClass(this);
292         if (lc_userStyleClass!=null) {
293             styleClass = lc_userStyleClass;
294         }
295         
296         // Set the style.
297
String JavaDoc lc_userStyle = getSkin().getFormUtils().getFieldStyle(pageContext, property);
298         if (lc_userStyle!=null) {
299             style = lc_userStyle;
300         }
301         
302         // Set the styleId
303
jspStyleId = styleId;
304         styleId = Expression.evaluate(styleId, pageContext);
305     }
306     
307     public boolean isFill(Object JavaDoc lc_value) throws JspException JavaDoc {
308         return lc_value != null && lc_value.toString().length()!=0;
309     }
310     public short getFieldDisplayMode() {
311         return fieldDisplayMode;
312     }
313     /**
314      * Return the value(s) that will be displayed.
315      */

316     protected abstract Object JavaDoc getFieldValue() throws JspException JavaDoc;
317   
318     /**
319      * Use by subclasses to know in which mode (create, edit, inspect) the form is.
320      */

321     protected int getFormMode() {
322         FormTag form = (FormTag) findAncestorWithClass(this, FormTag.class);
323         return form.getDisplayMode();
324   }
325   
326     /**
327      * Insert the method's description here.
328      * Creation date: (22/11/2001 15:22:08)
329      * @return java.lang.String
330      */

331     public java.lang.String JavaDoc getPolicy() {
332         return policy;
333     }
334     /**
335      * Print the value in a hidden input field.
336      */

337     protected final void printHiddenValue(Object JavaDoc in_value) throws JspException JavaDoc {
338         Collection JavaDoc lc_collection = LayoutUtils.getCollection(in_value, false);
339         if (lc_collection != null) {
340             printIndexedHiddenValue(lc_collection);
341         } else {
342             printSimpleHiddenValue(in_value);
343         }
344     }
345     protected void printSimpleHiddenValue(Object JavaDoc in_value) throws JspException JavaDoc {
346         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
347         lc_buffer.append("<input type=\"hidden\" name=\"");
348         lc_buffer.append(property);
349         lc_buffer.append("\" value=\"");
350         lc_buffer.append(in_value == null ? "" : ResponseUtils.filter(in_value.toString()));
351         if (styleId!=null) {
352             lc_buffer.append("\" id=\"");
353             lc_buffer.append(styleId);
354         }
355         lc_buffer.append("\">");
356         TagUtils.write(pageContext, lc_buffer.toString());
357     }
358     
359     protected void printIndexedHiddenValue(Collection JavaDoc lc_collection) throws JspException JavaDoc {
360         // Print all the bean in this collection.
361
Iterator JavaDoc lc_iterator = lc_collection.iterator();
362         int i = 0;
363         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
364         while (lc_iterator.hasNext()) {
365             Object JavaDoc lc_bean = lc_iterator.next();
366             lc_buffer.append("<input type=\"hidden\" name=\"");
367             lc_buffer.append(property);
368             lc_buffer.append("[");
369             lc_buffer.append(i);
370             lc_buffer.append("]");
371             lc_buffer.append("\" value=\"");
372             lc_buffer.append(lc_bean == null ? "" : lc_bean.toString());
373             lc_buffer.append("\">");
374             i++;
375         }
376         TagUtils.write(pageContext, lc_buffer.toString());
377     }
378     
379     /**
380      * Reset the tag after is has been processed.
381      * All variables modified during the evaluation must be reset to their initial value.
382      */

383     protected void reset() {
384         fieldDisplayMode = MODE_EDIT;
385         disabledAsBoolean = false;
386         if (jspReadonly!=null) {
387             readonly = jspReadonly.booleanValue();
388             jspReadonly = null;
389         }
390         styleClass = jspStyleClass;
391         style = jspStyle;
392         property = jspProperty;
393         mode = jspMode;
394         styleId = jspStyleId;
395         
396         jspStyleClass = null;
397         jspStyle = null;
398         jspProperty = null;
399         jspMode = null;
400         jspStyleId = null;
401     }
402     
403     /**
404      * Release the tag.
405      */

406     public void release() {
407         super.release();
408         policy = null;
409         mode = "E,E,I";
410         styleId = null;
411         disabledAsBoolean = false;
412         disabled = "false";
413         readonly = false;
414     }
415     protected short getEditAuthorizedMode() {
416         if (policy == null)
417             return getFieldDisplayMode();
418
419         AbstractPolicy lc_policy = getSkin().getPolicy();
420         return lc_policy.getAuthorizedDisplayMode(policy, name, property, pageContext);
421     }
422     
423     protected short getInspectAuthorizedMode(short in_inspectOnly) {
424         if (policy == null) {
425             return in_inspectOnly;
426             }
427         AbstractPolicy lc_policy = getSkin().getPolicy();
428         return lc_policy.getAuthorizedDisplayMode(policy, name, property, pageContext);
429     }
430     
431     /**
432      * Set the display mode
433      * format is XX,XX,XX where XX can be H (hidden), N (not displayed), E (editable), I (inspectable), S(inspect / no hidden). Order is create mode, edit mode, inspect mode
434      */

435     public void setMode(String JavaDoc in_mode) {
436         mode = in_mode;
437     }
438     public String JavaDoc getMode() {
439         return mode;
440     }
441     /**
442      * Insert the method's description here.
443      * Creation date: (22/11/2001 15:22:08)
444      * @param newPolicy java.lang.String
445      */

446     public void setPolicy(java.lang.String JavaDoc newPolicy) {
447         policy = newPolicy;
448     }
449     public void setProperty(String JavaDoc property) {
450         if (LayoutUtils.getNoErrorMode()) {
451             this.property = "property";
452         } else {
453             this.property = property;
454         }
455     }
456     
457     /**
458      * Returns the styleId.
459      * @return String
460      */

461     public String JavaDoc getStyleId() {
462         return styleId;
463     }
464
465     /**
466      * Sets the styleId.
467      * @param styleId The styleId to set
468      */

469     public void setStyleId(String JavaDoc styleId) {
470         this.styleId = styleId;
471     }
472         
473     /**
474      * @return
475      */

476     public boolean isDisabledAsBoolean() {
477         return disabledAsBoolean;
478     }
479     /**
480      * @param b
481      */

482     public void setDisabledAsBoolean(boolean b) {
483         disabledAsBoolean = b;
484     }
485
486     /**
487      * @return
488      */

489     public boolean isReadonly() {
490         return readonly;
491     }
492
493     /**
494      * @param b
495      */

496     public void setReadonly(boolean b) {
497         readonly = b;
498     }
499
500     /**
501      * @return Returns the disabled.
502      */

503     public String JavaDoc getDisabled() {
504         return disabled;
505     }
506     /**
507      * @param disabled The disabled to set.
508      */

509     public void setDisabled(String JavaDoc disabled) {
510         this.disabled = disabled;
511     }
512 }
Popular Tags