KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.field;
2
3 import java.lang.reflect.Array JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.List JavaDoc;
7
8 import javax.servlet.jsp.JspException JavaDoc;
9
10 import org.apache.struts.taglib.html.BaseHandlerTag;
11 import org.apache.struts.util.ResponseUtils;
12
13 import fr.improve.struts.taglib.layout.event.StartLayoutEvent;
14 import fr.improve.struts.taglib.layout.util.LayoutUtils;
15 import fr.improve.struts.taglib.layout.util.TagUtils;
16 /**
17  * Render a "select" tag with its nested "option" tags
18  * Works with the html and layout options & option tags
19  * @author: Jean-Noel Ribette
20  **/

21 public class SelectTag extends AbstractFieldTag implements ChoiceTag {
22     protected org.apache.struts.taglib.html.SelectTag selectTag =
23         new org.apache.struts.taglib.html.SelectTag();
24
25     protected String JavaDoc multiple;
26     protected String JavaDoc[] match;
27     protected boolean isMatched = false;
28
29     protected boolean isClosed = false;
30
31     private List JavaDoc errors = new ArrayList JavaDoc();
32
33     /**
34      * special default values for open selection<br>
35      * if one choice value equals to this value, a text input field is added after the select list.
36      */

37     public static final String JavaDoc OTHER_KEY = "struts.layout.other";
38     protected String JavaDoc otherKey = "struts.layout.other";
39     protected boolean need_other = false;
40     protected String JavaDoc otherProperty;
41     protected String JavaDoc otherValue;
42     
43     public boolean isMatched(String JavaDoc value) {
44         if ((match == null) || (value == null))
45             return (false);
46         for (int i = 0; i < match.length; i++) {
47             if (value.equals(match[i]))
48                 return (true);
49         }
50         return (false);
51     }
52     
53     public void addChoice(StringBuffer JavaDoc out_sb, Choice in_choice) throws JspException JavaDoc {
54         addChoice(out_sb, in_choice.getChoiceValue(), in_choice.getChoiceLabel());
55     }
56             
57     /**
58      * Generate the HTML code to add a choice.
59      *
60      * @param sb Buffer to print the HTML code to.
61      * @param value The value to send when this choice is selected
62      * @param label The label to display for this choice.
63      */

64     public void addChoice(StringBuffer JavaDoc sb, String JavaDoc value, String JavaDoc label)
65         throws JspException JavaDoc {
66             
67         if (isClosed) {
68             throw new IllegalStateException JavaDoc("Cannot add another option after nested text evaluation");
69         }
70             
71         switch (getFieldDisplayMode()) {
72             case MODE_EDIT :
73                 sb.append("<option value=\"");
74                 sb.append(value);
75                 sb.append("\"");
76                 if (isMatched(value))
77                     sb.append(" selected");
78                 sb.append(">");
79                 if (filter) {
80                     sb.append(ResponseUtils.filter(label));
81                 } else {
82                     sb.append(label);
83                 }
84                 sb.append("</option>\r\n");
85                 break;
86             case MODE_INSPECT :
87             case MODE_INSPECT_ONLY :
88             case MODE_INSPECT_PRESENT :
89                 if (isMatched(value)) {
90                     if (OTHER_KEY.equals(value)) {
91                         // sb.append(label);
92
// else {
93
// print the other value
94
sb.append("<span class=\"");
95                         sb.append(styleClass);
96                         sb.append("\">");
97                         if (otherValue != null) {
98                             sb.append(otherValue);
99                         } else {
100                             sb.append(LayoutUtils.getBeanFromPageContext(pageContext, name, otherProperty));
101                         }
102                         sb.append("</span>");
103                     } else {
104                         sb.append("<span class=\"");
105                         sb.append(styleClass);
106                         sb.append("\">");
107                         if (filter) {
108                             sb.append(ResponseUtils.filter(label));
109                         } else {
110                             sb.append(label);
111                         }
112                         sb.append("</span>");
113                     }
114                     isMatched = true;
115                 }
116
117         }
118
119     }
120
121     protected void doAfterValue() throws JspException JavaDoc {
122         if (!isClosed) {
123             isClosed = true;
124             
125             // Add a "other" option if needed
126
if (need_other) {
127                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
128                 addChoice(
129                     buffer,
130                     OTHER_KEY,
131                     LayoutUtils.getLabel(pageContext, OTHER_KEY, null));
132                 TagUtils.write(pageContext, buffer.toString());
133             }
134             
135             if (getFieldDisplayMode() == MODE_EDIT)
136                 selectTag.doEndTag();
137     
138             // eventually print a text input field if selection is open
139
if (need_other && getFieldDisplayMode() == MODE_EDIT) {
140                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
141                 buffer.append("&nbsp;<span id=\"");
142                 buffer.append(otherProperty);
143                 buffer.append("span\"");
144                 if (!isMatched(OTHER_KEY))
145                     buffer.append(" style=\"display:none\";");
146                 buffer.append("><span class=\"");
147                 buffer.append(styleClass);
148                 buffer.append("\">");
149                 buffer.append(LayoutUtils.getLabel(pageContext, otherKey, null));
150                 buffer.append(" :&nbsp;</span><input type=\"text\" name=\"");
151                 buffer.append(otherProperty);
152                 buffer.append("\" value=\"");
153                 if (otherValue != null)
154                     buffer.append(otherValue);
155                 else
156                     buffer.append(
157                         LayoutUtils.getBeanFromPageContext(pageContext, name, otherProperty));
158                 buffer.append("\" class=\"");
159                 buffer.append(styleClass);
160                 buffer.append("\"></span>");
161             }
162     
163         }
164     }
165     /**
166     * This method is called before displaying the value.
167     * This is the place to write something before the value, and set the fieldTag value.
168     * @return true - process the tag<br>
169     * false - skip the tag
170     */

171     protected boolean doBeforeValue() throws javax.servlet.jsp.JspException JavaDoc {
172         fieldTag = selectTag;
173         return true;
174     }
175     
176     /**
177      * Override default error handling.
178      */

179     protected int doStartEditMode() throws JspException JavaDoc {
180         errors = computeErrors();
181         if (isLayout()) {
182             beginFieldLayout();
183             beginFieldError(errors);
184         }
185         int lc_result = doStartEditField();
186         return lc_result;
187     }
188     
189     /**
190      * Override default to not display the value, but the label.
191      */

192     public int doStartInspectField() throws JspException JavaDoc {
193         // Maybe do something before dispaying the value.
194
if (!doBeforeValue()) return SKIP_BODY;
195     
196     return EVAL_BODY_INCLUDE;
197     }
198     public int doEndInspectField() throws JspException JavaDoc {
199         doAfterValue();
200         
201         if (!isMatched) {
202             Object JavaDoc lc_value = getFieldValue();
203             if (lc_value!=null) {
204                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
205                 buffer.append("<span class=\"");
206                 buffer.append(styleClass);
207                 buffer.append("\">");
208                 if (lc_value.getClass().isArray()) {
209                     int n = Array.getLength(lc_value);
210                     for (int i = 0; i < n; i++) {
211                         Object JavaDoc lc_item = Array.get(lc_value, i);
212                         if (lc_item!=null) {
213                             buffer.append(lc_item.toString());
214                         }
215                     }
216                 } else {
217                     buffer.append(lc_value.toString());
218                 }
219                 buffer.append("</span>");
220                 TagUtils.write(pageContext, buffer.toString());
221             }
222         }
223         return EVAL_PAGE;
224     }
225     
226     /**
227      * Override default error handling.
228      */

229     protected int doEndEditMode() throws JspException JavaDoc {
230         int lc_result = doEndEditField();
231         if (isLayout()) {
232             endFieldError(errors);
233             endFieldLayout();
234         }
235         return lc_result;
236     }
237     
238     public int doEndEditField() throws JspException JavaDoc {
239         doAfterValue();
240
241         return EVAL_PAGE;
242     }
243     public int doStartEditField() throws JspException JavaDoc {
244         // Maybe do something before dispaying the value
245
if (!doBeforeValue())
246             return SKIP_BODY;
247
248         // Initialize the tag
249
if (fieldTag == null)
250             throw new JspException JavaDoc(
251                 getClass().getName()
252                     + " should really set the fieldTag value in doBeforeValue() !");
253
254         // Initialize the field value.
255
//Object lc_value = getFieldValue();
256
//if (lc_value != null)
257
// value = lc_value.toString();
258
//else
259
// value = "";
260

261         // PENDING nice, but not that fast.
262
copyProperties(fieldTag);
263
264         //set the name of the javascript to use
265
boolean check = isRequired();
266         if (check) {
267             fieldTag.setOnchange(
268                 "checkValue(this, '" + property + "','TEXT'," + check + ");" +
269                 (onchange!=null ? onchange : "")
270                 );
271         }
272
273         fieldTag.doStartTag();
274
275         return EVAL_BODY_INCLUDE;
276     }
277     /**
278     * Return the value(s) that will be displayed.
279     */

280     protected java.lang.Object JavaDoc getFieldValue() throws JspException JavaDoc {
281         if (match != null)
282             return match;
283
284         Object JavaDoc lc_value = super.getFieldValue();
285         if (lc_value != null) try {
286             if (value!=null) {
287                 match = new String JavaDoc[1];
288                 match[0] = value;
289             } else {
290                 Object JavaDoc lc_bean = pageContext.findAttribute(name);
291                 match = LayoutUtils.getArrayProperty(lc_bean, property);
292             }
293         } catch (Exception JavaDoc e) {
294             TagUtils.saveException(pageContext, e);
295             throw new JspException JavaDoc(e.getMessage());
296         }
297         return lc_value;
298     }
299     public String JavaDoc getMultiple() {
300         return multiple;
301     }
302     /**
303      * Insert the method's description here.
304      * Creation date: (07/09/2001 10:27:22)
305      * @return java.lang.String
306      */

307     public java.lang.String JavaDoc getOtherKey() {
308         return otherKey;
309     }
310     /**
311      * Insert the method's description here.
312      * Creation date: (07/09/2001 10:27:22)
313      * @return java.lang.String
314      */

315     public java.lang.String JavaDoc getOtherProperty() {
316         return otherProperty;
317     }
318     /**
319      * Insert the method's description here.
320      * Creation date: (07/09/2001 10:27:22)
321      * @return java.lang.String
322      */

323     public java.lang.String JavaDoc getOtherValue() {
324         return otherValue;
325     }
326     public String JavaDoc getSize() {
327         return size;
328     }
329     protected void reset() {
330         super.reset();
331         match = null;
332         isMatched = false;
333         isClosed = false;
334         need_other = false;
335         errors.clear();
336     }
337     public void release() {
338         super.release();
339         
340         multiple = null;
341         onchange = null;
342
343         otherKey = "struts.layout.other";
344         otherValue = null;
345         otherProperty = null;
346     }
347     public void setMultiple(String JavaDoc multiple) {
348         this.multiple = multiple;
349
350     }
351     /**
352      * Insert the method's description here.
353      * Creation date: (07/09/2001 10:27:22)
354      * @param newOtherKey java.lang.String
355      */

356     public void setOtherKey(java.lang.String JavaDoc newOtherKey) {
357         otherKey = newOtherKey;
358         need_other = true;
359     }
360     /**
361      * Insert the method's description here.
362      * Creation date: (07/09/2001 10:27:22)
363      * @param newOtherProperty java.lang.String
364      */

365     public void setOtherProperty(java.lang.String JavaDoc newOtherProperty) {
366         otherProperty = newOtherProperty;
367         need_other = true;
368     }
369     /**
370      * Insert the method's description here.
371      * Creation date: (07/09/2001 10:27:22)
372      * @param newOtherValue java.lang.String
373      */

374     public void setOtherValue(java.lang.String JavaDoc newOtherValue) {
375         otherValue = newOtherValue;
376         need_other = true;
377     }
378     public void setSize(String JavaDoc size) {
379         this.size = size;
380     }
381     public void setValue(String JavaDoc value) {
382         this.value = value;
383     }
384     /**
385      * If multiple is set to false, render a SIMPLE property.
386      */

387     protected void printIndexedHiddenValue(Collection JavaDoc lc_collection) throws JspException JavaDoc {
388         if ("true".equalsIgnoreCase(multiple)) {
389             super.printIndexedHiddenValue(lc_collection);
390         } else {
391             if (!lc_collection.isEmpty()) {
392                 Object JavaDoc lc_value = lc_collection.iterator().next();
393                 printSimpleHiddenValue(lc_value);
394             }
395         }
396     }
397     
398     public String JavaDoc getValue() {
399         return "";
400     }
401     
402     /**
403      * @see fr.improve.struts.taglib.layout.event.LayoutEventListener#processStartLayoutEvent(StartLayoutEvent)
404      */

405     public Object JavaDoc processStartLayoutEvent(StartLayoutEvent in_event) throws JspException JavaDoc {
406         if (!isClosed) {
407             doAfterValue();
408         }
409         return super.processStartLayoutEvent(in_event);
410     }
411
412     protected void copyProperties(BaseHandlerTag in_dest) throws JspException JavaDoc {
413         super.copyProperties(in_dest);
414         selectTag.setProperty(getProperty());
415         selectTag.setValue(getValue());
416         selectTag.setName(getName());
417         selectTag.setMultiple(getMultiple());
418         selectTag.setSize(getSize());
419     }
420
421     /**
422      * Return false if the value is unset. Special case for arrays.
423      */

424     public boolean isFill(Object JavaDoc in_value) throws JspException JavaDoc {
425         // Null.
426
if (in_value==null) {
427             return false;
428         }
429         
430         // Not an array : value should not be an empty String.
431
if (!in_value.getClass().isArray()) {
432             return in_value.toString().length()!=0;
433         }
434         
435         // Array
436
Object JavaDoc[] lc_values = (Object JavaDoc[]) in_value;
437         
438         // Empty array is unset.
439
if (lc_values.length==0) {
440             return false;
441         }
442         
443         // Array with one empty element is unset.
444
if (lc_values.length==1) {
445             Object JavaDoc lc_firstValue = lc_values[0];
446             return lc_firstValue!=null && lc_firstValue.toString().length()!=0;
447         }
448         
449         // Array with many elements, assume true.
450
return true;
451     }
452 }
Popular Tags