1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
package org.apache.tapestry.form; |
16 | |
|
17 | |
import java.util.Iterator; |
18 | |
|
19 | |
import org.apache.hivemind.ApplicationRuntimeException; |
20 | |
import org.apache.tapestry.IActionListener; |
21 | |
import org.apache.tapestry.IForm; |
22 | |
import org.apache.tapestry.IMarkupWriter; |
23 | |
import org.apache.tapestry.IRequestCycle; |
24 | |
import org.apache.tapestry.Tapestry; |
25 | |
import org.apache.tapestry.coerce.ValueConverter; |
26 | |
import org.apache.tapestry.listener.ListenerInvoker; |
27 | |
import org.apache.tapestry.services.DataSqueezer; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public abstract class ListEdit extends AbstractFormComponent |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
protected void renderFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
45 | |
{ |
46 | 0 | this.render(writer, cycle, getSource()); |
47 | 0 | } |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
protected void rewindFormComponent(IMarkupWriter writer, IRequestCycle cycle) |
54 | |
{ |
55 | 0 | String[] values = cycle.getParameters(getName()); |
56 | |
|
57 | 0 | this.render(writer, cycle, (Iterator) getValueConverter().coerceValue( |
58 | |
values, |
59 | 0 | Iterator.class)); |
60 | 0 | } |
61 | |
|
62 | |
protected void render(IMarkupWriter writer, IRequestCycle cycle, Iterator i) |
63 | |
{ |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | 0 | if (i == null) |
69 | 0 | return; |
70 | |
|
71 | 0 | int index = 0; |
72 | |
|
73 | 0 | String element = getElement(); |
74 | |
|
75 | 0 | boolean indexBound = isParameterBound("index"); |
76 | |
|
77 | 0 | while (i.hasNext()) |
78 | |
{ |
79 | 0 | Object value = null; |
80 | |
|
81 | 0 | if (indexBound) |
82 | 0 | setIndex(index++); |
83 | |
|
84 | 0 | if (cycle.isRewinding()) |
85 | 0 | value = convertValue((String) i.next()); |
86 | |
else |
87 | |
{ |
88 | 0 | value = i.next(); |
89 | 0 | writeValue(getForm(), getName(), value); |
90 | |
} |
91 | |
|
92 | 0 | setValue(value); |
93 | |
|
94 | 0 | getListenerInvoker().invokeListener(getListener(), this, cycle); |
95 | |
|
96 | 0 | if (element != null) |
97 | |
{ |
98 | 0 | writer.begin(element); |
99 | 0 | renderInformalParameters(writer, cycle); |
100 | |
} |
101 | |
|
102 | 0 | renderBody(writer, cycle); |
103 | |
|
104 | 0 | if (element != null) |
105 | 0 | writer.end(); |
106 | 0 | } |
107 | 0 | } |
108 | |
|
109 | |
private void writeValue(IForm form, String name, Object value) |
110 | |
{ |
111 | |
String externalValue; |
112 | |
|
113 | |
try |
114 | |
{ |
115 | 0 | externalValue = getDataSqueezer().squeeze(value); |
116 | |
} |
117 | 0 | catch (Exception ex) |
118 | |
{ |
119 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
120 | |
"ListEdit.unable-to-convert-value", |
121 | |
value), this, null, ex); |
122 | 0 | } |
123 | |
|
124 | 0 | form.addHiddenValue(name, externalValue); |
125 | 0 | } |
126 | |
|
127 | |
private Object convertValue(String value) |
128 | |
{ |
129 | |
try |
130 | |
{ |
131 | 0 | return getDataSqueezer().unsqueeze(value); |
132 | |
} |
133 | 0 | catch (Exception ex) |
134 | |
{ |
135 | 0 | throw new ApplicationRuntimeException(Tapestry.format( |
136 | |
"ListEdit.unable-to-convert-string", |
137 | |
value), this, null, ex); |
138 | |
} |
139 | |
} |
140 | |
|
141 | |
public abstract String getElement(); |
142 | |
|
143 | |
|
144 | |
|
145 | |
public abstract IActionListener getListener(); |
146 | |
|
147 | |
|
148 | |
|
149 | |
public boolean isDisabled() |
150 | |
{ |
151 | 0 | return false; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
|
156 | |
public abstract Iterator getSource(); |
157 | |
|
158 | |
|
159 | |
|
160 | |
public abstract void setValue(Object value); |
161 | |
|
162 | |
|
163 | |
|
164 | |
public abstract void setIndex(int index); |
165 | |
|
166 | |
|
167 | |
|
168 | |
public abstract DataSqueezer getDataSqueezer(); |
169 | |
|
170 | |
|
171 | |
|
172 | |
public abstract ValueConverter getValueConverter(); |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public abstract ListenerInvoker getListenerInvoker(); |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
protected boolean getCanTakeFocus() |
188 | |
{ |
189 | 0 | return false; |
190 | |
} |
191 | |
|
192 | |
public String getDisplayName() |
193 | |
{ |
194 | |
|
195 | 0 | return null; |
196 | |
} |
197 | |
|
198 | |
} |