KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > servlet > http > jsp > tagext > tree > BeanContextTreeTag


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.servlet.http.jsp.tagext.tree;
8
9 import java.beans.beancontext.BeanContext JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Vector JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
16
17 import org.apache.struts.util.RequestUtils;
18
19
20 /**
21  * Description of the Class
22  *
23  * @author Laurent Etiemble
24  * @version $Revision: 1.1 $
25  * @todo Javadoc to complete
26  * @jsp:tag name="generateTree"
27  * body-content="empty"
28  */

29 public class BeanContextTreeTag extends TagSupport JavaDoc
30 {
31    /** Description of the Field */
32    protected String JavaDoc id = null;
33    /** Should we ignore missing beans and simply output nothing? */
34    protected boolean ignore = false;
35    /** Name of the bean that contains the data we will be rendering. */
36    protected String JavaDoc name = null;
37    /** Description of the Field */
38    protected String JavaDoc property = null;
39    /** The scope to be searched to retrieve the specified bean. */
40    protected String JavaDoc scope = null;
41
42
43    /**
44     * Description of the Method
45     *
46     * @return Description of the Return Value
47     * @exception JspException Description of the Exception
48     */

49    public int doStartTag()
50       throws JspException JavaDoc
51    {
52       // Look up the requested bean (if necessary)
53
BeanContext JavaDoc context = null;
54
55       if (property != null)
56       {
57          context = (BeanContext JavaDoc) RequestUtils.lookup(pageContext, name, property, scope);
58       }
59       else
60       {
61          context = (BeanContext JavaDoc) RequestUtils.lookup(pageContext, name, scope);
62       }
63
64       if (ignore)
65       {
66          if (context == null)
67          {
68             return (SKIP_BODY);
69          }
70          // Nothing to output
71
}
72
73       // Create the beancontext collection
74
Collection JavaDoc collection = populate(context, "");
75       pageContext.setAttribute(id, collection);
76
77       // Continue processing this page
78
return (SKIP_BODY);
79    }
80
81
82    /**
83     * Gets the id attribute of the BeanContextTreeTag object
84     *
85     * @return The id value
86     * @jsp:attribute name="id"
87     * required="true"
88     * rtexprvalue="true"
89     */

90    public String JavaDoc getId()
91    {
92       return (this.id);
93    }
94
95
96    /**
97     * Gets the ignore attribute of the BeanContextTreeTag object
98     *
99     * @return The ignore value
100     * @jsp:attribute name="ignore"
101     * required="false"
102     * rtexprvalue="true"
103     */

104    public boolean getIgnore()
105    {
106       return (this.ignore);
107    }
108
109
110    /**
111     * Getter for the name attribute
112     *
113     * @return The value of name attribute
114     * @jsp:attribute name="name"
115     * required="true"
116     * rtexprvalue="true"
117     */

118    public String JavaDoc getName()
119    {
120       return (this.name);
121    }
122
123
124    /**
125     * Gets the property attribute of the BeanContextTreeTag object
126     *
127     * @return The property value
128     * @jsp:attribute name="property"
129     * required="false"
130     * rtexprvalue="true"
131     */

132    public String JavaDoc getProperty()
133    {
134       return (this.property);
135    }
136
137
138    /**
139     * Getter for the scope attribute
140     *
141     * @return The value of scope attribute
142     * @jsp:attribute name="scope"
143     * required="false"
144     * rtexprvalue="true"
145     */

146    public String JavaDoc getScope()
147    {
148       return (this.scope);
149    }
150
151
152    /** Description of the Method */
153    public void release()
154    {
155       super.release();
156       ignore = false;
157       name = null;
158       property = null;
159       scope = null;
160       id = null;
161    }
162
163
164    /**
165     * Sets the id attribute of the BeanContextTreeTag object
166     *
167     * @param id The new id value
168     */

169    public void setId(String JavaDoc id)
170    {
171       this.id = id;
172    }
173
174
175    /**
176     * Setter for the ignore attribute
177     *
178     * @param ignore The new value for ignore attribute
179     */

180    public void setIgnore(boolean ignore)
181    {
182       this.ignore = ignore;
183    }
184
185
186    /**
187     * Setter for the name attribute
188     *
189     * @param name The new value for name attribute
190     */

191    public void setName(String JavaDoc name)
192    {
193       this.name = name;
194    }
195
196
197    /**
198     * Sets the property attribute of the BeanContextTreeTag object
199     *
200     * @param property The new property value
201     */

202    public void setProperty(String JavaDoc property)
203    {
204       this.property = property;
205    }
206
207
208    /**
209     * Setter for the scope attribute
210     *
211     * @param scope The new value for scope attribute
212     */

213    public void setScope(String JavaDoc scope)
214    {
215       this.scope = scope;
216    }
217
218
219    /**
220     * Description of the Method
221     *
222     * @param context Description of the Parameter
223     * @param depth Description of the Parameter
224     * @return Description of the Return Value
225     */

226    protected Collection JavaDoc populate(BeanContext JavaDoc context, String JavaDoc depth)
227    {
228       String JavaDoc newDepth = depth;
229       Collection JavaDoc result = new Vector JavaDoc();
230
231       Iterator JavaDoc it = context.iterator();
232       while (it.hasNext())
233       {
234          BeanContext JavaDoc bc = (BeanContext JavaDoc) it.next();
235          IndentedObject o = new IndentedObject();
236          o.setObject(bc);
237          o.setDepth(depth);
238          if (it.hasNext())
239          {
240             newDepth = depth + "1";
241          }
242          else
243          {
244             newDepth = depth + "0";
245             o.setLast(true);
246          }
247          result.add(o);
248
249          Collection JavaDoc collection = populate(bc, newDepth);
250          result.addAll(collection);
251       }
252       return result;
253    }
254 }
255
Popular Tags