1 package org.apache.tapestry.pets.presentation.components; 2 3 import java.util.Iterator ; 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 _source; 14 private IBinding _valueBinding; 15 private IBinding _indexBinding; 16 private String _element; 17 private Object _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 44 protected Iterator 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 64 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 65 { 66 Iterator dataSource = getSourceData(); 67 68 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 155 public Object getValue() 156 { 157 if (!_rendering) 158 { 159 throw Tapestry.createRenderOnlyPropertyException(this, "value"); 160 } 161 162 return _value; 163 } 164 165 public String getElement() 166 { 167 return _element; 168 } 169 170 public void setElement(String element) 171 { 172 _element = element; 173 } 174 175 public Object getSource() 176 { 177 return _source; 178 } 179 180 public void setSource(Object source) 181 { 182 _source = source; 183 } 184 185 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 |