1 15 package org.apache.tapestry.components; 16 17 import java.util.Iterator ; 18 19 import org.apache.tapestry.AbstractComponent; 20 import org.apache.tapestry.IMarkupWriter; 21 import org.apache.tapestry.IRequestCycle; 22 import org.apache.tapestry.Tapestry; 23 import org.apache.tapestry.coerce.ValueConverter; 24 25 36 37 public abstract class Foreach extends AbstractComponent 38 { 39 private Object _value; 40 41 private int _index; 42 43 49 50 protected Iterator getSourceData() 51 { 52 Object source = getSource(); 53 54 if (source == null) 55 return null; 56 57 return (Iterator ) getValueConverter().coerceValue(source, Iterator .class); 58 } 59 60 protected void prepareForRender(IRequestCycle cycle) 61 { 62 _value = null; 63 _index = 0; 64 } 65 66 protected void cleanupAfterRender(IRequestCycle cycle) 67 { 68 _value = null; 69 } 70 71 75 76 protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) 77 { 78 Iterator dataSource = getSourceData(); 79 80 82 if (dataSource == null) 83 return; 84 85 boolean indexBound = isParameterBound("index"); 86 boolean valueBound = isParameterBound("value"); 87 88 String element = getElement(); 89 90 boolean hasNext = dataSource.hasNext(); 91 92 while (hasNext) 93 { 94 _value = dataSource.next(); 95 hasNext = dataSource.hasNext(); 96 97 if (indexBound) 98 setIndexParameter(_index); 99 100 if (valueBound) 101 setValueParameter(_value); 102 103 if (element != null) 104 { 105 writer.begin(element); 106 renderInformalParameters(writer, cycle); 107 } 108 109 renderBody(writer, cycle); 110 111 if (element != null) 112 writer.end(); 113 114 _index++; 115 } 116 117 } 118 119 125 126 public Object getValue() 127 { 128 if (!isRendering()) 129 throw Tapestry.createRenderOnlyPropertyException(this, "value"); 130 131 return _value; 132 } 133 134 public abstract String getElement(); 135 136 public abstract Object getSource(); 137 138 145 146 public int getIndex() 147 { 148 if (!isRendering()) 149 throw Tapestry.createRenderOnlyPropertyException(this, "index"); 150 151 return _index; 152 } 153 154 155 public abstract void setIndexParameter(int value); 156 157 158 public abstract void setValueParameter(Object value); 159 160 161 public abstract ValueConverter getValueConverter(); 162 } | Popular Tags |