KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > FormTag


1 package fr.improve.struts.taglib.layout;
2
3 import javax.servlet.http.HttpServletResponse JavaDoc;
4 import javax.servlet.jsp.JspException JavaDoc;
5 import javax.servlet.jsp.PageContext JavaDoc;
6
7 import org.apache.struts.config.ActionConfig;
8 import org.apache.struts.config.ModuleConfig;
9 import org.apache.struts.taglib.html.Constants;
10
11 import fr.improve.struts.taglib.layout.el.Expression;
12 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
13 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
14 import fr.improve.struts.taglib.layout.util.FormUtilsInterface;
15 import fr.improve.struts.taglib.layout.util.LayoutUtils;
16 import fr.improve.struts.taglib.layout.util.TagUtils;
17
18 /**
19  * Display a form tag using the panel layout.<br>
20  * This tag can specify a default action for the action using the DispatchAction class.<br>
21  *
22  * @author: Jean-NoŽl Ribette
23  */

24 public class FormTag extends PanelTag {
25     protected String JavaDoc acceptCharset = null;
26     
27     protected String JavaDoc action = null;
28     protected String JavaDoc enctype = null;
29     
30     protected String JavaDoc focus = null;
31     protected String JavaDoc editFocus = null;
32     protected String JavaDoc inspectFocus = null;
33     protected String JavaDoc createFocus = null;
34     protected String JavaDoc jspFocus = null;
35     
36     
37     protected String JavaDoc method = "POST";
38     protected String JavaDoc onreset = null;
39     protected String JavaDoc onsubmit = null;
40     protected String JavaDoc onkeypress = null;
41     protected String JavaDoc scope = "session";
42     protected String JavaDoc style = null;
43     protected String JavaDoc target = null;
44     protected String JavaDoc type = null;
45     protected String JavaDoc reqCode = null;
46     
47     protected String JavaDoc styleId;
48     
49     /**
50      * Required scheme.
51      * If not null, an absolute URL
52      * with the specified scheme is generated.
53      */

54     protected String JavaDoc scheme;
55     
56     protected org.apache.struts.taglib.html.FormTag formTag = new SchemeFormTag();
57
58     protected int mode = FormUtilsInterface.EDIT_MODE;
59     
60     /**
61      * @deprecated
62      */

63     public static final int CREATE_MODE = FormUtilsInterface.CREATE_MODE;
64     
65     /**
66      * @deprecated
67      */

68     public static final int EDIT_MODE = FormUtilsInterface.EDIT_MODE;
69     
70     /**
71      * @deprecated
72      */

73     public static final int INSPECT_MODE = FormUtilsInterface.INSPECT_MODE;
74     
75     /**
76      * Layout
77      */

78     protected boolean layout = true;
79     
80     /**
81      * Modified Struts form tag to support scheme.
82      */

83     private class SchemeFormTag extends org.apache.struts.taglib.html.FormTag {
84         /**
85          * Render action attribute.
86          */

87         protected void renderAction(StringBuffer JavaDoc results) {
88             String JavaDoc schemeNow = Expression.evaluate(scheme, this.pageContext);
89             if (schemeNow==null || schemeNow.length()==0) {
90                 // Do nothing : use super.renderAction
91
super.renderAction(results);
92             } else {
93                 // Compute absolute URL.
94
HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc) this.pageContext.getResponse();
95
96                 String JavaDoc host = this.pageContext.getRequest().getServerName();
97                 String JavaDoc action = TagUtils.getActionMappingURL(this.action, this.pageContext);
98                 
99                 results.append(" action=\"");
100                 results.append(
101                     response.encodeURL(schemeNow + "://" + host + action));
102                 results.append("\"");
103             }
104         }
105     }
106
107     public FormTag() {
108         super();
109     // name = Constants.BEAN_KEY;
110
}
111     
112     public int doEndLayoutTag() throws JspException JavaDoc {
113         if (layout) {
114             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
115             doPrintBlankLine(buffer);
116             doAfterBody(buffer);
117             TagUtils.write(pageContext, buffer.toString());
118             buffer.setLength(0);
119             doEndPanel(buffer);
120             TagUtils.write(pageContext, buffer.toString());
121             doEndLayout();
122         }
123         
124         int ret = formTag.doEndTag();
125         formTag.release();
126         if(getOnkeypress()!=null && getOnkeypress().length()!=0){
127             TagUtils.write(pageContext,"<script language=\"javaScript\">document.forms['" + formTag.getBeanName() + "'].onkeypress="+ getOnkeypress() +";</script>");
128         }
129         return ret;
130     }
131     
132     public int doStartLayoutTag() throws JspException JavaDoc {
133         //Initialize the struts form tag.
134
LayoutUtils.copyProperties(formTag,this);
135         
136         // Start the struts form tag.
137
int ret = formTag.doStartTag();
138         
139         //Start the layout.
140
StringBuffer JavaDoc buffer = null;
141         if (layout) {
142             doStartLayout();
143             buffer = new StringBuffer JavaDoc("\n");
144             doStartPanel(buffer);
145             TagUtils.write(pageContext,buffer.toString());
146         }
147         
148     
149         // the mode must be calculated after the struts formTag is begun.
150
mode = getSkin().getFormUtils().getFormDisplayMode(pageContext);
151         computeDisplayMode();
152         
153         if (layout) {
154             buffer.setLength(0);
155         }
156         // if needed, add an hidden input field setting the reqCode.
157
processRequestCode(buffer);
158     
159         // recalculate the key.
160
processKey();
161         
162         // recalculate the focus.
163
processFocus();
164         
165         if (layout) {
166             doPrintTitle(buffer);
167             doBeforeBody(buffer);
168             doPrintBlankLine(buffer);
169             TagUtils.write(pageContext,buffer.toString());
170         }
171         return ret;
172     }
173
174     /**
175      * Specify the default action parameter if Action class is subclass of DispatchAction.
176      */

