KickJava   Java API By Example, From Geeks To Geeks.

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


1 package fr.improve.struts.taglib.layout.field;
2
3 import javax.servlet.jsp.JspException JavaDoc;
4
5 import org.apache.struts.taglib.html.BaseHandlerTag;
6
7 import fr.improve.struts.taglib.layout.util.LayoutUtils;
8 import fr.improve.struts.taglib.layout.util.TagUtils;
9 /**
10  * A simple checkbox tag.
11  * @author: Jean-NoŽl Ribette
12  */

13 public class CheckboxTag extends AbstractFieldTag {
14     org.apache.struts.taglib.html.CheckboxTag checkboxTag = new org.apache.struts.taglib.html.CheckboxTag();
15 protected boolean doBeforeValue() {
16     fieldTag = checkboxTag;
17     return true;
18 }
19 public int doStartInspectField() throws JspException JavaDoc {
20       // Maybe do something before displaying the value.
21
if (!doBeforeValue()) return SKIP_BODY;
22
23       doAfterValue();
24      
25       return EVAL_BODY_INCLUDE;
26 }
27
28  
29
30 protected Object JavaDoc getFieldValue() throws JspException JavaDoc {
31     Object JavaDoc lc_value = value;
32     if (lc_value==null) lc_value = "on";
33     switch (getFieldDisplayMode()) {
34         case MODE_EDIT :
35             break;
36         case MODE_NODISPLAY :
37             break;
38         case MODE_INSPECT :
39             return super.getFieldValue();
40     }
41     return lc_value;
42 }
43
44 protected void doAfterValue() throws JspException JavaDoc {
45     Object JavaDoc lc_value = value;
46     if (lc_value==null) lc_value = "on";
47     switch (getFieldDisplayMode()) {
48         case MODE_EDIT :
49             break;
50         case MODE_NODISPLAY :
51             break;
52         case MODE_INSPECT :
53             if (isChecked()) {
54                 StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
55                 buffer.append("<img SRC=\"");
56                 buffer.append(getSkin().getImageDirectory(pageContext.getRequest()));
57                 buffer.append("/");
58                 buffer.append(getSkin().getProperty("layout.checkbox.checked"));
59                 buffer.append("\" border=\"0\" alt=\"");
60                 buffer.append(getSkin().getProperty("layout.checkbox.checked.label"));
61                 buffer.append("\">");
62                 TagUtils.write(pageContext, buffer.toString());
63             } else {
64                 String JavaDoc lc_imgsrc = null;
65                 String JavaDoc lc_label = "unchecked";
66                 try {
67                     lc_imgsrc = getSkin().getProperty("layout.checkbox.unchecked");
68                     lc_label = getSkin().getProperty("layout.checkbox.unchecked.label");
69                 } catch(java.util.MissingResourceException JavaDoc me) {
70                     // Nothing
71
}
72                 if (null != lc_imgsrc && lc_imgsrc.length()!=0) {
73                     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
74                     buffer.append("<img SRC=\"");
75                     buffer.append(getSkin().getImageDirectory(pageContext.getRequest()));
76                     buffer.append("/");
77                     buffer.append(lc_imgsrc);
78                     buffer.append("\" border=\"0\" alt=\"");
79                     buffer.append(lc_label);
80                     buffer.append("\">");
81                     TagUtils.write(pageContext, buffer.toString());
82                 }
83             }
84         }
85     }
86     /**
87      * @see fr.improve.struts.taglib.layout.field.AbstractModeFieldTag#printSimpleHiddenValue(Object)
88      */

89     protected void printSimpleHiddenValue(Object JavaDoc in_value) throws JspException JavaDoc {
90         // A checkbox is a fucking widget: we must not render anytinh if it is not checked.
91
if (isChecked()) {
92             super.printSimpleHiddenValue(in_value);
93         }
94     }
95     
96     protected boolean isChecked() throws JspException JavaDoc {
97         Object JavaDoc lc_value = LayoutUtils.getBeanFromPageContext(pageContext, name, property);
98         return lc_value != null && (lc_value.toString().equals("on") || lc_value.toString().equals("yes") || lc_value.toString().equals("true"));
99     }
100     
101     protected void copyProperties(BaseHandlerTag in_dest) throws JspException JavaDoc {
102         super.copyProperties(in_dest);
103         checkboxTag.setName(getName());
104         checkboxTag.setProperty(getProperty());
105         checkboxTag.setValue(getValue());
106         checkboxTag.setReadonly(isReadonly());
107     }
108
109 }
110
Popular Tags