KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > divpanel > DivPanel


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

18 package org.apache.beehive.netui.tags.divpanel;
19
20 import org.apache.beehive.netui.pageflow.requeststate.NameService;
21 import org.apache.beehive.netui.script.ExpressionUpdateException;
22 import org.apache.beehive.netui.script.IllegalExpressionException;
23 import org.apache.beehive.netui.tags.AbstractClassicTag;
24 import org.apache.beehive.netui.tags.ExpressionHandling;
25 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature;
26 import org.apache.beehive.netui.tags.javascript.IScriptReporter;
27 import org.apache.beehive.netui.tags.javascript.ScriptRequestState;
28 import org.apache.beehive.netui.tags.rendering.AbstractHtmlState;
29 import org.apache.beehive.netui.tags.rendering.DivTag;
30 import org.apache.beehive.netui.tags.rendering.TagRenderingBase;
31 import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
32 import org.apache.beehive.netui.util.Bundle;
33
34 import javax.servlet.ServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.jsp.JspException JavaDoc;
37
38 /**
39  * A DivPanel creates an HTML <div> tag that may contain additional <div> tags. There will only
40  * be a single div that is visible at a time.
41  * @jsptagref.tagdescription Creates an HTML <div> tag that may contain additional <div> tags. Only a single section will be visible at a time.
42  * @netui:tag name="divPanel"
43  * description="A divPanel is an placeholder which may contain multiple sections. Only a single section will be visible at a time."
44  */

