1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package javax.faces.component; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
|
23 | |
import javax.el.ValueExpression; |
24 | |
import javax.faces.FactoryFinder; |
25 | |
import javax.faces.application.FacesMessage; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.convert.Converter; |
28 | |
import javax.faces.convert.ConverterException; |
29 | |
import javax.faces.render.RenderKit; |
30 | |
import javax.faces.render.RenderKitFactory; |
31 | |
import javax.faces.render.Renderer; |
32 | |
|
33 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent; |
34 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFJspProperty; |
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | @JSFComponent(name = "f:viewParam", bodyContent = "JSP", |
50 | |
tagClass = "org.apache.myfaces.taglib.core.ViewParamTag") |
51 | |
@JSFJspProperty(name = "maxlength", returnType = "int", longDesc = "The max number or characters allowed for this param") |
52 | |
public class UIViewParameter extends UIInput |
53 | |
{ |
54 | |
public static final String COMPONENT_FAMILY = "javax.faces.ViewParameter"; |
55 | |
public static final String COMPONENT_TYPE = "javax.faces.ViewParameter"; |
56 | |
|
57 | |
private static final String DELEGATE_FAMILY = UIInput.COMPONENT_FAMILY; |
58 | |
private static final String DELEGATE_RENDERER_TYPE = "javax.faces.Text"; |
59 | |
|
60 | |
private static Renderer _delegateRenderer; |
61 | |
|
62 | |
public UIViewParameter() |
63 | 0 | { |
64 | 0 | setRendererType(null); |
65 | 0 | } |
66 | |
|
67 | |
@Override |
68 | |
public String getFamily() |
69 | |
{ |
70 | 0 | return COMPONENT_FAMILY; |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public void decode(FacesContext context) |
75 | |
{ |
76 | |
|
77 | |
|
78 | 0 | String value = context.getExternalContext().getRequestParameterMap().get(getName()); |
79 | |
|
80 | |
|
81 | |
|
82 | 0 | if (value != null) |
83 | |
{ |
84 | 0 | setSubmittedValue(value); |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
public void encodeAll(FacesContext context) throws IOException |
90 | |
{ |
91 | 0 | if (context == null) |
92 | |
{ |
93 | 0 | throw new NullPointerException(); |
94 | |
} |
95 | 0 | setSubmittedValue(getStringValue(context)); |
96 | 0 | } |
97 | |
|
98 | |
public String getName() |
99 | |
{ |
100 | 0 | return (String) getStateHelper().get(PropertyKeys.name); |
101 | |
} |
102 | |
|
103 | |
public String getStringValue(FacesContext context) |
104 | |
{ |
105 | 0 | if (getValueExpression ("value") != null) |
106 | |
{ |
107 | |
|
108 | |
|
109 | 0 | return getStringValueFromModel (context); |
110 | |
} |
111 | |
|
112 | |
|
113 | |
|
114 | 0 | return ((String) this.getLocalValue()); |
115 | |
} |
116 | |
|
117 | |
public String getStringValueFromModel(FacesContext context) throws ConverterException |
118 | |
{ |
119 | 0 | ValueExpression ve = getValueExpression ("value"); |
120 | |
Converter converter; |
121 | |
Object value; |
122 | |
|
123 | 0 | if (ve == null) |
124 | |
{ |
125 | |
|
126 | 0 | return null; |
127 | |
} |
128 | |
|
129 | 0 | value = ve.getValue (context.getELContext()); |
130 | |
|
131 | 0 | if (value instanceof String) |
132 | |
{ |
133 | |
|
134 | 0 | return ((String) value); |
135 | |
} |
136 | |
|
137 | 0 | converter = getConverter(); |
138 | |
|
139 | 0 | if (converter == null) |
140 | |
{ |
141 | 0 | if (value == null) |
142 | |
{ |
143 | |
|
144 | 0 | return null; |
145 | |
} |
146 | |
|
147 | |
|
148 | |
|
149 | 0 | converter = context.getApplication().createConverter (value.getClass()); |
150 | |
|
151 | 0 | if (converter == null) |
152 | |
{ |
153 | |
|
154 | |
|
155 | 0 | return value.toString(); |
156 | |
} |
157 | |
} |
158 | |
|
159 | 0 | return converter.getAsString (context, this, value); |
160 | |
} |
161 | |
|
162 | |
@Override |
163 | |
public String getSubmittedValue() |
164 | |
{ |
165 | 0 | return (String)super.getSubmittedValue(); |
166 | |
} |
167 | |
|
168 | |
@JSFProperty(tagExcluded=true) |
169 | |
@Override |
170 | |
public boolean isImmediate() |
171 | |
{ |
172 | 0 | return false; |
173 | |
} |
174 | |
|
175 | |
@JSFProperty(tagExcluded=true) |
176 | |
@Override |
177 | |
public boolean isRendered() |
178 | |
{ |
179 | 0 | return super.isRendered(); |
180 | |
} |
181 | |
|
182 | |
@Override |
183 | |
public void processValidators(FacesContext context) |
184 | |
{ |
185 | 0 | if (context == null) |
186 | |
{ |
187 | 0 | throw new NullPointerException ("context"); |
188 | |
} |
189 | |
|
190 | |
|
191 | |
|
192 | 0 | if ((getSubmittedValue() == null) && isRequired()) |
193 | |
{ |
194 | |
FacesMessage message; |
195 | 0 | String required = getRequiredMessage(); |
196 | |
|
197 | 0 | if (required != null) |
198 | |
{ |
199 | 0 | message = new FacesMessage (FacesMessage.SEVERITY_ERROR, required, required); |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 0 | Object label = _MessageUtils.getLabel (context, this); |
204 | |
|
205 | 0 | message = _MessageUtils.getMessage (context, context.getViewRoot().getLocale(), |
206 | |
FacesMessage.SEVERITY_ERROR, REQUIRED_MESSAGE_ID, new Object[] { label }); |
207 | |
} |
208 | |
|
209 | 0 | setValid (false); |
210 | |
|
211 | 0 | context.addMessage (getClientId (context), message); |
212 | 0 | context.validationFailed(); |
213 | 0 | context.renderResponse(); |
214 | |
|
215 | 0 | return; |
216 | |
} |
217 | |
|
218 | 0 | super.processValidators (context); |
219 | 0 | } |
220 | |
|
221 | 0 | enum PropertyKeys |
222 | |
{ |
223 | 0 | name |
224 | |
} |
225 | |
|
226 | |
public void setName(String name) |
227 | |
{ |
228 | 0 | getStateHelper().put(PropertyKeys.name, name ); |
229 | 0 | } |
230 | |
|
231 | |
@Override |
232 | |
public void updateModel(FacesContext context) |
233 | |
{ |
234 | 0 | super.updateModel(context); |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | 0 | if ((getValueExpression ("value") == null) && isValid() && isLocalValueSet()) |
240 | |
{ |
241 | 0 | context.getExternalContext().getRequestMap().put (getName(), getLocalValue()); |
242 | |
} |
243 | 0 | } |
244 | |
|
245 | |
@Override |
246 | |
protected Object getConvertedValue(FacesContext context, Object submittedValue) |
247 | |
{ |
248 | 0 | return getDelegateRenderer(context).getConvertedValue(context, this, submittedValue); |
249 | |
} |
250 | |
|
251 | |
private static Renderer getDelegateRenderer(FacesContext context) |
252 | |
{ |
253 | 0 | if (_delegateRenderer == null) |
254 | |
{ |
255 | 0 | RenderKitFactory factory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); |
256 | 0 | RenderKit kit = factory.getRenderKit(context, RenderKitFactory.HTML_BASIC_RENDER_KIT); |
257 | |
|
258 | 0 | _delegateRenderer = kit.getRenderer(DELEGATE_FAMILY, DELEGATE_RENDERER_TYPE); |
259 | |
} |
260 | |
|
261 | 0 | return _delegateRenderer; |
262 | |
} |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
public static class Reference |
271 | |
{ |
272 | |
private int _index; |
273 | |
private UIViewParameter _param; |
274 | |
private Object _state; |
275 | |
private String _viewId; |
276 | |
|
277 | |
public Reference(FacesContext context, UIViewParameter param, int indexInParent, |
278 | |
String viewIdAtTimeOfConstruction) |
279 | 0 | { |
280 | |
|
281 | |
|
282 | 0 | _param = param; |
283 | 0 | _viewId = viewIdAtTimeOfConstruction; |
284 | 0 | _index = indexInParent; |
285 | 0 | _state = param.saveState(context); |
286 | 0 | } |
287 | |
|
288 | |
public UIViewParameter getUIViewParameter(FacesContext context) |
289 | |
{ |
290 | |
|
291 | 0 | if (context.getViewRoot().getViewId().equals(_viewId)) |
292 | |
{ |
293 | |
|
294 | |
|
295 | 0 | return (UIViewParameter) _param.getParent().getChildren().get(_index); |
296 | |
} |
297 | |
else |
298 | |
{ |
299 | |
|
300 | |
|
301 | 0 | _param.restoreState(context, _state); |
302 | |
|
303 | 0 | return _param; |
304 | |
} |
305 | |
} |
306 | |
} |
307 | |
} |