177     protected void processRequestCode(StringBuffer JavaDoc buffer) throws JspException JavaDoc {
178         if (reqCode!=null) {
179             try {
180                 String JavaDoc parameter = computeActionParameter(pageContext, action);
181             
182                 // set the default reqCode if we are posting to a dispatch action
183
if (parameter!=null) {
184                     buffer.append("<input type=\"hidden\" name=\"");
185                     buffer.append(parameter);
186                     buffer.append("\" value=\"");
187                     buffer.append(reqCode);
188                     buffer.append("\">\n");
189                 }
190             } catch (Exception JavaDoc e) {
191                 JspException JavaDoc jspe = new JspException JavaDoc("BaseHandlerTag - cannot acces the DispatchAction parameter: " + e.getMessage() + "(" + e.getClass().getName() + ")");
192                 TagUtils.saveException(pageContext, jspe);
193                 throw jspe;
194             }
195         }
196     }
197     
198     /**
199      * Look for the struts action parameter value defined in struts-config.
200      */

201     public static String JavaDoc computeActionParameter(PageContext JavaDoc in_pageContext, String JavaDoc in_actionName) {
202         String JavaDoc value = computeActionName(in_actionName);
203         ModuleConfig lc_moduleConfig = TagUtils.getModuleConfig(in_pageContext);
204         ActionConfig lc_actionConfig = lc_moduleConfig.findActionConfig(value);
205         String JavaDoc parameter = lc_actionConfig.getParameter();
206         return parameter;
207     }
208     
209     /**
210      * Retrurn the current form action parameter.
211      */

