Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/tethys/tag/html/CheckboxTag.java


1   /*
2    * CheckboxTag.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.io.IOException;
13  
14  import javax.servlet.http.*;
15  import javax.servlet.jsp.*;
16  import javax.servlet.jsp.tagext.*;
17  
18  import com.aendvari.common.model.*;
19  
20  import com.aendvari.tethys.tag.*;
21  import com.aendvari.tethys.context.*;
22  
23  
24  /**
25   * The HTML <code>checkbox</code> tag.
26   *
27   * @author Scott Milne
28   * 
29   */
30  
31  public class CheckboxTag extends AbstractTag
32  {
33      /** The name of this field. */
34      private String name;
35  
36    /** The <code>checked</code> attribute.. */
37    private String checked = null;
38    
39    /** Does the user want to use the "hidden field" feature. */
40    private String useHiddenField = null;
41  
42  
43    /* Accessors */
44  
45    public String getName() { return name; }
46    public void setName(String param) { name = param; }
47  
48    public String getId() { return name; }
49    public void setId(String param) { name = param; }
50  
51    public String getChecked() { return checked; }
52    public void setChecked(String param) { checked = param; }
53  
54    public String getUseHiddenField() { return useHiddenField; }
55    public void setUseHiddenField(String param) { useHiddenField = param; }
56    public boolean useHiddenFieldFeature()
57    {
58      if (useHiddenField != null)
59      {
60        if (useHiddenField.equalsIgnoreCase("true") ||
61          useHiddenField.equalsIgnoreCase("yes") ||
62          useHiddenField.equalsIgnoreCase("on"))
63        {
64          return true;
65        }
66      }
67        
68      return false;
69    }
70    
71  
72      public int doStartTag() throws JspException
73      {
74        try
75        {
76        StringBuffer tagString = new StringBuffer("<input type=\"checkbox\" ");
77        StringBuffer hiddenTagString = new StringBuffer("<input type=\"hidden\" ");
78  
79        // if no name is given, use the path
80        if (name == null)
81        {
82          // establish tag context
83          establishModelContext();
84  
85          // build the name from the current context plus the path
86          name = modelContext.extendModelPath(getPath());
87  
88          // for visual purposes, convert all "." into "/"
89          // OSM supports both, but it looks better to have all the same
90          name = name.replace('.', '/');
91        }
92  
93        // if they want to use the hidden field feature, add the name to the hidden field
94        if (useHiddenFieldFeature())
95        {
96          hiddenTagString.append(" name=\"");
97          hiddenTagString.append(name);
98          hiddenTagString.append("\"");
99        }
100       // otherwise, add the name to the checkbox field
101       else
102       {
103         tagString.append(" name=\"");
104         tagString.append(name);
105         tagString.append("\"");
106       }
107 
108 
109       // if checked was provided, don't use the value
110       if (checked != null)
111       {
112         if (checked.equalsIgnoreCase("true") || 
113           checked.equalsIgnoreCase("yes") ||
114             checked.equalsIgnoreCase("on"))
115         {
116           if (useHiddenFieldFeature())
117           {
118               hiddenTagString.append(" value=\"true\"");
119           }
120 
121             tagString.append(" checked");
122         }
123         else
124         {
125           if (useHiddenFieldFeature())
126           {
127               hiddenTagString.append(" value=\"false\"");
128           }
129         }
130       }
131       // use the model value
132       else if (getPath() != null)
133       {
134         String value = extractModelValue(getPath());
135 
136         if (value.equalsIgnoreCase("true") || 
137           value.equalsIgnoreCase("yes") ||
138             value.equalsIgnoreCase("on"))
139         {
140           if (useHiddenFieldFeature())
141           {
142               hiddenTagString.append(" value=\"true\"");
143           }
144 
145             tagString.append(" checked");
146         }
147         else
148         {
149           if (useHiddenFieldFeature())
150           {
151               hiddenTagString.append(" value=\"false\"");
152           }
153         }
154       }
155       else
156       {
157         throw new JspTagException("CheckboxTag Error: You must have either a \"path\" or a \"value\" attribute.");
158       }
159 
160       // if we are using the hidden field feature, create the javascript required for it now
161       if (useHiddenFieldFeature())
162       {
163         tagString.append(" onclick=\"");
164         tagString.append("var theElm = form.elements['" + getName() + "']; if(this.checked){theElm.value='true';}else{theElm.value='false';}; ");
165         
166         if (getOnclick() != null)
167         {
168           tagString.append(getOnclick());
169         }
170 
171         tagString.append("\"");
172 
173         // clear the onclick so it's not added later
174         setOnclick(null);
175       }
176       
177   
178       // append the remaining attribtues to the checkbox
179 
180       if (getAccesskey() != null)
181       {
182         tagString.append(" accesskey=\"");
183         tagString.append(getAccesskey());
184         tagString.append("\"");
185       }
186 
187       if (getTabindex() != null)
188       {
189         tagString.append(" tabindex=\"");
190         tagString.append(getTabindex());
191         tagString.append("\"");
192       }
193       
194       // add the extra attributes (if any were defined)
195       tagString.append(generateScriptAttribtues());
196       tagString.append(generateStyleAttribute());
197 
198 
199       // finish off the checkbox tag
200       tagString.append(">");
201 
202 
203       // finish off the hidden tag
204       if (useHiddenFieldFeature())
205       {
206           hiddenTagString.append(">");
207       }
208       
209 
210       // print out the finalized hidden field (if being used)
211       if (useHiddenFieldFeature())
212       {
213         pageContext.getOut().write(hiddenTagString.toString());
214         pageContext.getOut().write("\n");
215       }
216 
217       // print out the finalized checkbox string
218       pageContext.getOut().write(tagString.toString());
219     }
220     catch (Exception e)
221     {
222       throw new JspTagException("CheckboxTag Error:" + e.toString());
223     }
224 
225     //
226     // COMPATIBILITY NOTICE
227     //
228     // If you are using JSP 1.1, this value is non-functional, use the following instead:
229     //
230     // EVAL_BODY_TAG
231     //
232     return EVAL_BODY_BUFFERED;
233   }
234 
235     public int doEndTag() throws JspException
236     {
237     if (bodyContent == null)
238     {
239       return EVAL_PAGE;
240     }
241 
242     try
243     {
244       pageContext.getOut().println(bodyContent.getString().trim());
245     }
246     catch (IOException e)
247     {
248       throw new JspTagException("CheckboxTag Error:" + e.toString());
249     }
250 
251     // Continue evaluating this page
252     return EVAL_PAGE;
253     }
254 
255     public void release()
256     {
257     super.release();
258     name = null;
259     checked = null;
260     useHiddenField = null;
261     }
262 }
263