45 public class DivPanel extends AbstractClassicTag
46 {
47     private String JavaDoc _tagId;
48     private String JavaDoc _firstPage;
49     private String JavaDoc _dataSource = null; // The name of the tree.
50
private DivTag.State _divState = new DivTag.State();
51
52     private final String JavaDoc JAVASCRIPT_CLASS = "NetUIDivPanel";
53
54     public static final String JavaDoc DIVPANEL_JAVASCRIPT_ATTR = "netui-div-panel";
55     public static final String JavaDoc DIVPANEL_FIRST_PAGE = "netui-div-panel-first";
56
57     public static final String JavaDoc DIVPANEL_DIV_ID = "netui_divpanel_";
58
59     public static String JavaDoc getCurrentPage(ServletRequest JavaDoc req, String JavaDoc tagId)
60     {
61         String JavaDoc reqId = DIVPANEL_DIV_ID + tagId;
62         String JavaDoc page = req.getParameter(reqId);
63         return page;
64     }
65
66     /**
67      * Returns the name of the Tag. This is used to
68      * identify the type of tag reporting errors.
69      * @return a constant string representing the name of the tag.
70      */

71     public String JavaDoc getTagName()
72     {
73         return "DivPanel";
74     }
75
76     /**
77      * Sets an expression which indentifies the DivPanelState which will store the state of the
78      * DivPanel between posts to the server.
79      * @param dataSource the tree attribute name
80      * @jsptagref.attributedescription An expression which identifies which DivPanelState object will store state between posts to the server.
81      * @jsptagref.databindable true
82      * @jsptagref.attributesyntaxvalue <i>expression</i>
83      * @netui:attribute description="Sets an expression which indentifies the DivPanelState storing the state of the
84      * DivPanel between posts."
85      */

86     public void setDataSource(String JavaDoc dataSource)
87     {
88         _dataSource = dataSource;
89     }
90
91     /**
92      * Set the ID of the tag.
93      * @param tagId the tagId.
94      * @jsptagref.attributedescription Set the ID of the &lt;div> tag
95      * @jsptagref.databindable true
96      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
97      * @netui:attribute required="true" rtexprvalue="true"
98      * description="Set the ID of the tag."
99      */

100     public void setTagId(String JavaDoc tagId)
101     {
102         _tagId = tagId;
103     }
104
105     /**
106      * Set the name of the first page to display.
107      * @param firstPage the name of the first page.
108      * @jsptagref.attributedescription Set the name of the first page to display.
109      * @jsptagref.databindable true
110      * @jsptagref.attributesyntaxvalue <i>string_or_expression</i>
111      * @netui:attribute rtexprvalue="true"
112      * description="Set the name of the first page to display."
113      */

114     public void setFirstPage(String JavaDoc firstPage)
115     {
116         _firstPage = firstPage;
117
118     }
119
120     /**
121      * Causes the content of the section to be rendered into a buffer.
122      * @return SKIP_BODY if the visible state is <code>false</code>,
123      * otherwise EVAL_BODY_BUFFERED to cause the body content to be buffered.
124      * @throws javax.servlet.jsp.JspException if there are errors.
125      */

126     public int doStartTag()
127             throws JspException JavaDoc
128     {
129         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
130
131         // if there was a dataSource defined we need to get it based upon the expression
132
// if the variable comes back null, we then create a DivPanelState and set it back
133
// to the property referred to by the expression.
134
DivPanelState state = null;
135         if (_dataSource != null) {
136             ExpressionHandling _expr;
137             _expr = new ExpressionHandling(this);
138             try {
139                 state = getState(_expr);
140             }
141             catch (IllegalExpressionException iee) {
142                 String JavaDoc s = Bundle.getString("TreeRootError", new Object JavaDoc[]{_dataSource, iee.getMessage()});
143                 registerTagError(s, null);
144                 return SKIP_BODY;
145             }
146             if (hasErrors())
147                 reportAndExit(SKIP_BODY);
148
149             // if we got here and the state is null then create a new divPanel, and push it back on
150
// the expression
151
if (state == null) {
152                 try {
153                     state = new DivPanelState();
154                     String JavaDoc datasource = "{" + _dataSource + "}";
155                     _expr.updateExpression(datasource, state, pageContext);
156                 }
157                 catch (ExpressionUpdateException e) {
158                     String JavaDoc s = Bundle.getString("Tags_UnableToWriteTree", new Object JavaDoc[]{_dataSource, e.getMessage()});
159                     registerTagError(s, null);
160                     reportErrors();
161                     return SKIP_BODY;
162                 }
163
164                 if (hasErrors())
165                     reportAndExit(SKIP_BODY);
166
167                 // name the divPanel so we can post state back to this state object.
168
NameService ns = NameService.instance(pageContext.getSession());
169                 ns.nameObject("DivPanel", state);
170                 ns.put(state);
171             }
172         }
173
174         WriteRenderAppender writer = new WriteRenderAppender(pageContext);
175
176         // verify hat we are in a container with run at client on...
177
IScriptReporter sr = getScriptReporter();
178         ScriptRequestState srs = ScriptRequestState.getScriptRequestState(req);
179         if (!srs.isFeatureWritten(CoreScriptFeature.DYNAMIC_INIT)) {
180             String JavaDoc s = Bundle.getString("Tags_DivPanelHtmlRunAtClient", null);
181             registerTagError(s, null);
182             reportAndExit(SKIP_BODY);
183         }
184         srs.writeFeature(sr, writer, CoreScriptFeature.DIVPANEL_INIT, true, false, null);
185
186         // figure out if there is a page to render
187
String JavaDoc page = _firstPage;
188         if (state != null) {
189             String JavaDoc fp = state.getFirstPage();
190             if (fp != null)
191                 page = fp;
192         }
193
194         if (hasErrors())
195             reportAndExit(EVAL_BODY_INCLUDE);
196
197         _divState.id = this.getIdForTagId(_tagId);
198         _divState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, DIVPANEL_JAVASCRIPT_ATTR, "true");
199         if (page != null)
200             _divState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, DIVPANEL_FIRST_PAGE, page);
201         if (state != null)
202             _divState.registerAttribute(AbstractHtmlState.ATTR_GENERAL, "netui:divName", state.getObjectName());
203
204         TagRenderingBase divRenderer = TagRenderingBase.Factory.getRendering(TagRenderingBase.DIV_TAG, req);
205         divRenderer.doStartTag(writer, _divState);
206         return EVAL_BODY_INCLUDE;
207     }
208
209     /**
210      * Stores the buffered body content into the <code>TEMPLATE_SECTIONS
211      * HashMap</code>. The buffered body is
212      * accessed by the template page to obtain
213      * the content for <code>IncludeSection</code> tags.
214      * @return EVAL_PAGE to continue evaluating the page.
215      * @throws JspException on error.
216      */

217     public int doEndTag()
218             throws JspException JavaDoc
219     {
220         HttpServletRequest JavaDoc req = (HttpServletRequest JavaDoc) pageContext.getRequest();
221         if (!hasErrors()) {
222             WriteRenderAppender writer = new WriteRenderAppender(pageContext);
223             TagRenderingBase divRenderer = TagRenderingBase.Factory.getRendering(TagRenderingBase.DIV_TAG, req);
224             divRenderer.doEndTag(writer);
225         }
226         localRelease();
227         return EVAL_PAGE;
228     }
229
230     protected void localRelease()
231     {
232         super.localRelease();
233         _tagId = null;
234         _divState.clear();
235         _firstPage = null;
236         _dataSource = null;
237     }
238
239     /**
240      */

241     protected DivPanelState getState(ExpressionHandling expr)
242             throws JspException JavaDoc
243     {
244         String JavaDoc datasource = "{" + _dataSource + "}";
245         Object JavaDoc state = expr.evaluateExpression(datasource, "dataSource", pageContext);
246         if (state == null || hasErrors()) {
247             return null;
248         }
249
250         if (!(state instanceof DivPanelState)) {
251             String JavaDoc s = Bundle.getString("Tags_DivPanelInvalidAttribute", _dataSource);
252             registerTagError(s, null);
253             return null;
254         }
255         return (DivPanelState) state;
256     }
257 }
258
Popular Tags