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 |
70
| protected Iterator getSourceData()
|
53 |
| { |
54 |
70
| Object source = null;
|
55 |
| |
56 |
70
| IBinding sourceBinding = getBinding("source");
|
57 |
70
| if (sourceBinding != null)
|
58 |
70
| source = sourceBinding.getObject();
|
59 |
| |
60 |
70
| if (source == null)
|
61 |
0
| return null;
|
62 |
| |
63 |
70
| return (Iterator) getValueConverter().coerceValue(source, Iterator.class);
|
64 |
| } |
65 |
| |
66 |
70
| protected void prepareForRender(IRequestCycle cycle)
|
67 |
| { |
68 |
70
| _value = null;
|
69 |
70
| _index = 0;
|
70 |
| } |
71 |
| |
72 |
70
| protected void cleanupAfterRender(IRequestCycle cycle)
|
73 |
| { |
74 |
70
| _value = null;
|
75 |
| } |
76 |
| |
77 |
| |
78 |
| |
79 |
| |
80 |
| |
81 |
| |
82 |
70
| protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle)
|
83 |
| { |
84 |
70
| Iterator dataSource = getSourceData();
|
85 |
| |
86 |
| |
87 |
| |
88 |
70
| if (dataSource == null)
|
89 |
0
| return;
|
90 |
| |
91 |
70
| boolean indexBound = isParameterBound("index");
|
92 |
70
| boolean valueBound = isParameterBound("value");
|
93 |
| |
94 |
70
| String element = getElement();
|
95 |
| |
96 |
70
| boolean hasNext = dataSource.hasNext();
|
97 |
| |
98 |
70
| while (hasNext)
|
99 |
| { |
100 |
1059
| _value = dataSource.next();
|
101 |
1059
| hasNext = dataSource.hasNext();
|
102 |
| |
103 |
1059
| if (indexBound)
|
104 |
24
| setIndexParameter(_index);
|
105 |
| |
106 |
1059
| if (valueBound)
|
107 |
1045
| setValueParameter(_value);
|
108 |
| |
109 |
1059
| if (element != null)
|
110 |
| { |
111 |
1032
| writer.begin(element);
|
112 |
1032
| renderInformalParameters(writer, cycle);
|
113 |
| } |
114 |
| |
115 |
1059
| renderBody(writer, cycle);
|
116 |
| |
117 |
1059
| if (element != null)
|
118 |
1032
| writer.end();
|
119 |
| |
120 |
1059
| _index++;
|
121 |
| } |
122 |
| |
123 |
| } |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
| |
131 |
| |
132 |
19
| public Object getValue()
|
133 |
| { |
134 |
19
| if (!isRendering())
|
135 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "value");
|
136 |
| |
137 |
19
| return _value;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
| |
148 |
6
| public int getIndex()
|
149 |
| { |
150 |
6
| if (!isRendering())
|
151 |
0
| throw Tapestry.createRenderOnlyPropertyException(this, "index");
|
152 |
| |
153 |
6
| 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 |
| } |