KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > datagrid > ConfigurePager


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.databinding.datagrid;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException;
24 import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
25 import org.apache.beehive.netui.databinding.datagrid.api.rendering.PagerRenderer;
26 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.pager.FirstPreviousNextLastPagerRenderer;
27 import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.pager.PreviousNextPagerRenderer;
28 import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
29 import org.apache.beehive.netui.databinding.datagrid.runtime.util.ExtensionUtil;
30 import org.apache.beehive.netui.databinding.datagrid.api.pager.PagerModel;
31 import org.apache.beehive.netui.tags.AbstractSimpleTag;
32 import org.apache.beehive.netui.util.logging.Logger;
33 import org.apache.beehive.netui.util.Bundle;
34
35 /**
36  * <p>
37  * The configurePager tag is used to parameterize the data used to render a data grid's pager. This
38  * tag should be used inside of a &lt;netui-data:dataGrid&gt; tag. The tag supports adding a custom
39  * {@link PagerRenderer} via the {@link #setPagerRendererClass(String)}, overriding the default page
40  * size for a data grid via the {@link #setDefaultPageSize(Integer)} attribute, and setting
41  * the current data grid's page size via the {@link #setPageSize(int)} attribute. This tag produces
42  * no output and does not evaluate its body.
43  * </p>
44  * @jsptagref.tagdescription
45  * <p>
46  * The configurePager tag is used to parameterize the data used to render a data grid's pager. This
47  * tag should be used inside of a &lt;netui-data:dataGrid&gt; tag. The tag supports adding a custom
48  * {@link PagerRenderer} via the {@link #setPagerRendererClass(String)}, overriding the default page
49  * size for a data grid via the {@link #setDefaultPageSize(Integer)} attribute, and setting
50  * the current data grid's page size via the {@link #setPageSize(int)} attribute. This tag produces
51  * no output and does not evaluate its body.
52  * </p>
53  * @netui:tag name="configurePager" body-content="empty"
54  * description="Pager tag for the configuring and rendering the pager rendered in the NetUI data grid"
55  */

