KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout;
2
3 import java.util.Iterator JavaDoc;
4
5 import javax.servlet.jsp.JspException JavaDoc;
6 import javax.servlet.jsp.PageContext JavaDoc;
7
8 import org.apache.struts.Globals;
9 import org.apache.struts.action.ActionErrors;
10 import org.apache.struts.action.ActionMessage;
11 import org.apache.struts.taglib.html.Constants;
12
13 import fr.improve.struts.taglib.layout.event.EndLayoutEvent;
14 import fr.improve.struts.taglib.layout.event.LayoutEventListener;
15 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
16 import fr.improve.struts.taglib.layout.util.LayoutUtils;
17 import fr.improve.struts.taglib.layout.util.TagUtils;
18
19 /**
20  * Class for the usual input fields (text, textarea, password and checkbox). <br>
21  * This tag automatically display the error associated to the input field if there is one.
22  * Javascript for simple type checking is also generated.<br>
23  * <br>
24  * Type for text can be: text, number, date or email.<br>
25  * <br>
26  * Values can be read-write, read-only, or read & resend.
27  *
28  * The content of the tag is displayed after the input field
29  * so that buttons or other elements can be added
30  *
31  * @author: Jean-Noel Ribette
32  * @deprecated
33  **/

34 public class FieldTag extends BaseFieldTag implements LayoutEventListener {
35
36     protected String JavaDoc accept;
37     protected String JavaDoc access = READWRITE;
38     public static final String JavaDoc CHECKBOX = "CHECKBOX";
39     // the taglib.html field properties
40
protected String JavaDoc cols = null;
41     public static final String JavaDoc DATE = "DATE";
42     public static final String JavaDoc EMAIL ="EMAIL";
43     // the error associated with this input field
44
protected String JavaDoc error = null;
45     protected org.apache.struts.taglib.html.BaseHandlerTag fieldTag = null;
46     protected boolean isRequired = false;
47     protected String JavaDoc maxlength = null;
48     public static final String JavaDoc NUMBER = "NUMBER";
49     public static final String JavaDoc PASSWORD = "PASSWORD";
50     public static final String JavaDoc READONLY = "READONLY";
51     public static final String JavaDoc READSEND ="READSEND";
52     // the read/write possibilities
53
// see setAccess() for more details
54
public static final String JavaDoc READWRITE = "READWRITE";
55     protected boolean redisplay = true;
56     protected String JavaDoc rows = null;
57     // the allowed types for this field
58
// TODO: use the work in progress on validation.
59
// see setType for more details
60
public static final String JavaDoc TEXT = "TEXT";
61     public static final String JavaDoc TEXTAREA = "TEXTAREA";
62     protected String JavaDoc type = TEXT;
63     protected String JavaDoc value = null;
64
65 public FieldTag() {
66     name = Constants.BEAN_KEY;
67 }
68 public int doEndLayoutTag() throws JspException JavaDoc {
69
70     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
71         
72     endField(buffer);
73     
74     TagUtils.write(pageContext, buffer.toString());
75     
76     return EVAL_PAGE;
77 }
78 public int doStartLayoutTag() throws JspException JavaDoc {
79     
80     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
81     
82     // display the label
83
beginField(buffer);
84
85     // get the error if any and print the html tags
86
error = retrieveError();
87     if (error!=null && access.equals(READWRITE))
88         buffer.append("<table><tr><td class=ERROR>");
89
90
91     TagUtils.write(pageContext,buffer.toString());
92                     
93     // create the corresponding tag
94
// maybe having a pool of tags would be better
95
if (access.equals(READSEND)) type = "hidden";
96     if (type.equals(CHECKBOX)) fieldTag = new org.apache.struts.taglib.html.CheckboxTag();
97     if (type.equals(PASSWORD)) fieldTag = new org.apache.struts.taglib.html.PasswordTag();
98     if (type.equals(TEXT)) fieldTag = new org.apache.struts.taglib.html.TextTag();
99     if (type.equals(TEXTAREA)) fieldTag = new org.apache.struts.taglib.html.TextareaTag();
100     if (type.equals(DATE)) fieldTag = new org.apache.struts.taglib.html.TextTag();
101     if (type.equals(NUMBER)) fieldTag = new org.apache.struts.taglib.html.TextTag();
102     if (type.equals(EMAIL)) fieldTag = new org.apache.struts.taglib.html.TextTag();
103     if (type.equals("hidden")) fieldTag = new org.apache.struts.taglib.html.HiddenTag();
104     // initialize the tag
105
LayoutUtils.copyProperties(fieldTag,this);
106
107     //set the name of the javascript to use
108
boolean check = isRequired;
109     fieldTag.setOnchange("checkValue(this, '"+ property + "','"+ type +"'," + check +");");
110             
111     // start the tag
112
if (!access.equals(READONLY)) {
113         fieldTag.doStartTag();
114         fieldTag.doEndTag();
115     }
116
117     buffer.setLength(0);
118     Object JavaDoc objvalue = LayoutUtils.getBeanFromPageContext(pageContext, name, property);
119
120     // the value is read only so display it as text.
121
if (!access.equals(READWRITE) && objvalue!=null) {
122         buffer.append("<span class=\"");
123         buffer.append(styleClass);
124         buffer.append("\">");
125         buffer.append(objvalue.toString());
126         buffer.append("</span>");
127     }
128
129     
130     // display the error if any
131
if (error!=null && access.equals(READWRITE)) {
132         buffer.append(error);
133         buffer.append("</td></tr></table>");
134     }
135
136         
137     // display an image if a value is needed
138
if (objvalue==null) objvalue="";
139     if (isRequired && objvalue.equals("")) {
140         buffer.append("<img name=\"" + property + "required\" SRC=\"");
141         buffer.append(getSkin().getImageDirectory(pageContext.getRequest()));
142         buffer.append("/ast.gif\">");
143     } else {
144         buffer.append("<img name=\"" + property + "required\" SRC=\"");
145         buffer.append(getSkin().getImageDirectory(pageContext.getRequest()));
146         buffer.append("/clearpixel.gif\">");
147     }
148
149     TagUtils.write(pageContext, buffer.toString());
150         
151     return EVAL_BODY_INCLUDE;
152 }
153 public String JavaDoc getAccept() {
154     return accept;
155 }
156 public String JavaDoc getCols() {
157     return cols;
158 }
159 public String JavaDoc getMaxlength() {
160     return maxlength;
161 }
162 public boolean getRedisplay() {
163     return redisplay;
164 }
165 public String JavaDoc getRows() {
166     return rows;
167 }
168 public String JavaDoc getSize() {
169     return cols;
170 }
171 public String JavaDoc getType() {
172     return type;
173 }
174 public String JavaDoc getValue() {
175     return value;
176 }
177 public void release() {
178     super.release();
179     access = READWRITE;
180     cols = null;
181     isRequired = false;
182     rows = null;
183     type = TEXT;
184     name = Constants.BEAN_KEY;
185
186     if (fieldTag!=null) {
187         fieldTag.release();
188         fieldTag = null;
189     }
190
191     error = null;
192 }
193 /**
194  * Retrieve the first error associated with the current property if there is one
195  */

196 protected String JavaDoc retrieveError() throws JspException JavaDoc {
197     ActionErrors errors = (ActionErrors) pageContext.getAttribute(Globals.ERROR_KEY, PageContext.REQUEST_SCOPE);
198     String JavaDoc error = null;
199     if (errors!=null && !errors.isEmpty()) {
200         Iterator JavaDoc it = errors.get(property);
201         if (it.hasNext()) {
202             ActionMessage report = (ActionMessage) it.next();
203             error = LayoutUtils.getLabel(pageContext, report.getKey(), report.getValues());
204         }
205     }
206     return error;
207     
208 }
209 public void setAccept(String JavaDoc accept) {
210     this.accept = accept;
211 }
212     /**
213  * Default Value: READWRITE
214  * Allowed values: READWRITE, READONLY, READSEND
215  *
216  * Setting access to READONLY will render the property value as text instead of a input field
217  * Setting access to READSEND will render the property value as text and as an hidden input field so that the server can get the value back
218      */

219 public void setAccess(String JavaDoc newAccess) {
220     access = READWRITE;
221     if (newAccess.equalsIgnoreCase(READONLY)) access = READONLY;
222     if (newAccess.equalsIgnoreCase(READSEND)) access = READSEND;
223     }
224
225 /**
226  * Insert the method's description here.
227  * Creation date: (19/02/01 11:29:27)
228  * @param newCols int
229  */

230 public void setCols(String JavaDoc newCols) {
231     cols = newCols;
232 }
233 /**
234  * If set to "TRUE" a red star is added after the field when the value is null
235  * A piece of Javascript adds or removes the start when the value changes
236  */

237 public void setIsRequired(String JavaDoc newIsRequired) {
238     isRequired = newIsRequired.equalsIgnoreCase("true");
239 }
240 public void setMaxlength(String JavaDoc maxlength) {
241     this.maxlength = maxlength;
242 }
243 public void setRedisplay(boolean redisplay) {
244     this.redisplay = redisplay;
245 }
246 /**
247  * Insert the method's description here.
248  * Creation date: (19/02/01 11:29:27)
249  * @param newRows int
250  */

251 public void setRows(String JavaDoc newRows) {
252     rows = newRows;
253 }
254 /**
255  * Insert the method's description here.
256  * Creation date: (19/02/01 11:29:27)
257  * @param newSize int
258  */

259 public void setSize(String JavaDoc newSize) {
260     cols = newSize;
261 }
262 /**
263  * Type of the input field <br>
264  *
265  * Default value: TEXT <br>
266  * Allowed values: PASSWORD, CHECKBOX, NUMBER, DATE, EMAIL <br>
267  *
268  * For NUMBER, DATE and EMAIL some Javascipt test is done to check
269  * if the value type in is correct or not.
270  * If not a red start is displayed after the field.
271  */

272 public void setType(String JavaDoc newType) {
273     if (newType.equalsIgnoreCase(TEXT)) type = TEXT;
274     if (newType.equalsIgnoreCase(TEXTAREA)) type = TEXTAREA;
275     if (newType.equalsIgnoreCase(PASSWORD)) type = PASSWORD;
276     if (newType.equalsIgnoreCase(CHECKBOX)) type = CHECKBOX;
277     if (newType.equalsIgnoreCase(NUMBER)) type = NUMBER;
278     if (newType.equalsIgnoreCase(DATE)) type = DATE;
279     if (newType.equalsIgnoreCase(EMAIL)) type = EMAIL;
280     
281 }
282 public void setValue(String JavaDoc value) {
283     this.value = value;
284 }
285     /**
286      * Gets the access.
287      * @return Returns a String
288      */

289     public String JavaDoc getAccess() {
290         return access;
291     }
292
293     /**
294      * Gets the isRequired.
295      * @return Returns a boolean
296      */

297     public String JavaDoc getIsRequired() {
298         return isRequired?"true":"false";
299     }
300     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) {
301         return Boolean.FALSE;
302     }
303     public Object JavaDoc processEndLayoutEvent(EndLayoutEvent in_event) {
304         return Boolean.FALSE;
305     }
306
307 }
308
Popular Tags