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