Source code: com/aendvari/tethys/tag/html/FormTag.java
1 /*
2 * OptionTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.html;
11
12 import java.util.*;
13 import java.io.IOException;
14
15 import javax.servlet.http.*;
16 import javax.servlet.jsp.*;
17 import javax.servlet.jsp.tagext.*;
18
19 import com.aendvari.common.model.*;
20
21 import com.aendvari.tethys.context.*;
22 import com.aendvari.tethys.tag.*;
23 import com.aendvari.tethys.tag.model.*;
24
25
26 /**
27 * The HTML <code>form</code> tag.
28 *
29 * @author Scott Milne
30 *
31 */
32
33 public class FormTag extends ModelTreeTag
34 {
35 /** The <code>action</code> HTML attribute. */
36 protected String action = null;
37
38 /** The name of the field to receive focus, if any. */
39 protected String focus = null;
40
41 /** The <code>method</code> HTML attribute. */
42 protected String method = "POST";
43
44 /** The <code>name</code> HTML attribute. */
45 protected String name = null;
46
47 /** The <code>onreset</code> HTML attribute. */
48 protected String onReset = null;
49
50 /** The <code>onsubmit</code> HTML attribute. */
51 protected String onSubmit = null;
52
53 /** The <code>style</code> HTML attribute. */
54 protected String style = null;
55
56 /** The <code>class</code> HTML attribute. */
57 protected String styleClass = null;
58
59 /** The <code>target</code> HTML attribute. */
60 protected String target = null;
61
62 /** The list of html:copyform entries to add to onsubmit (if any) */
63 private ArrayList copyFormNames = new ArrayList();
64
65
66 /* Accessors */
67
68 public String getAction() { return action; }
69 public void setAction(String param) { action = param; }
70
71 public String getFocus() { return focus; }
72 public void setFocus(String param) { focus = param; }
73
74 public String getMethod() { return method; }
75 public void setMethod(String param) { method = param; }
76
77 public String getName() { return name; }
78 public void setName(String param) { name = param; }
79
80 public String getOnreset() { return onReset; }
81 public void setOnreset(String param) { onReset = param; }
82
83 public String getOnsubmit() { return onSubmit; }
84 public void setOnsubmit(String param) { onSubmit = param; }
85
86 public String getStyle() { return style; }
87 public void setStyle(String param) { style = param; }
88
89 public String getStyleClass() { return styleClass; }
90 public void setStyleClass(String param) { styleClass = param; }
91
92 public String getTarget() { return target; }
93 public void setTarget(String param) { target = param; }
94
95
96 /* Methods */
97
98 public void addCopyFormEntry(String name) { copyFormNames.add(name); }
99
100
101 public int doStartTag() throws JspException
102 {
103 try
104 {
105 // establish tag context
106 establishModelContext();
107
108 // generate the tag string
109 StringBuffer tagString = new StringBuffer("<form");
110
111 tagString.append(" name=\"");
112 tagString.append(name);
113 tagString.append("\"");
114
115 tagString.append(" method=\"");
116 tagString.append(method);
117 tagString.append("\"");
118
119 tagString.append(" action=\"");
120 tagString.append(action);
121 tagString.append("\"");
122
123 if (styleClass != null)
124 {
125 tagString.append(" class=\"");
126 tagString.append(styleClass);
127 tagString.append("\"");
128 }
129
130 if (onReset != null)
131 {
132 tagString.append(" onreset=\"");
133 tagString.append(onReset);
134 tagString.append("\"");
135 }
136
137 // create the "internal" submit function call
138 tagString.append(" onsubmit=\"return ");
139 tagString.append(name);
140 tagString.append("_submitForm(this)");
141 tagString.append("\"");
142
143 if (style != null)
144 {
145 tagString.append(" style=\"");
146 tagString.append(style);
147 tagString.append("\"");
148 }
149
150 if (target != null)
151 {
152 tagString.append(" target=\"");
153 tagString.append(target);
154 tagString.append("\"");
155 }
156
157
158 // finish off the tag
159 tagString.append(">");
160
161 // print out the finalized string
162 pageContext.getOut().write(tagString.toString());
163 }
164 catch (Exception e)
165 {
166 throw new JspTagException("FormTag Error:" + e.toString());
167 }
168
169 // allow the body of this tag to be shown
170 return EVAL_BODY_INCLUDE;
171 }
172
173 public int doEndTag() throws JspException
174 {
175 try
176 {
177 StringBuffer tagString = new StringBuffer("</form>");
178
179 // create the JS area for this form
180 tagString.append("<script language=\"JavaScript\"");
181 tagString.append(" type=\"text/javascript\">\r\n");
182 tagString.append(" <!--\r\n");
183
184 // if they want a given field to have focus, do it now
185 if (focus != null)
186 {
187 tagString.append(" document.");
188 tagString.append(name);
189 tagString.append(".");
190 tagString.append(focus);
191 tagString.append(".focus()\r\n");
192 tagString.append("\r\n");
193 }
194
195 // create the internal submit function
196 tagString.append("function ");
197 tagString.append(name);
198 tagString.append("_submitForm(theForm) {\r\n");
199
200 // if there were any copyform tags inside this form, add their copyForm function call here
201 if (copyFormNames.size() > 0)
202 {
203 Iterator iterator = copyFormNames.iterator();
204
205 while (iterator.hasNext())
206 {
207 tagString.append(" satyrCopyForm(theForm, '");
208 tagString.append(iterator.next());
209 tagString.append("');\r\n");
210 }
211 }
212
213 // if a user provided onsubmit was given, add it here
214 if (onSubmit != null)
215 {
216 tagString.append(onSubmit);
217 }
218
219 // close of the JS
220 tagString.append("\r\n}\r\n");
221 tagString.append(" // -->\r\n");
222 tagString.append("</script>\r\n");
223
224 // print out the finalized string
225 pageContext.getOut().write(tagString.toString());
226 }
227 catch (Exception e)
228 {
229 throw new JspTagException("FormTag Error:" + e.toString());
230 }
231
232 // continue processing this page
233 return EVAL_PAGE;
234 }
235
236
237 public void release()
238 {
239 super.release();
240
241 action = null;
242 focus = null;
243 method = "POST";
244 name = null;
245 onReset = null;
246 onSubmit = null;
247 style = null;
248 styleClass = null;
249 target = null;
250 copyFormNames = null;
251 }
252 }