212     public static String JavaDoc computeActionParameter(PageContext JavaDoc in_pageContext) {
213         org.apache.struts.taglib.html.FormTag lc_strutsFormTag = findFormTag(in_pageContext);
214         String JavaDoc action = lc_strutsFormTag==null ? "" : lc_strutsFormTag.getAction();
215         String JavaDoc parameter = FormTag.computeActionParameter(in_pageContext, action);
216         return parameter;
217     }
218     
219     private static org.apache.struts.taglib.html.FormTag findFormTag(PageContext JavaDoc in_pageContext) {
220         org.apache.struts.taglib.html.FormTag lc_strutsFormTag = (org.apache.struts.taglib.html.FormTag) in_pageContext.findAttribute(Constants.FORM_KEY);
221         if (lc_strutsFormTag==null && !LayoutUtils.getNoErrorMode()) {
222             throw new IllegalStateException JavaDoc("reqCode can not be set if tag is not inside a form tag");
223         }
224         return lc_strutsFormTag;
225     }
226
227     /**
228      * Return the current form name.
229      */

230     public static String JavaDoc computeFormName(PageContext JavaDoc in_pageContext) {
231         org.apache.struts.taglib.html.FormTag lc_strutsFormTag = findFormTag(in_pageContext);
232         return lc_strutsFormTag.getBeanName();
233     }
234
235     /**
236      * Look for the struts action name as defined in struts-config.
237      */

238     protected static String JavaDoc computeActionName(String JavaDoc in_actionName) {
239         return TagUtils.getActionMappingName(in_actionName);
240     }
241
242     /**
243      * Add .edit /.create or .inspect to the end of the key if needed.
244      */

245     protected void processKey() {
246         if (key!=null && LayoutUtils.getUseFormDisplayMode()) {
247             switch (mode) {
248                 case FormUtilsInterface.EDIT_MODE:
249                     key = key + ".edit";
250                     break;
251                 case FormUtilsInterface.CREATE_MODE:
252                     key = key + ".create";
253                     break;
254                 case FormUtilsInterface.INSPECT_MODE:
255                     key = key + ".inspect";
256             }
257         }
258     }
259     
260     /**
261      * Set the focus according to the form display mode.
262      */

263     protected void processFocus() {
264         switch (mode) {
265             case FormUtilsInterface.EDIT_MODE:
266                 if (editFocus!=null) {
267                     formTag.setFocus(editFocus);
268                 }
269                 break;
270             case FormUtilsInterface.INSPECT_MODE:
271                 if (inspectFocus!=null) {
272                     formTag.setFocus(inspectFocus);
273                 }
274                 break;
275             case FormUtilsInterface.CREATE_MODE:
276                 if (createFocus!=null) {
277                     formTag.setFocus(createFocus);
278                 }
279                 break;
280         }
281     }
282
283 public String JavaDoc getAction() {
284     return action;
285 }
286     public int getDisplayMode() { return mode; }
287 public String JavaDoc getEnctype() {
288     return enctype;
289 }
290 public String JavaDoc getFocus() {
291     return focus;
292 }
293 public String JavaDoc getMethod() {
294     return method;
295 }
296 public String JavaDoc getOnreset() {
297     return onreset;
298 }
299 public String JavaDoc getOnsubmit() {
300     return onsubmit;
301 }
302 public String JavaDoc getOnkeypress() {
303     return onkeypress;
304 }
305 public String JavaDoc getScope() {
306     return scope;
307 }
308 public String JavaDoc getReqCode() {
309     return reqCode;
310 }
311 public String JavaDoc getStyle() {
312     return style;
313 }
314 public String JavaDoc getTarget() {
315     return target;
316 }
317 public String JavaDoc getType() {
318     return type;
319 }
320 public void release() {
321     super.release();
322     acceptCharset = null;
323     action = null;
324     method = null;
325     scope = null;
326     //name = Constants.BEAN_KEY;
327
mode = FormUtilsInterface.EDIT_MODE;
328     reqCode = null;
329     onkeypress = null;
330     onreset = null;
331     onsubmit = null;
332     
333     focus = null;
334     editFocus = null;
335     createFocus = null;
336     inspectFocus = null;
337     jspFocus = null;
338     styleId = null;
339     scheme = null;
340     
341     layout = true;
342     
343     formTag.release();
344 }
345 /**
346  * Set the action for the form tag. <br>
347  * If the init-parameter 'struts-layout-mode' is equal to 'noerror' the action is set to '/default'.
348  */

