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