KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tag > panel > PanelTag


1 /**
2  * Feb 9, 2006
3  *
4  * Copyright 2004 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tag.panel;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25
26 import net.sf.uitags.tag.AbstractUiTag;
27 import net.sf.uitags.tagutil.AttributeSupport;
28 import net.sf.uitags.tagutil.AttributeSupportHelper;
29 import net.sf.uitags.tagutil.ScopedIdGenerator;
30 import net.sf.uitags.tagutil.TaglibProperties;
31 import net.sf.uitags.util.Template;
32
33 /**
34  * Tag that renders a button to display a panel containing user
35  * specified content.
36  *
37  * @author hgani
38  * @version $Id$
39  */

40 public class PanelTag extends AbstractUiTag implements AttributeSupport {
41
42   ///////////////////////////////
43
////////// Constants //////////
44
///////////////////////////////
45

46   /**
47    * Serial Version UID.
48    */

49   private static final long serialVersionUID = 100L;
50
51   /**
52    * Prefix for all javascript variables, functions, classes for this tag.
53    */

54   static final String JavaDoc JS_PREFIX = "uiPanel_";
55
56   /**
57    * Key of the scoped attribute that stores auto-incremented ID for
58    * instances of this tag handler.
59    */

60   private static final String JavaDoc TAG_INSTANCE_ID_KEY =
61       PanelTag.class.getName() + "instanceId";
62
63
64   ///////////////////////////////////////////////
65
////////// Property keys (constants) //////////
66
///////////////////////////////////////////////
67

68   /**
69    * See the corresponding property in the factory default config file.
70    */

71   private static final String JavaDoc PROP_CSS = "panel.class";
72
73   /**
74    * See the corresponding property in the factory default config file.
75    */

76   private static final String JavaDoc PROP_LISTENER = "panel.listener";
77   
78
79   ////////////////////////////
80
////////// Fields //////////
81
////////////////////////////
82

83   /**
84    * The 'class' tag attribute.
85    */

86   private String JavaDoc cssClass;
87
88   /**
89    * The 'listener' tag attribute.
90    */

91   private String JavaDoc listener;
92
93   /**
94    * The 'anchorTo' and 'anchorToName' tag attributes.
95    */

96   private Position positioningStrategy = null;
97
98   /**
99    * Helper for tag that allows arbitrary HTML attributes.
100    */

101   private AttributeSupportHelper attributeHelper;
102
103   /**
104    * List of code generated by child tags, which is to be included
105    * somewhere in the parent's generated code.
106    */

107   private List JavaDoc childJsCodeList;
108
109
110   ///////////////////////////////////////////
111
////////// Tag attribute setters //////////
112
///////////////////////////////////////////
113

114   /**
115    * Tag attribute setter.
116    *
117    * @param val value of the tag attribute
118    */

119   public void setId(String JavaDoc val) {
120     super.setId(val);
121   }
122
123   /** {@inheritDoc} */
124   public String JavaDoc getId() {
125     if (super.getId() == null) {
126       long instanceId = ScopedIdGenerator.nextId(
127           PageContext.REQUEST_SCOPE, TAG_INSTANCE_ID_KEY, this.pageContext) - 1;
128       setId(PanelTag.JS_PREFIX + getTagName() + instanceId);
129     }
130     return super.getId();
131   }
132
133   /**
134    * Tag attribute setter.
135    *
136    * @param val value of the tag attribute
137    */

138   public void setClass(String JavaDoc val) {
139     this.cssClass = val;
140   }
141
142   /**
143    * Tag attribute setter.
144    *
145    * @param val value of the tag attribute
146    */

147   public void setAnchorTo(String JavaDoc val) {
148     this.positioningStrategy = Position.getFromAnchorId(val);
149   }
150
151   /**
152    * Tag attribute setter.
153    *
154    * @param val value of the tag attribute
155    */

156   public void setAnchorToName(String JavaDoc val) {
157     this.positioningStrategy = Position.getFromAnchorName(val);
158   }
159
160   /**
161    * Tag attribute setter.
162    *
163    * @param val value of the tag attribute
164    */

165   public void setListener(String JavaDoc val) {
166     this.listener = val;
167   }
168
169
170   ///////////////////////////////
171
////////// Tag logic //////////
172
///////////////////////////////
173

174   /**
175    * Instructs the servlet engine to buffer the panel's content.
176    *
177    * @see javax.servlet.jsp.tagext.Tag#doStartTag()
178    * @return <code>EVAL_BODY_BUFFERED</code>
179    * @throws JspException to communicate error
180    */

181   public int doStartTag() throws JspException JavaDoc {
182     this.attributeHelper = new AttributeSupportHelper();
183     this.childJsCodeList = new ArrayList JavaDoc();
184
185     makeVisibleToChildren();
186     return EVAL_BODY_BUFFERED;
187   }
188
189   private String JavaDoc getContentAsString() {
190     // NOTE: body content might be null if there is no character between
191
// the start and end tag (empty body)
192
// this tag allows empty body to enable JSP developer to dynamically
193
// create the panel content (e.g. using Javascript)
194
if (this.bodyContent == null) {
195       return "";
196     }
197     return this.bodyContent.getString();
198   }
199
200   /**
201    * Renders the HTML code for the button and the Javascript code for the
202    * action when the button is clicked.
203    *
204    * @see javax.servlet.jsp.tagext.Tag#doEndTag()
205    * @return <code>EVAL_PAGE</code>
206    * @throws JspException to communicate error
207    */

208   public int doEndTag() throws JspException JavaDoc {
209     if (this.positioningStrategy != null) {
210       addChildJsCode(this.positioningStrategy.getJsCode());
211     }
212
213     TaglibProperties props = TaglibProperties.getInstance();
214     props.setRuntimeProperty(PROP_CSS, this.cssClass);
215     props.setRuntimeProperty(PROP_LISTENER, this.listener);
216
217     Template tpl = Template.forName(Template.PANEL);
218     tpl.map("id", getId());
219     tpl.map("class", props.get(PROP_CSS));
220     tpl.map("listener", props.get(PROP_LISTENER));
221     tpl.map("content", getContentAsString());
222     tpl.map("jsCodeList", this.childJsCodeList);
223     tpl.map("otherAttributes", this.attributeHelper.eval());
224     println(tpl.evalToString());
225
226     // so that when the tag is reused the widget ID gets generated again
227
setId(null);
228
229     makeInvisibleFromChildren();
230     return EVAL_PAGE;
231   }
232
233   /** {@inheritDoc} */
234   public void addAttribute(String JavaDoc attrName, String JavaDoc attrValue) {
235     this.attributeHelper.addAttribute(attrName, attrValue);
236   }
237
238   public void addChildJsCode(String JavaDoc code) {
239     this.childJsCodeList.add(code);
240   }
241
242
243   ///////////////////////////////////
244
////////// Inner classes //////////
245
///////////////////////////////////
246

247   /**
248    * The type of positioning.
249    *
250    * @author hgani
251    */

252   private static class Position {
253     /// Fields ///
254

255     // values for velocity templates
256
final String JavaDoc key;
257     final String JavaDoc value;
258
259     /// Constructors ///
260

261     /**
262      * Non-instantiable by client.
263      */

264     private Position(String JavaDoc key, String JavaDoc value) {
265       this.key = key;
266       this.value = value;
267     }
268
269     /// Methods ///
270

271     String JavaDoc getJsCode() {
272       Template tpl = Template.forName(Template.PANEL_ANCHOR);
273       tpl.map(this.key, this.value);
274       return tpl.evalToString();
275     }
276
277     static Position getFromAnchorId(String JavaDoc anchorId) {
278       return new Position("anchorId", anchorId);
279     }
280
281     static Position getFromAnchorName(String JavaDoc anchorName) {
282       return new Position("anchorName", anchorName);
283     }
284   }
285 }
286
Popular Tags