KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tags > databinding > repeater > pad > Pad


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.repeater.pad;
19
20 import javax.servlet.jsp.JspException JavaDoc;
21
22 import org.apache.beehive.netui.tags.databinding.repeater.RepeaterComponent;
23 import org.apache.beehive.netui.util.Bundle;
24 import org.apache.beehive.netui.util.logging.Logger;
25
26 /**
27  * Sets the number of items rendered by a {@link org.apache.beehive.netui.tags.databinding.repeater.Repeater} tag.
28  * The <netui-data:pad> tag has the ability to turn an irregular data set in the <netui-data:repeater> tag
29  * into a regular data set through the use of the three attributes:
30  * <blockquote>
31  * <ul>
32  * <li><code>maxRepeat</code> - truncates the rendering of the data set</li>
33  * <li><code>minRepeat</code> - forces the &lt;netui-data:repeater> to render
34  * a minimum number of elements</li>
35  * <li><code>padText</code> - default text to render if the data set does not have the
36  * minimum number of elements</li>
37  * </ul>
38  * </blockquote>
39  * If the <code>padText</code> attribute is unset, the body of the &lt;netui-data:pad> tag is used as the default text.
40  * </p>
41  * <p>
42  * Note, the <code>container</code> data binding context should not be used inside of the Pad's <code>padText</code>
43  * attribute as binding to both the <code>item</code> and <code>index</code> could product unexpected results.
44  * </p>
45  * <p>
46  * In the following example, assume that the &lt;netui-data:repeater> tag iterates over a {@link java.util.List} object.
47  * The &lt;netui-data:pad> limits the iteration to three cycles, no matter how many elements are actually in the List.
48  * <pre>
49  * &lt;netui-data:repeater dataSource="pageFlow.myList">
50  * &lt;netui-data:pad maxRepeat="3"/>
51  * &lt;netui-data:repeaterItem>
52  * &lt;netui:span value="${container.item}" />
53  * &lt;/netui-data:repeaterItem>
54  * &lt;/netui-data:repeater>
55  * </pre>
56  * </p>
57  *
58  * @jsptagref.tagdescription <p>
59  * Sets the number of items rendered by a {@link org.apache.beehive.netui.tags.databinding.repeater.Repeater} tag.
60  * The &lt;netui-data:pad> tag has the ability to turn an irregular data set in the &lt;netui-data:repeater> tag
61  * into a regular data set through the use of the three attributes:
62  * <blockquote>
63  * <ul>
64  * <li><code>maxRepeat</code> - truncates the rendering of the data set</li>
65  * <li><code>minRepeat</code> - forces the &lt;netui-data:repeater> to render
66  * a minimum number of elements</li>
67  * <li><code>padText</code> - default text to render if the data set does not have the
68  * minimum number of elements</li>
69  * </ul>
70  * </blockquote>
71  * If the <code>padText</code> attribute is unset, the body of the &lt;netui-data:pad> tag is used as the default text.
72  * </p>
73  * <p>
74  * Note, the <code>container</code> data binding context should not be used inside of the Pad's <code>padText</code>
75  * attribute as binding to both the <code>item</code> and <code>index</code> could product unexpected results.
76  * </p>
77  *
78  * @example
79  * <p>
80  * In the following example, assume that the &lt;netui-data:repeater> tag iterates over a {@link java.util.List} object.
81  * The &lt;netui-data:pad> limits the iteration to three cycles, no matter how many elements are actually in the List.
82  * <pre>
83  * &lt;netui-data:repeater dataSource="pageFlow.myList">
84  * &lt;netui-data:pad maxRepeat="3"/>
85  * &lt;netui-data:repeaterItem>
86  * &lt;netui:span value="${container.item}" />
87  * &lt;/netui-data:repeaterItem>
88  * &lt;/netui-data:repeater>
89  * </pre>
90  * </p>
91  *
92  * @netui:tag name="pad"
93  * description="Repeater tag set tag that can pad an irregularly side data set in order to force the repeater to render a regular number of rows"
94  */