56 public class ConfigurePager
57         extends AbstractSimpleTag {
58
59     private static final Logger LOGGER = Logger.getInstance(ConfigurePager.class);
60     private static final String JavaDoc PAGER_FORMAT_FIRST_LAST_PREV_NEXT = "firstPrevNextLast";
61     private static final String JavaDoc PAGER_FORMAT_PREV_NEXT = "prevNext";
62
63     private Boolean JavaDoc _disableDefaultPager = null;
64     private Integer JavaDoc _pageSize = null;
65     private Integer JavaDoc _defaultPageSize = null;
66     private String JavaDoc _pagerFormat = null;
67     private String JavaDoc _pageHref = null;
68     private String JavaDoc _pageAction = null;
69     private String JavaDoc _pagerRendererClass = null;
70
71     /**
72      * The name of this tag; this value is used for error reporting.
73      * @return the String name of this tag
74      */

75     public String JavaDoc getTagName() {
76         return "ConfigurePager";
77     }
78
79     /**
80      * Set the page size for the current data grid. This value is the maximum number of data rows
81      * that will be rendered by a data grid. When this value is set, it overides the
82      * {@link #setDefaultPageSize(Integer)} attribute. If this value is not set, the
83      * {@link #setDefaultPageSize(Integer)} is used to determine the maximum size of a data grid's
84      * page. Use this value when a data grid allows a user to vary the number of rows in a grid.
85      * @jsptagref.attributedescription
86      * Set the page size for the current data grid. This value is the maximum number of data rows
87      * that will be rendered by a data grid. When this value is set, it overides the
88      * {@link #setDefaultPageSize(Integer)} attribute. If this value is not set, the
89      * {@link #setDefaultPageSize(Integer)} is used to determine the maximum size of a data grid's
90      * page. Use this value when a data grid allows a user to vary the number of rows in a grid.
91      * @jsptagref.attributesyntaxvalue <i>int_pageSize</i>
92      * @netui:attribute required="false" rtexprvalue="true"
93      * description="Set the maximum number of rows to render for a data grid."
94      */

95     public void setPageSize(int pageSize) {
96         _pageSize = new Integer JavaDoc(pageSize);
97     }
98
99     /**
100      * Set the appearance of the pager rendered by the data grid. This attribute allows the page author to
101      * choose from a set of pre-defined pager renderers which can be used to render paging UI
102      * in a data grid. The values include:
103      * <b>prevNext</b> which renders a pager as:<br/>
104      * <pre>
105      * Page # of # Previous Next
106      * </pre>
107      * where Previous and Next are anchors that can navigate to the previous and next pages. When
108      * there is no previous or next page, Previous or Next are displayed as literal text.
109      * <br/>
110      * <br/>
111      * <b>firstPrevNextLast</b> which renders a pager as:<br/>
112      * <pre>
113      * Page # of # First Previous Next Last
114      * </pre>
115      * where First, Previous, Next, and Last are anchors that can navigate to the first, previous, next, and last
116      * pages respectively. When the anchors would reference invalid pages, First, Previous, Nest, and Last
117      * are displayed as literal text.
118      * @jsptagref.attributedescription
119      * Set the style of the pager rendered by the data grid. This attribute allows the page author to
120      * choose from a set of pre-defined pager renderers which can be used to render paging UI
121      * in a data grid. The values include:
122      * <b>prevNext</b> which renders a pager as:<br/>
123      * <pre>
124      * Page # of # Previous Next
125      * </pre>
126      * where Previous and Next are anchors that can navigate to the previous and next pages. When
127      * there is no previous or next page, Previous or Next are displayed as literal text.
128      * <br/>
129      * <br/>
130      * <b>firstPrevNextLast</b> which renders a pager as:<br/>
131      * <pre>
132      * Page # of # First Previous Next Last
133      * </pre>
134      * where First, Previous, Next, and Last are anchors that can navigate to the first, previous, next, and last
135      * pages respectively. When the anchors would reference invalid pages, First, Previous, Nest, and Last
136      * are displayed as literal text.
137      * @jsptagref.attributesyntaxvalue <i>String_pagerStyle</i>
138      * @netui:attribute required="false" rtexprvalue="true"
139      * description="Set the appearance of the pager rendered by the data grid.
140      */

141     public void setPagerFormat(String JavaDoc pagerFormat) {
142         _pagerFormat = pagerFormat;
143     }
144
145     /**
146      * Set the HREF used to perform paging. When a pager renders anchors for navigating the data set
147      * inside of the data grid, this HREF when set is used as the request URI to perform the paging.
148      * Only one of this and the {@link #setPageAction(String)} may be set.
149      * @jsptagref.attributedescription
150      * Set the HREF used to perform paging. When a pager renders anchors for navigating the data set
151      * inside of the data grid, this HREF when set is used as the request URI to perform the paging.
152      * Only one of this and the {@link #setPageAction(String)} may be set.
153      * @jsptagref.attributesyntaxvalue <i>String_pageUri</i>
154      * @netui:attribute required="false" rtexprvalue="true" description="The HREF used when building HTML
155      * anchors that perform paging."
156      */

157     public void setPageHref(String JavaDoc pageHref) {
158         _pageHref = pageHref;
159     }
160
161     /**
162      * Set the action used to perform paging. When a pager renders anchors for navigating the
163      * data set in a data grid, this action is used as the request URI to perform the paging.
164      * Only one of this and the {@link #setPageHref(String)} may be set.
165      * @jsptagref.attributedescription
166      * Set the action used to perform paging. When a pager renders anchors for navigating the
167      * data set in a data grid, this action is used as the request URI to perform the paging.
168      * Only one of this and the {@link #setPageHref(String)} may be set.
169      * @jsptagref.attributesyntaxvalue <i>String_pageAction</i>
170      * @netui:attribute required="false" rtexprvalue="true"
171      * description="The action used when building HTML anchors that perrform paging.
172      */

173     public void setPageAction(String JavaDoc pageAction) {
174         _pageAction = pageAction;
175     }
176
177     /**
178      * Sets the value of an attribute that enables or disables rendering the data grid's default pager.
179      * When this value is set to <code>false</code>, the data grid will not render its pager and rendering
180      * is left to the page author. When rendering is disabled, a pager can be rendered using the
181      * {@link RenderPager} tag or manually in a page.
182      * @jsptagref.attributedescription
183      * Sets the value of an attribute that enables or disables rendering the data grid's default pager.
184      * When this value is set to <code>false</code>, the data grid will not render its pager and rendering
185      * is left to the page author. When rendering is disabled, a pager can be rendered using the
186      * {@link RenderPager} tag or manually in a page.
187      * @jsptagref.attributesyntaxvalue <i>boolean</i>
188      * @netui:attribute required="false" rtexprvalue="true"
189      * description="Boolean that enables / disables a data grid rendering the pager in its default location"
190      */

191     public void setDisableDefaultPager(boolean disableDefaultPager) {
192         _disableDefaultPager = Boolean.valueOf(disableDefaultPager);
193     }
194
195     /**
196      * Set a Java class name to create a {@link PagerRenderer} used to render a data grid's pager. The
197      * class referenced by this name must extend the {@link PagerRenderer} base class.
198      * @jsptagref.attributedescription
199      * Set a Java class name to create a {@link PagerRenderer} used to render a data grid's pager. The
200      * class referenced by this name must extend the {@link PagerRenderer} base class.
201      * @jsptagref.attributesyntaxvalue <i>string</i>
202      * @netui:attribute required="false" rtexprvalue="true"
203      * description="The data grid pager's custom pager render classname"
204      */

205     public void setPagerRendererClass(String JavaDoc pagerRendererClass) {
206         _pagerRendererClass = pagerRendererClass;
207     }
208
209     /**
210      * Set the default page size for the data grid. The grid has a default page size that is used
211      * when no other page size is specified; this value is {@link PagerModel#DEFAULT_PAGE_SIZE}.
212      * This value overrides that default so that the normal rendering of such a data grid is
213      * change in the absence of an override provided by the {@link #setPageSize(int)} attribute.
214      *
215      * @jsptagref.attributedescription
216      * Set the default page size for the data grid. The grid has a default page size that is used
217      * when no other page size is specified; this value is {@link PagerModel#DEFAULT_PAGE_SIZE}.
218      * This value overrides that default so that the normal rendering of such a data grid is
219      * change in the absence of an override provided by the {@link #setPageSize(int)} attribute.
220      * @jsptagref.attributesyntaxvalue <i>string</i>
221      * @netui:attribute required="false" rtexprvalue="true" description="The data grid pager's default page size"
222      */

223     public void setDefaultPageSize(Integer JavaDoc defaultPageSize) {
224         _defaultPageSize = defaultPageSize;
225     }
226
227     /**
228      * Execute the ConfigurePager tag. When the ConfigurePager tag runs, it applies its tag attributes
229      * onto a {@link DataGridTagModel} state object which is used when the data grid renders its pager.
230      * @throws JspException when errors occur when processing this tag's attribute values
231      */

232     public void doTag()
233         throws JspException JavaDoc {
234
235         DataGridTagModel dgm = DataGridUtil.getDataGridTagModel(getJspContext());
236         if(dgm == null)
237             throw new JspException JavaDoc(Bundle.getErrorString("DataGridTags_MissingDataGridModel"));
238
239         PagerModel pm = dgm.getState().getPagerModel();
240         assert pm != null;
241
242         if(_disableDefaultPager != null)
243             dgm.setDisableDefaultPagerRendering(_disableDefaultPager.booleanValue());
244
245         if(_pageSize != null)
246             pm.setPageSize(_pageSize.intValue());
247
248         if(_defaultPageSize != null)
249             pm.setDefaultPageSize(_defaultPageSize.intValue());
250
251         PagerRenderer pagerRenderer = null;
252         if(_pagerRendererClass != null) {
253             try {
254                 pagerRenderer = (PagerRenderer)ExtensionUtil.instantiateClass(_pagerRendererClass, PagerRenderer.class);
255                 assert pagerRenderer != null : "Expected a non-null pager renderer of type \"" + _pagerRendererClass + "\"";
256             }
257             catch(DataGridExtensionException e) {
258                 String JavaDoc msg = Bundle.getErrorString("ConfigurePager_CantCreateCustomPagerRenderer", new Object JavaDoc[]{e});
259                 throw new JspException JavaDoc(msg, e);
260             }
261         }
262         else if(_pagerFormat != null) {
263             if(_pagerFormat.equals(PAGER_FORMAT_FIRST_LAST_PREV_NEXT))
264                 pagerRenderer = new FirstPreviousNextLastPagerRenderer();
265             else if(_pagerFormat.equals(PAGER_FORMAT_PREV_NEXT))
266                 pagerRenderer = new PreviousNextPagerRenderer();
267             else
268                 throw new JspException JavaDoc(Bundle.getErrorString("ConfigurePager_UnsupportedPagerFormat", new Object JavaDoc[]{_pagerFormat}));
269         }
270
271         if(pagerRenderer != null)
272             dgm.setPagerRenderer(pagerRenderer);
273
274         if(_pageHref != null && _pageAction != null)
275             throw new JspException JavaDoc(Bundle.getErrorString("ConfigurePager_CantSetHrefAndAction"));
276
277         if(_pageHref == null && _pageAction == null) {
278             LOGGER.info("The configurePager tag has no page action or HREF specified; using the current request URI instead.");
279             _pageHref = ((HttpServletRequest JavaDoc)JspUtil.getRequest(getJspContext())).getRequestURI();
280         }
281
282         if(_pageHref != null)
283             pm.setPageHref(_pageHref);
284
285         if(_pageAction != null)
286             pm.setPageAction(_pageAction);
287     }
288 }
289
290
Popular Tags