1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.view.facelets.tag.jsf; |
20 | |
|
21 | |
import java.io.IOException; |
22 | |
|
23 | |
import javax.el.ValueExpression; |
24 | |
import javax.faces.component.UIComponent; |
25 | |
import javax.faces.component.ValueHolder; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.faces.convert.Converter; |
28 | |
import javax.faces.view.ValueHolderAttachedObjectHandler; |
29 | |
import javax.faces.view.facelets.ComponentHandler; |
30 | |
import javax.faces.view.facelets.ConverterHandler; |
31 | |
import javax.faces.view.facelets.FaceletContext; |
32 | |
import javax.faces.view.facelets.MetaRuleset; |
33 | |
import javax.faces.view.facelets.TagAttribute; |
34 | |
import javax.faces.view.facelets.TagException; |
35 | |
import javax.faces.view.facelets.TagHandlerDelegate; |
36 | |
|
37 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
38 | |
import org.apache.myfaces.view.facelets.tag.MetaRulesetImpl; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public class ConverterTagHandlerDelegate extends TagHandlerDelegate implements ValueHolderAttachedObjectHandler |
52 | |
{ |
53 | |
private ConverterHandler _delegate; |
54 | |
|
55 | |
public ConverterTagHandlerDelegate(ConverterHandler delegate) |
56 | 0 | { |
57 | 0 | _delegate = delegate; |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
@Override |
77 | |
public void apply(FaceletContext ctx, UIComponent parent) throws IOException |
78 | |
{ |
79 | |
|
80 | 0 | if (!ComponentHandler.isNew(parent)) |
81 | |
{ |
82 | 0 | return; |
83 | |
} |
84 | 0 | if (parent instanceof ValueHolder) |
85 | |
{ |
86 | 0 | applyAttachedObject(ctx.getFacesContext(), parent); |
87 | |
} |
88 | 0 | else if (UIComponent.isCompositeComponent(parent)) |
89 | |
{ |
90 | 0 | if (getFor() == null) |
91 | |
{ |
92 | 0 | throw new TagException(_delegate.getTag(), "is nested inside a composite component" |
93 | |
+ " but does not have a for attribute."); |
94 | |
} |
95 | 0 | FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx); |
96 | 0 | mctx.addAttachedObjectHandler(parent, _delegate); |
97 | 0 | } |
98 | |
else |
99 | |
{ |
100 | 0 | throw new TagException(_delegate.getTag(), |
101 | |
"Parent not composite component or an instance of ValueHolder: " + parent); |
102 | |
} |
103 | 0 | } |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
protected Converter createConverter(FaceletContext ctx) |
113 | |
{ |
114 | 0 | if (_delegate.getConverterId(ctx) == null) |
115 | |
{ |
116 | 0 | throw new TagException(_delegate.getTag(), |
117 | |
"Default behavior invoked of requiring a converter-id passed in the " |
118 | |
+ "constructor, must override ConvertHandler(ConverterConfig)"); |
119 | |
} |
120 | 0 | return ctx.getFacesContext().getApplication().createConverter(_delegate.getConverterId(ctx)); |
121 | |
} |
122 | |
|
123 | |
@Override |
124 | |
public MetaRuleset createMetaRuleset(Class type) |
125 | |
{ |
126 | 0 | return new MetaRulesetImpl(_delegate.getTag(), type).ignore("binding").ignore("for"); |
127 | |
} |
128 | |
|
129 | |
public void applyAttachedObject(FacesContext context, UIComponent parent) |
130 | |
{ |
131 | |
|
132 | 0 | FaceletContext faceletContext = (FaceletContext) context.getAttributes().get( |
133 | |
FaceletContext.FACELET_CONTEXT_KEY); |
134 | |
|
135 | |
|
136 | 0 | ValueHolder vh = (ValueHolder) parent; |
137 | 0 | ValueExpression ve = null; |
138 | 0 | Converter c = null; |
139 | 0 | if (_delegate.getBinding() != null) |
140 | |
{ |
141 | 0 | ve = _delegate.getBinding().getValueExpression(faceletContext, Converter.class); |
142 | 0 | c = (Converter) ve.getValue(faceletContext); |
143 | |
} |
144 | 0 | if (c == null) |
145 | |
{ |
146 | 0 | c = this.createConverter(faceletContext); |
147 | 0 | if (ve != null) |
148 | |
{ |
149 | 0 | ve.setValue(faceletContext, c); |
150 | |
} |
151 | |
} |
152 | 0 | if (c == null) |
153 | |
{ |
154 | 0 | throw new TagException(_delegate.getTag(), "No Converter was created"); |
155 | |
} |
156 | 0 | _delegate.setAttributes(faceletContext, c); |
157 | 0 | vh.setConverter(c); |
158 | 0 | Object lv = vh.getLocalValue(); |
159 | 0 | FacesContext faces = faceletContext.getFacesContext(); |
160 | 0 | if (lv instanceof String) |
161 | |
{ |
162 | 0 | vh.setValue(c.getAsObject(faces, parent, (String) lv)); |
163 | |
} |
164 | 0 | } |
165 | |
|
166 | |
public String getFor() |
167 | |
{ |
168 | 0 | TagAttribute forAttribute = _delegate.getTagAttribute("for"); |
169 | |
|
170 | 0 | if (forAttribute == null) |
171 | |
{ |
172 | 0 | return null; |
173 | |
} |
174 | |
else |
175 | |
{ |
176 | 0 | return forAttribute.getValue(); |
177 | |
} |
178 | |
} |
179 | |
} |