1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
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 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public abstract class Foreach extends AbstractComponent |
38 |
| { |
39 |
| private Object _value; |
40 |
| |
41 |
| private int _index; |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
85
| protected Iterator getSourceData()
|
51 |
| { |
52 |
85
| Object source = getSource();
|
53 |
| |
54 |
85
| if (source == null)
|
55 |
0
| return null;
|
56 |
| |
57 |
85
| return (Iterator) getValueConverter().coerceValue(source, Iterator.class);
|
58 |
| } |
59 |
| |
60 |
85
| protected void prepareForRender(IRequestCycle cycle)
|
61 |
| { |
62 |
85
| _value = null;
|
63 |
85
| _index = 0;
|
64 |
| } |
65 |
| |
66 |
85
| protected void cleanupAfterRender(IRequestCycle cycle)
|
67 |
| { |
68 |
85
| _value = null;
|
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
85
| protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
|
77 |
| { |
78 |
85
| Iterator dataSource = getSourceData();
|
79 |
| |
80 |
| |
81 |
| |
82 |
85
| if (dataSource == null)
|
83 |
0
| return;
|
84 |
| |
85 |
85
| boolean indexBound = isParameterBound("index");
|
86 |
85
| boolean valueBound = isParameterBound("value");
|
87 |
| |
88 |
85
| String element = getElement();
|
89 |
| |
90 |
85
| boolean hasNext = dataSource.hasNext();
|
91 |
| |
92 |
85
| while (hasNext)
|
93 |
| { |
94 |
1295
| _value = dataSource.next();
|
95 |
1295
| hasNext = dataSource.hasNext();
|
96 |
| |
97 |
1295
| if (indexBound)
|
98 |
31
| setIndexParameter(_index);
|
99 |
| |
100 |
1295
| if (valueBound)
|
101 |
1281
| setValueParameter(_value);
|
102 |
| |
103 |
1295
| if (element != null)
|
104 |
| { |
105 |
1261
| writer.begin(element);
|
106 |
1261
| renderInformalParameters(writer, cycle);
|
107 |
| } |
108 |
| |
109 |
1295
| renderBody(writer, cycle);
|
110 |
| |
111 |
1295
| if (element != null)
|
112 |
1261
| writer.end();
|
113 |
| |
114 |
1295
| _index++;
|
115 |
| } |
116 |
| |
117 |
| } |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
19
| public Object getValue()
|
127 |
| { |
128 |
19
| if (!isRendering())
|
129 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "value");
|
130 |
| |
131 |
19
| return _value;
|
132 |
| } |
133 |
| |
134 |
| public abstract String getElement(); |
135 |
| |
136 |
| public abstract Object getSource(); |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
6
| public int getIndex()
|
147 |
| { |
148 |
6
| if (!isRendering())
|
149 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "index");
|
150 |
| |
151 |
6
| 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 |
| } |