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