|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ListEdit.java | 88.9% | 88.4% | 75% | 87.7% |
|
1 |
// Copyright 2004, 2005 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.request.RequestContext;
|
|
28 |
import org.apache.tapestry.services.DataSqueezer;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* A specialized component used to edit a list of items within a form; it is similar to a
|
|
32 |
* {@link org.apache.tapestry.components.Foreach}but leverages hidden inputs within the
|
|
33 |
* <form> to store the items in the list. [ <a
|
|
34 |
* href="../../../../../ComponentReference/ListEdit.html">Component Reference </a>]
|
|
35 |
*
|
|
36 |
* @author Howard Lewis Ship
|
|
37 |
* @since 1.0.2
|
|
38 |
*/
|
|
39 |
|
|
40 |
public abstract class ListEdit extends AbstractFormComponent |
|
41 |
{ |
|
42 | 5 |
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) |
43 |
{ |
|
44 | 5 |
Iterator i = null;
|
45 |
|
|
46 | 5 |
IForm form = getForm(cycle); |
47 |
|
|
48 | 5 |
boolean cycleRewinding = cycle.isRewinding();
|
49 |
|
|
50 |
// If the cycle is rewinding, but not this particular form,
|
|
51 |
// then do nothing (don't even render the body).
|
|
52 |
|
|
53 | 5 |
if (cycleRewinding && !form.isRewinding())
|
54 | 0 |
return;
|
55 |
|
|
56 | 5 |
String name = form.getElementId(this);
|
57 |
|
|
58 | 5 |
boolean indexBound = isParameterBound("index"); |
59 |
|
|
60 | 5 |
if (!cycleRewinding)
|
61 |
{ |
|
62 | 3 |
i = getSource(); |
63 |
} |
|
64 |
else
|
|
65 |
{ |
|
66 | 2 |
RequestContext context = cycle.getRequestContext(); |
67 | 2 |
String[] submittedValues = context.getParameters(name); |
68 |
|
|
69 | 2 |
i = (Iterator) getValueConverter().coerceValue(submittedValues, Iterator.class);
|
70 |
} |
|
71 |
|
|
72 |
// If the source (when rendering), or the submitted values (on submit)
|
|
73 |
// are null, then skip the remainder (nothing to update, nothing to
|
|
74 |
// render).
|
|
75 |
|
|
76 | 5 |
if (i == null) |
77 | 0 |
return;
|
78 |
|
|
79 | 5 |
int index = 0;
|
80 |
|
|
81 | 5 |
IActionListener listener = getListener(); |
82 | 5 |
String element = getElement(); |
83 |
|
|
84 | 5 |
while (i.hasNext())
|
85 |
{ |
|
86 | 10 |
Object value = null;
|
87 |
|
|
88 | 10 |
if (indexBound)
|
89 | 3 |
setIndex(index++); |
90 |
|
|
91 | 10 |
if (cycleRewinding)
|
92 | 4 |
value = convertValue((String) i.next()); |
93 |
else
|
|
94 |
{ |
|
95 | 6 |
value = i.next(); |
96 | 6 |
writeValue(form, name, value); |
97 |
} |
|
98 |
|
|
99 | 9 |
setValue(value); |
100 |
|
|
101 | 9 |
if (listener != null) |
102 | 3 |
listener.actionTriggered(this, cycle);
|
103 |
|
|
104 | 9 |
if (element != null) |
105 |
{ |
|
106 | 6 |
writer.begin(element); |
107 | 6 |
renderInformalParameters(writer, cycle); |
108 |
} |
|
109 |
|
|
110 | 9 |
renderBody(writer, cycle); |
111 |
|
|
112 | 9 |
if (element != null) |
113 | 6 |
writer.end(); |
114 |
|
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 | 6 |
private void writeValue(IForm form, String name, Object value) |
119 |
{ |
|
120 | 6 |
String externalValue; |
121 |
|
|
122 | 6 |
try
|
123 |
{ |
|
124 | 6 |
externalValue = getDataSqueezer().squeeze(value); |
125 |
} |
|
126 |
catch (IOException ex)
|
|
127 |
{ |
|
128 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
129 |
"ListEdit.unable-to-convert-value",
|
|
130 |
value), this, null, ex); |
|
131 |
} |
|
132 |
|
|
133 | 6 |
form.addHiddenValue(name, externalValue); |
134 |
} |
|
135 |
|
|
136 | 4 |
private Object convertValue(String value)
|
137 |
{ |
|
138 | 4 |
try
|
139 |
{ |
|
140 | 4 |
return getDataSqueezer().unsqueeze(value);
|
141 |
} |
|
142 |
catch (IOException ex)
|
|
143 |
{ |
|
144 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
145 |
"ListEdit.unable-to-convert-string",
|
|
146 |
value), this, null, ex); |
|
147 |
} |
|
148 |
} |
|
149 |
|
|
150 |
public abstract String getElement();
|
|
151 |
|
|
152 |
/** @since 2.2 * */
|
|
153 |
|
|
154 |
public abstract IActionListener getListener();
|
|
155 |
|
|
156 |
/** @since 3.0 * */
|
|
157 |
|
|
158 | 0 |
public boolean isDisabled() |
159 |
{ |
|
160 | 0 |
return false; |
161 |
} |
|
162 |
|
|
163 |
/** @since 3.1 */
|
|
164 |
|
|
165 |
public abstract Iterator getSource();
|
|
166 |
|
|
167 |
/** @since 3.1 */
|
|
168 |
|
|
169 |
public abstract void setValue(Object value); |
|
170 |
|
|
171 |
/** @since 3.1 */
|
|
172 |
|
|
173 |
public abstract void setIndex(int index); |
|
174 |
|
|
175 |
/** @since 3.1 */
|
|
176 |
|
|
177 |
public abstract DataSqueezer getDataSqueezer();
|
|
178 |
|
|
179 |
/** @since 3.1 */
|
|
180 |
|
|
181 |
public abstract ValueConverter getValueConverter();
|
|
182 |
} |
|