|
|||||||||||||||||||
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 | 87.5% | 87.5% | 75% | 86.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.listener.ListenerInvoker;
|
|
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 |
String[] submittedValues = cycle.getParameters(name); |
67 |
|
|
68 | 2 |
i = (Iterator) getValueConverter().coerceValue(submittedValues, Iterator.class);
|
69 |
} |
|
70 |
|
|
71 |
// If the source (when rendering), or the submitted values (on submit)
|
|
72 |
// are null, then skip the remainder (nothing to update, nothing to
|
|
73 |
// render).
|
|
74 |
|
|
75 | 5 |
if (i == null) |
76 | 0 |
return;
|
77 |
|
|
78 | 5 |
int index = 0;
|
79 |
|
|
80 | 5 |
String element = getElement(); |
81 |
|
|
82 | 5 |
while (i.hasNext())
|
83 |
{ |
|
84 | 10 |
Object value = null;
|
85 |
|
|
86 | 10 |
if (indexBound)
|
87 | 3 |
setIndex(index++); |
88 |
|
|
89 | 10 |
if (cycleRewinding)
|
90 | 4 |
value = convertValue((String) i.next()); |
91 |
else
|
|
92 |
{ |
|
93 | 6 |
value = i.next(); |
94 | 6 |
writeValue(form, name, value); |
95 |
} |
|
96 |
|
|
97 | 9 |
setValue(value); |
98 |
|
|
99 | 9 |
getListenerInvoker().invokeListener(getListener(), this, cycle);
|
100 |
|
|
101 | 9 |
if (element != null) |
102 |
{ |
|
103 | 6 |
writer.begin(element); |
104 | 6 |
renderInformalParameters(writer, cycle); |
105 |
} |
|
106 |
|
|
107 | 9 |
renderBody(writer, cycle); |
108 |
|
|
109 | 9 |
if (element != null) |
110 | 6 |
writer.end(); |
111 |
|
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 | 6 |
private void writeValue(IForm form, String name, Object value) |
116 |
{ |
|
117 | 6 |
String externalValue; |
118 |
|
|
119 | 6 |
try
|
120 |
{ |
|
121 | 6 |
externalValue = getDataSqueezer().squeeze(value); |
122 |
} |
|
123 |
catch (IOException ex)
|
|
124 |
{ |
|
125 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
126 |
"ListEdit.unable-to-convert-value",
|
|
127 |
value), this, null, ex); |
|
128 |
} |
|
129 |
|
|
130 | 6 |
form.addHiddenValue(name, externalValue); |
131 |
} |
|
132 |
|
|
133 | 4 |
private Object convertValue(String value)
|
134 |
{ |
|
135 | 4 |
try
|
136 |
{ |
|
137 | 4 |
return getDataSqueezer().unsqueeze(value);
|
138 |
} |
|
139 |
catch (IOException ex)
|
|
140 |
{ |
|
141 | 0 |
throw new ApplicationRuntimeException(Tapestry.format( |
142 |
"ListEdit.unable-to-convert-string",
|
|
143 |
value), this, null, ex); |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
public abstract String getElement();
|
|
148 |
|
|
149 |
/** @since 2.2 * */
|
|
150 |
|
|
151 |
public abstract IActionListener getListener();
|
|
152 |
|
|
153 |
/** @since 3.0 * */
|
|
154 |
|
|
155 | 0 |
public boolean isDisabled() |
156 |
{ |
|
157 | 0 |
return false; |
158 |
} |
|
159 |
|
|
160 |
/** @since 4.0 */
|
|
161 |
|
|
162 |
public abstract Iterator getSource();
|
|
163 |
|
|
164 |
/** @since 4.0 */
|
|
165 |
|
|
166 |
public abstract void setValue(Object value); |
|
167 |
|
|
168 |
/** @since 4.0 */
|
|
169 |
|
|
170 |
public abstract void setIndex(int index); |
|
171 |
|
|
172 |
/** @since 4.0 */
|
|
173 |
|
|
174 |
public abstract DataSqueezer getDataSqueezer();
|
|
175 |
|
|
176 |
/** @since 4.0 */
|
|
177 |
|
|
178 |
public abstract ValueConverter getValueConverter();
|
|
179 |
|
|
180 |
/**
|
|
181 |
* Injected.
|
|
182 |
*
|
|
183 |
* @since 4.0
|
|
184 |
*/
|
|
185 |
|
|
186 |
public abstract ListenerInvoker getListenerInvoker();
|
|
187 |
} |
|