95 public class Pad
96     extends RepeaterComponent {
97
98     private static final Logger LOGGER = Logger.getInstance(Pad.class);
99
100     private String JavaDoc _padText = null;
101     private Integer JavaDoc _maxRepeat = null;
102     private Integer JavaDoc _minRepeat = null;
103
104     /**
105      * Get the name of this tag. This is used to identify the type of this tag
106      * for reporting tag errors.
107      *
108      * @return a constant String representing the name of this tag.
109      */

110     public String JavaDoc getTagName() {
111         return "Pad";
112     }
113
114     /**
115      * Set the text that will be used when padding a {@link org.apache.beehive.netui.tags.databinding.repeater.Repeater}
116      * that renders its body too few times.
117      *
118      * @param padText the text that is used to pad a repeater's content
119      * @jsptagref.attributedescription
120      * Set the text that will be used when padding a {@link org.apache.beehive.netui.tags.databinding.repeater.Repeater}
121      * that renders its body too few times.
122      * @jsptagref.attributesyntaxvalue <i>string_padText</i>
123      * @netui:attribute required="false" rtexprvalue="true"
124      */

125     public void setPadText(String JavaDoc padText) {
126         _padText = padText;
127     }
128
129     /**
130      * Set the maximum number of items that will be rendered by a &lt;netui-data:repeater> tag. If the size
131      * of the data set is greater than this, only this many items will be rendered. This value can be an integer
132      * or can be an expression that resolves to an integer.
133      *
134      * @param maxRepeat an integer or an expression that references an integer for the maximum number of items to render
135      * @jsptagref.attributedescription
136      * Set the maximum number of items that will be rendered by a &lt;netui-data:repeater> tag. If the size
137      * of the data set is greater than this, only this many items will be rendered. This value can be an integer
138      * or can be an expression that resolves to an integer.
139      * @jsptagref.attributesyntaxvalue <i>integer_or_expression_maxRepeat</i>
140      * @netui:attribute required="false" rtexprvalue="true"
141      */

142     public void setMaxRepeat(int maxRepeat) {
143         _maxRepeat = new Integer JavaDoc(maxRepeat);
144     }
145
146     /**
147      *
148      * @param minRepeat an integer or an expression that references an integer for the minimum number of items to render
149      * @jsptagref.attributedescription
150      * Set the minimum number of items that will be rendered by a &lt;netui-data:repeater> tag. If the size of
151      * the data set is smaller than this, the data set will be padded with the value of the <code>padText</code>
152      * attribute. This value can be an integer or can be an expression that resolves to an integer.
153      * @jsptagref.attributesyntaxvalue <i>integer_or_expression_minRepeat</i>
154      * @netui:attribute required="false" rtexprvalue="true"
155      */

156     public void setMinRepeat(int minRepeat) {
157         _minRepeat = new Integer JavaDoc(minRepeat);
158     }
159
160     protected boolean shouldRender() {
161         if(_padText == null)
162             return true;
163         else
164             return false;
165     }
166
167     public int doAfterBody()
168             throws JspException JavaDoc {
169         return SKIP_BODY;
170     }
171
172     /**
173      * Complete rendering the body of this tag. If the padText property was unset,
174      * the body of the tag is used as the pad text.
175      *
176      * @param state the current render state of the Repeater
177      * @return EVAL_PAGE
178      * @throws JspException if an error occurs that can not be reported on the page
179      */

180     protected int renderEndTag(int state)
181             throws JspException JavaDoc {
182
183         if(_padText == null && bodyContent != null) {
184             _padText = bodyContent.getString();
185         }
186
187         if(hasErrors()) {
188             reportErrors();
189             localRelease();
190             return EVAL_PAGE;
191         }
192
193         PadContext pc = new PadContext(_padText,
194                 (_minRepeat != null ? _minRepeat.intValue() : PadContext.DEFAULT_VALUE),
195                 (_maxRepeat != null ? _maxRepeat.intValue() : PadContext.DEFAULT_VALUE));
196
197         getRepeater().setPadContext(pc);
198
199         return EVAL_PAGE;
200     }
201
202     /**
203      * Reset all of the fields of this tag.
204      */

205     protected void localRelease() {
206         super.localRelease();
207         if(bodyContent != null)
208             bodyContent.clearBody();
209         _padText = null;
210         _maxRepeat = null;
211         _minRepeat = null;
212     }
213
214     /**
215      * Verify that the attributes set on the tag are correct. Check:
216      * <ul>
217      * <li>The minRepeat value can be converted into an integer</li>
218      * <li>The maxRepeat value can be converted into an integer</li>
219      * <li>The minRepeat value is less than or equal to the maxRepeat value</li>
220      * </ul>
221      *
222      * @throws JspException if an error occurs that can not be reported on the page
223      */

224     protected void verifyAttributes()
225             throws JspException JavaDoc {
226         if(_maxRepeat != null && _maxRepeat.intValue() <= 0) {
227             String JavaDoc msg = Bundle.getErrorString("Tags_Pad_intTooSmall", new Object JavaDoc[]{_maxRepeat, "maxRepeat"});
228             getRepeater().registerTagError(msg, null);
229         }
230
231         if(_minRepeat != null && _minRepeat.intValue() <= 0) {
232             String JavaDoc msg = Bundle.getErrorString("Tags_Pad_minRepeatIntTooSmall", new Object JavaDoc[]{_minRepeat, "minRepeat"});
233             getRepeater().registerTagError(msg, null);
234         }
235
236         // check composite properties
237
if(_maxRepeat != null &&
238                 _minRepeat != null &&
239                 _minRepeat.intValue() > _maxRepeat.intValue()) {
240             if(LOGGER.isWarnEnabled()) {
241                 String JavaDoc msg = "The value of maxRepeat (" + _maxRepeat + ") must be greater than the value of minRepeat (" +
242                         _minRepeat + ") on the Repeater Pad tag.";
243                 LOGGER.warn(msg);
244             }
245
246             String JavaDoc msg = Bundle.getErrorString("Tags_Pad_minGreaterThanMax", new Object JavaDoc[]{_maxRepeat, _minRepeat});
247             getRepeater().registerTagError(msg, null);
248         }
249     }
250 }
251
252
Popular Tags