001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.databinding.datagrid;
020:
021: import javax.servlet.jsp.JspException;
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.beehive.netui.databinding.datagrid.api.exceptions.DataGridExtensionException;
025: import org.apache.beehive.netui.databinding.datagrid.api.rendering.DataGridTagModel;
026: import org.apache.beehive.netui.databinding.datagrid.api.rendering.PagerRenderer;
027: import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.pager.FirstPreviousNextLastPagerRenderer;
028: import org.apache.beehive.netui.databinding.datagrid.runtime.rendering.pager.PreviousNextPagerRenderer;
029: import org.apache.beehive.netui.databinding.datagrid.runtime.util.JspUtil;
030: import org.apache.beehive.netui.databinding.datagrid.runtime.util.ExtensionUtil;
031: import org.apache.beehive.netui.databinding.datagrid.api.pager.PagerModel;
032: import org.apache.beehive.netui.tags.AbstractSimpleTag;
033: import org.apache.beehive.netui.util.logging.Logger;
034: import org.apache.beehive.netui.util.Bundle;
035:
036: /**
037: * <p>
038: * The configurePager tag is used to parameterize the data used to render a data grid's pager. This
039: * tag should be used inside of a <netui-data:dataGrid> tag. The tag supports adding a custom
040: * {@link PagerRenderer} via the {@link #setPagerRendererClass(String)}, overriding the default page
041: * size for a data grid via the {@link #setDefaultPageSize(Integer)} attribute, and setting
042: * the current data grid's page size via the {@link #setPageSize(int)} attribute. This tag produces
043: * no output and does not evaluate its body.
044: * </p>
045: * @jsptagref.tagdescription
046: * <p>
047: * The configurePager tag is used to parameterize the data used to render a data grid's pager. This
048: * tag should be used inside of a <netui-data:dataGrid> tag. The tag supports adding a custom
049: * {@link PagerRenderer} via the {@link #setPagerRendererClass(String)}, overriding the default page
050: * size for a data grid via the {@link #setDefaultPageSize(Integer)} attribute, and setting
051: * the current data grid's page size via the {@link #setPageSize(int)} attribute. This tag produces
052: * no output and does not evaluate its body.
053: * </p>
054: * @netui:tag name="configurePager" body-content="empty"
055: * description="Pager tag for the configuring and rendering the pager rendered in the NetUI data grid"
056: */
057: public class ConfigurePager extends AbstractSimpleTag {
058:
059: private static final Logger LOGGER = Logger
060: .getInstance(ConfigurePager.class);
061: private static final String PAGER_FORMAT_FIRST_LAST_PREV_NEXT = "firstPrevNextLast";
062: private static final String PAGER_FORMAT_PREV_NEXT = "prevNext";
063:
064: private Boolean _disableDefaultPager = null;
065: private Integer _pageSize = null;
066: private Integer _defaultPageSize = null;
067: private String _pagerFormat = null;
068: private String _pageHref = null;
069: private String _pageAction = null;
070: private String _pagerRendererClass = null;
071:
072: /**
073: * The name of this tag; this value is used for error reporting.
074: * @return the String name of this tag
075: */
076: public String getTagName() {
077: return "ConfigurePager";
078: }
079:
080: /**
081: * Set the page size for the current data grid. This value is the maximum number of data rows
082: * that will be rendered by a data grid. When this value is set, it overides the
083: * {@link #setDefaultPageSize(Integer)} attribute. If this value is not set, the
084: * {@link #setDefaultPageSize(Integer)} is used to determine the maximum size of a data grid's
085: * page. Use this value when a data grid allows a user to vary the number of rows in a grid.
086: * @jsptagref.attributedescription
087: * Set the page size for the current data grid. This value is the maximum number of data rows
088: * that will be rendered by a data grid. When this value is set, it overides the
089: * {@link #setDefaultPageSize(Integer)} attribute. If this value is not set, the
090: * {@link #setDefaultPageSize(Integer)} is used to determine the maximum size of a data grid's
091: * page. Use this value when a data grid allows a user to vary the number of rows in a grid.
092: * @jsptagref.attributesyntaxvalue <i>int_pageSize</i>
093: * @netui:attribute required="false" rtexprvalue="true"
094: * description="Set the maximum number of rows to render for a data grid."
095: */
096: public void setPageSize(int pageSize) {
097: _pageSize = new Integer(pageSize);
098: }
099:
100: /**
101: * Set the appearance of the pager rendered by the data grid. This attribute allows the page author to
102: * choose from a set of pre-defined pager renderers which can be used to render paging UI
103: * in a data grid. The values include:
104: * <b>prevNext</b> which renders a pager as:<br/>
105: * <pre>
106: * Page # of # Previous Next
107: * </pre>
108: * where Previous and Next are anchors that can navigate to the previous and next pages. When
109: * there is no previous or next page, Previous or Next are displayed as literal text.
110: * <br/>
111: * <br/>
112: * <b>firstPrevNextLast</b> which renders a pager as:<br/>
113: * <pre>
114: * Page # of # First Previous Next Last
115: * </pre>
116: * where First, Previous, Next, and Last are anchors that can navigate to the first, previous, next, and last
117: * pages respectively. When the anchors would reference invalid pages, First, Previous, Nest, and Last
118: * are displayed as literal text.
119: * @jsptagref.attributedescription
120: * Set the style of the pager rendered by the data grid. This attribute allows the page author to
121: * choose from a set of pre-defined pager renderers which can be used to render paging UI
122: * in a data grid. The values include:
123: * <b>prevNext</b> which renders a pager as:<br/>
124: * <pre>
125: * Page # of # Previous Next
126: * </pre>
127: * where Previous and Next are anchors that can navigate to the previous and next pages. When
128: * there is no previous or next page, Previous or Next are displayed as literal text.
129: * <br/>
130: * <br/>
131: * <b>firstPrevNextLast</b> which renders a pager as:<br/>
132: * <pre>
133: * Page # of # First Previous Next Last
134: * </pre>
135: * where First, Previous, Next, and Last are anchors that can navigate to the first, previous, next, and last
136: * pages respectively. When the anchors would reference invalid pages, First, Previous, Nest, and Last
137: * are displayed as literal text.
138: * @jsptagref.attributesyntaxvalue <i>String_pagerStyle</i>
139: * @netui:attribute required="false" rtexprvalue="true"
140: * description="Set the appearance of the pager rendered by the data grid.
141: */
142: public void setPagerFormat(String pagerFormat) {
143: _pagerFormat = pagerFormat;
144: }
145:
146: /**
147: * Set the HREF used to perform paging. When a pager renders anchors for navigating the data set
148: * inside of the data grid, this HREF when set is used as the request URI to perform the paging.
149: * Only one of this and the {@link #setPageAction(String)} may be set.
150: * @jsptagref.attributedescription
151: * Set the HREF used to perform paging. When a pager renders anchors for navigating the data set
152: * inside of the data grid, this HREF when set is used as the request URI to perform the paging.
153: * Only one of this and the {@link #setPageAction(String)} may be set.
154: * @jsptagref.attributesyntaxvalue <i>String_pageUri</i>
155: * @netui:attribute required="false" rtexprvalue="true" description="The HREF used when building HTML
156: * anchors that perform paging."
157: */
158: public void setPageHref(String pageHref) {
159: _pageHref = pageHref;
160: }
161:
162: /**
163: * Set the action used to perform paging. When a pager renders anchors for navigating the
164: * data set in a data grid, this action is used as the request URI to perform the paging.
165: * Only one of this and the {@link #setPageHref(String)} may be set.
166: * @jsptagref.attributedescription
167: * Set the action used to perform paging. When a pager renders anchors for navigating the
168: * data set in a data grid, this action is used as the request URI to perform the paging.
169: * Only one of this and the {@link #setPageHref(String)} may be set.
170: * @jsptagref.attributesyntaxvalue <i>String_pageAction</i>
171: * @netui:attribute required="false" rtexprvalue="true"
172: * description="The action used when building HTML anchors that perrform paging.
173: */
174: public void setPageAction(String pageAction) {
175: _pageAction = pageAction;
176: }
177:
178: /**
179: * Sets the value of an attribute that enables or disables rendering the data grid's default pager.
180: * When this value is set to <code>false</code>, the data grid will not render its pager and rendering
181: * is left to the page author. When rendering is disabled, a pager can be rendered using the
182: * {@link RenderPager} tag or manually in a page.
183: * @jsptagref.attributedescription
184: * Sets the value of an attribute that enables or disables rendering the data grid's default pager.
185: * When this value is set to <code>false</code>, the data grid will not render its pager and rendering
186: * is left to the page author. When rendering is disabled, a pager can be rendered using the
187: * {@link RenderPager} tag or manually in a page.
188: * @jsptagref.attributesyntaxvalue <i>boolean</i>
189: * @netui:attribute required="false" rtexprvalue="true"
190: * description="Boolean that enables / disables a data grid rendering the pager in its default location"
191: */
192: public void setDisableDefaultPager(boolean disableDefaultPager) {
193: _disableDefaultPager = Boolean.valueOf(disableDefaultPager);
194: }
195:
196: /**
197: * Set a Java class name to create a {@link PagerRenderer} used to render a data grid's pager. The
198: * class referenced by this name must extend the {@link PagerRenderer} base class.
199: * @jsptagref.attributedescription
200: * Set a Java class name to create a {@link PagerRenderer} used to render a data grid's pager. The
201: * class referenced by this name must extend the {@link PagerRenderer} base class.
202: * @jsptagref.attributesyntaxvalue <i>string</i>
203: * @netui:attribute required="false" rtexprvalue="true"
204: * description="The data grid pager's custom pager render classname"
205: */
206: public void setPagerRendererClass(String pagerRendererClass) {
207: _pagerRendererClass = pagerRendererClass;
208: }
209:
210: /**
211: * Set the default page size for the data grid. The grid has a default page size that is used
212: * when no other page size is specified; this value is {@link PagerModel#DEFAULT_PAGE_SIZE}.
213: * This value overrides that default so that the normal rendering of such a data grid is
214: * change in the absence of an override provided by the {@link #setPageSize(int)} attribute.
215: *
216: * @jsptagref.attributedescription
217: * Set the default page size for the data grid. The grid has a default page size that is used
218: * when no other page size is specified; this value is {@link PagerModel#DEFAULT_PAGE_SIZE}.
219: * This value overrides that default so that the normal rendering of such a data grid is
220: * change in the absence of an override provided by the {@link #setPageSize(int)} attribute.
221: * @jsptagref.attributesyntaxvalue <i>string</i>
222: * @netui:attribute required="false" rtexprvalue="true" description="The data grid pager's default page size"
223: */
224: public void setDefaultPageSize(Integer defaultPageSize) {
225: _defaultPageSize = defaultPageSize;
226: }
227:
228: /**
229: * Execute the ConfigurePager tag. When the ConfigurePager tag runs, it applies its tag attributes
230: * onto a {@link DataGridTagModel} state object which is used when the data grid renders its pager.
231: * @throws JspException when errors occur when processing this tag's attribute values
232: */
233: public void doTag() throws JspException {
234:
235: DataGridTagModel dgm = DataGridUtil
236: .getDataGridTagModel(getJspContext());
237: if (dgm == null)
238: throw new JspException(
239: Bundle
240: .getErrorString("DataGridTags_MissingDataGridModel"));
241:
242: PagerModel pm = dgm.getState().getPagerModel();
243: assert pm != null;
244:
245: if (_disableDefaultPager != null)
246: dgm.setDisableDefaultPagerRendering(_disableDefaultPager
247: .booleanValue());
248:
249: if (_pageSize != null)
250: pm.setPageSize(_pageSize.intValue());
251:
252: if (_defaultPageSize != null)
253: pm.setDefaultPageSize(_defaultPageSize.intValue());
254:
255: PagerRenderer pagerRenderer = null;
256: if (_pagerRendererClass != null) {
257: try {
258: pagerRenderer = (PagerRenderer) ExtensionUtil
259: .instantiateClass(_pagerRendererClass,
260: PagerRenderer.class);
261: assert pagerRenderer != null : "Expected a non-null pager renderer of type \""
262: + _pagerRendererClass + "\"";
263: } catch (DataGridExtensionException e) {
264: String msg = Bundle.getErrorString(
265: "ConfigurePager_CantCreateCustomPagerRenderer",
266: new Object[] { e });
267: JspException jsp = new JspException(msg, e);
268:
269: // todo: future cleanup
270: // The 2.5 Servlet api will set the initCause in the Throwable superclass during construction,
271: // this will cause an IllegalStateException on the following call.
272: if (jsp.getCause() == null) {
273: jsp.initCause(e);
274: }
275: throw jsp;
276: }
277: } else if (_pagerFormat != null) {
278: if (_pagerFormat.equals(PAGER_FORMAT_FIRST_LAST_PREV_NEXT))
279: pagerRenderer = new FirstPreviousNextLastPagerRenderer();
280: else if (_pagerFormat.equals(PAGER_FORMAT_PREV_NEXT))
281: pagerRenderer = new PreviousNextPagerRenderer();
282: else
283: throw new JspException(Bundle.getErrorString(
284: "ConfigurePager_UnsupportedPagerFormat",
285: new Object[] { _pagerFormat }));
286: }
287:
288: if (pagerRenderer != null)
289: dgm.setPagerRenderer(pagerRenderer);
290:
291: if (_pageHref != null && _pageAction != null)
292: throw new JspException(
293: Bundle
294: .getErrorString("ConfigurePager_CantSetHrefAndAction"));
295:
296: if (_pageHref == null && _pageAction == null) {
297: LOGGER
298: .info("The configurePager tag has no page action or HREF specified; using the current request URI instead.");
299: _pageHref = JspUtil.getRequest(getJspContext())
300: .getRequestURI();
301: }
302:
303: if (_pageHref != null)
304: pm.setPageHref(_pageHref);
305:
306: if (_pageAction != null)
307: pm.setPageAction(_pageAction);
308: }
309: }
|