KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > pets > presentation > components > PagedForeach


1 package org.apache.tapestry.pets.presentation.components;
2
3 import java.util.Iterator JavaDoc;
4
5 import org.apache.tapestry.AbstractComponent;
6 import org.apache.tapestry.IBinding;
7 import org.apache.tapestry.IMarkupWriter;
8 import org.apache.tapestry.IRequestCycle;
9 import org.apache.tapestry.Tapestry;
10
11 public class PagedForeach extends AbstractComponent
12 {
13     private Object JavaDoc _source;
14     private IBinding _valueBinding;
15     private IBinding _indexBinding;
16     private String JavaDoc _element;
17     private Object JavaDoc _value;
18     private int _index;
19     private boolean _rendering;
20     private int pageSize;
21     private PagedModel pagedModel;
22     private IBinding _pagedModelBinding;
23
24     public IBinding getIndexBinding()
25     {
26         return _indexBinding;
27     }
28
29     public void setIndexBinding(IBinding value)
30     {
31         _indexBinding = value;
32     }
33
34     /**
35     * Gets the source binding and returns an {@link Iterator}
36     * representing
37     * the values identified by the source. Returns an empty {@link Iterator}
38     * if the binding, or the binding value, is null.
39     *
40     * <p>Invokes {@link Tapestry#coerceToIterator(Object)} to perform
41     * the actual conversion.
42     *
43     **/

44     protected Iterator JavaDoc getSourceData()
45     {
46         if (_source == null)
47         {
48             return null;
49         }
50
51         return Tapestry.coerceToIterator(_source);
52     }
53
54     public IBinding getValueBinding()
55     {
56         return _valueBinding;
57     }
58
59     /**
60     * Gets the source binding and iterates through
61     * its values. For each, it updates the value binding and render's its wrapped elements.
62     *
63     **/

64     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
65     {
66         Iterator JavaDoc dataSource = getSourceData();
67
68         // The dataSource was either not convertable, or was empty.
69
if (dataSource == null)
70         {
71             return;
72         }
73
74         try
75         {
76             _rendering = true;
77             _value = null;
78             _index = 0;
79             pagedModel = (PagedModel) _pagedModelBinding.getObject("pagedModel", PagedModel.class);
80
81             pagedModel.setPageSize(getPageSize());
82
83             int startIndex = ((pagedModel.getPageToShow() - 1) * getPageSize());
84             int endIndex = (startIndex + getPageSize()) - 1;
85
86             boolean hasNext = dataSource.hasNext();
87
88             while (hasNext)
89             {
90                 _value = dataSource.next();
91                 hasNext = dataSource.hasNext();
92
93                 if ((_index >= startIndex) && (_index <= endIndex))
94                 {
95                     if (_indexBinding != null)
96                     {
97                         _indexBinding.setInt(_index);
98                     }
99
100                     if (_valueBinding != null)
101                     {
102                         _valueBinding.setObject(_value);
103                     }
104
105                     if (_element != null)
106                     {
107                         writer.begin(_element);
108                         renderInformalParameters(writer, cycle);
109                     }
110
111                     renderBody(writer, cycle);
112
113                     if (_element != null)
114                     {
115                         writer.end();
116                     }
117                 }
118
119                 _index++;
120             }
121
122             pagedModel.setNextPageNumber(pagedModel.getPageToShow() + 1);
123             pagedModel.setPreviousPageNumber(pagedModel.getPageToShow() - 1);
124             pagedModel.setItemCount(_index);
125         }
126         finally
127         {
128             _value = null;
129             _rendering = false;
130             pagedModel = null;
131         }
132     }
133
134     public void setValueBinding(IBinding value)
135     {
136         _valueBinding = value;
137     }
138
139     public IBinding getPagedModelBinding()
140     {
141         return _pagedModelBinding;
142     }
143
144     public void setPagedModelBinding(IBinding pagedModelBinding)
145     {
146         _pagedModelBinding = pagedModelBinding;
147     }
148
149     /**
150     * Returns the most recent value extracted from the source parameter.
151     *
152     * @throws RenderOnlyPropertyException if the Foreach is not currently rendering.
153     *
154     **/

155     public Object JavaDoc getValue()
156     {
157         if (!_rendering)
158         {
159             throw Tapestry.createRenderOnlyPropertyException(this, "value");
160         }
161
162         return _value;
163     }
164
165     public String JavaDoc getElement()
166     {
167         return _element;
168     }
169
170     public void setElement(String JavaDoc element)
171     {
172         _element = element;
173     }
174
175     public Object JavaDoc getSource()
176     {
177         return _source;
178     }
179
180     public void setSource(Object JavaDoc source)
181     {
182         _source = source;
183     }
184
185     /**
186     * The index number, within the {@link #getSource() source}, of the
187     * the current value.
188     *
189     * @throws RenderOnlyPropertyException if the Foreach is not currently rendering.
190     *
191     * @since 2.2
192     *
193     **/

194     public int getIndex()
195     {
196         if (!_rendering)
197         {
198             throw Tapestry.createRenderOnlyPropertyException(this, "value");
199         }
200
201         return _index;
202     }
203
204     public int getPageSize()
205     {
206         return pageSize;
207     }
208
209     public void setPageSize(int pageSize)
210     {
211         this.pageSize = pageSize;
212     }
213 }
214
Popular Tags