349 public void setAction(String JavaDoc action) {
350     this.action = action;
351     if (LayoutUtils.getNoErrorMode()) this.action = "/default";
352
353 }
354 public void setEnctype(String JavaDoc enctype) {
355     this.enctype = enctype;
356 }
357 public void setFocus(String JavaDoc focus) {
358     this.focus = focus;
359 }
360 public void setMethod(String JavaDoc method) {
361     this.method = method;
362 }
363 public void setOnreset(String JavaDoc onreset) {
364     this.onreset = onreset;
365 }
366 public void setOnsubmit(String JavaDoc onsubmit) {
367     this.onsubmit = onsubmit;
368 }
369 public void setOnkeypress(String JavaDoc onkeypress) {
370     this.onkeypress = onkeypress;
371 }
372     public void setReqCode(String JavaDoc reqCode) {
373         this.reqCode = reqCode;
374     }
375 public void setScope(String JavaDoc scope) {
376     this.scope = scope;
377 }
378 public void setStyle(String JavaDoc style) {
379     this.style = style;
380 }
381 public void setTarget(String JavaDoc target) {
382     this.target = target;
383 }
384 public void setType(String JavaDoc type) {
385     this.type = type;
386 }
387     /**
388      * Returns the createFocus.
389      * @return String
390      */

391     public String JavaDoc getCreateFocus() {
392         return createFocus;
393     }
394
395     /**
396      * Returns the editFocus.
397      * @return String
398      */

399     public String JavaDoc getEditFocus() {
400         return editFocus;
401     }
402
403     /**
404      * Returns the inspectFocus.
405      * @return String
406      */

407     public String JavaDoc getInspectFocus() {
408         return inspectFocus;
409     }
410
411     /**
412      * Sets the createFocus.
413      * @param createFocus The createFocus to set
414      */

415     public void setCreateFocus(String JavaDoc createFocus) {
416         this.createFocus = createFocus;
417     }
418
419     /**
420      * Sets the editFocus.
421      * @param editFocus The editFocus to set
422      */

423     public void setEditFocus(String JavaDoc editFocus) {
424         this.editFocus = editFocus;
425     }
426
427     /**
428      * Sets the inspectFocus.
429      * @param inspectFocus The inspectFocus to set
430      */

431     public void setInspectFocus(String JavaDoc inspectFocus) {
432         this.inspectFocus = inspectFocus;
433     }
434
435     protected void initDynamicValues() {
436         // Super init.
437
super.initDynamicValues();
438         
439         // Compute focus value.
440
jspFocus = focus;
441         focus = Expression.evaluate(focus, pageContext);
442     }
443     protected void reset() {
444         // Reset focus value.
445
focus = jspFocus;
446         jspFocus = null;
447         
448         // Super reset.
449
super.reset();
450     }
451
452     public String JavaDoc getStyleId() {
453         return styleId;
454     }
455
456     public void setStyleId(String JavaDoc styleId) {
457         this.styleId = styleId;
458     }
459
460     public String JavaDoc getAcceptCharset() {
461         return acceptCharset;
462     }
463
464     public void setAcceptCharset(String JavaDoc acceptCharset) {
465         this.acceptCharset = acceptCharset;
466     }
467
468     public String JavaDoc getScheme() {
469         return scheme;
470     }
471
472     public void setScheme(String JavaDoc scheme) {
473         this.scheme = scheme;
474     }
475
476     public boolean isLayout() {
477         return layout;
478     }
479
480     public void setLayout(boolean layout) {
481         this.layout = layout;
482     }
483
484     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) throws JspException JavaDoc {
485         if (layout) {
486             return super.processEndLayoutEvent(in_event);
487         } else {
488             return Boolean.FALSE;
489         }
490     }
491
492     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
493         if (layout) {
494             return super.processStartLayoutEvent(in_event);
495         } else {
496             return Boolean.FALSE;
497         }
498     }
499 }
500
Popular Tags