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.composite; |
20 | |
|
21 | |
import java.beans.BeanDescriptor; |
22 | |
import java.beans.IntrospectionException; |
23 | |
import java.beans.PropertyDescriptor; |
24 | |
import java.io.IOException; |
25 | |
import java.util.ArrayList; |
26 | |
import java.util.HashMap; |
27 | |
import java.util.List; |
28 | |
import java.util.Map; |
29 | |
import java.util.logging.Level; |
30 | |
import java.util.logging.Logger; |
31 | |
|
32 | |
import javax.faces.component.UIComponent; |
33 | |
import javax.faces.view.facelets.FaceletContext; |
34 | |
import javax.faces.view.facelets.TagAttribute; |
35 | |
import javax.faces.view.facelets.TagConfig; |
36 | |
import javax.faces.view.facelets.TagException; |
37 | |
import javax.faces.view.facelets.TagHandler; |
38 | |
|
39 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
40 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
41 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
42 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
@JSFFaceletTag(name="composite:insertFacet") |
51 | |
public class InsertFacetHandler extends TagHandler |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public static final String INSERT_FACET_USED = "org.apache.myfaces.INSERT_FACET_USED"; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public static final String INSERT_FACET_KEYS = "org.apache.myfaces.INSERT_FACET_KEYS"; |
64 | |
|
65 | 0 | private static final Logger log = Logger.getLogger(InsertFacetHandler.class.getName()); |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
@JSFFaceletAttribute(name="name", |
71 | |
className="javax.el.ValueExpression", |
72 | |
deferredValueType="java.lang.String", |
73 | |
required=true) |
74 | |
protected final TagAttribute _name; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
@JSFFaceletAttribute(name="required", |
81 | |
className="javax.el.ValueExpression", |
82 | |
deferredValueType="boolean") |
83 | |
protected final TagAttribute _required; |
84 | |
|
85 | |
public InsertFacetHandler(TagConfig config) |
86 | |
{ |
87 | 0 | super(config); |
88 | 0 | _name = getRequiredAttribute("name"); |
89 | 0 | _required = getAttribute("required"); |
90 | 0 | } |
91 | |
|
92 | |
public String getFacetName(FaceletContext ctx) |
93 | |
{ |
94 | 0 | return _name.getValue(ctx); |
95 | |
} |
96 | |
|
97 | |
@SuppressWarnings("unchecked") |
98 | |
public void apply(FaceletContext ctx, UIComponent parent) |
99 | |
throws IOException |
100 | |
{ |
101 | 0 | if (((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata()) |
102 | |
{ |
103 | 0 | String facetName = _name.getValue(ctx); |
104 | |
|
105 | 0 | UIComponent compositeBaseParent |
106 | |
= FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack(); |
107 | |
|
108 | 0 | CompositeComponentBeanInfo beanInfo = |
109 | |
(CompositeComponentBeanInfo) compositeBaseParent.getAttributes() |
110 | |
.get(UIComponent.BEANINFO_KEY); |
111 | |
|
112 | 0 | if (beanInfo == null) |
113 | |
{ |
114 | 0 | if (log.isLoggable(Level.SEVERE)) |
115 | |
{ |
116 | 0 | log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY "); |
117 | |
} |
118 | 0 | return; |
119 | |
} |
120 | |
|
121 | 0 | BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); |
122 | |
|
123 | 0 | List<String> facetList = (List<String>) beanDescriptor.getValue(INSERT_FACET_USED); |
124 | |
|
125 | 0 | if (facetList == null) |
126 | |
{ |
127 | |
|
128 | 0 | facetList = new ArrayList<String>(); |
129 | 0 | beanDescriptor.setValue( |
130 | |
INSERT_FACET_USED, |
131 | |
facetList); |
132 | |
} |
133 | |
|
134 | 0 | facetList.add(facetName); |
135 | |
|
136 | 0 | Map<String, PropertyDescriptor> insertFacetPropertyDescriptorMap = (Map<String, PropertyDescriptor>) |
137 | |
beanDescriptor.getValue(INSERT_FACET_KEYS); |
138 | |
|
139 | 0 | if (insertFacetPropertyDescriptorMap == null) |
140 | |
{ |
141 | 0 | insertFacetPropertyDescriptorMap = new HashMap<String, PropertyDescriptor>(); |
142 | 0 | beanDescriptor.setValue(INSERT_FACET_KEYS, insertFacetPropertyDescriptorMap); |
143 | |
} |
144 | |
|
145 | 0 | PropertyDescriptor facetDescriptor = _createFacetPropertyDescriptor(facetName, ctx); |
146 | 0 | insertFacetPropertyDescriptorMap.put(facetName, facetDescriptor); |
147 | 0 | } |
148 | |
else |
149 | |
{ |
150 | 0 | String facetName = _name.getValue(ctx); |
151 | |
|
152 | 0 | AbstractFaceletContext actx = (AbstractFaceletContext) ctx; |
153 | |
|
154 | 0 | actx.includeCompositeComponentDefinition(parent, facetName); |
155 | |
} |
156 | |
|
157 | 0 | } |
158 | |
|
159 | |
private PropertyDescriptor _createFacetPropertyDescriptor(String facetName, FaceletContext ctx) |
160 | |
throws TagException, IOException |
161 | |
{ |
162 | |
try |
163 | |
{ |
164 | 0 | CompositeComponentPropertyDescriptor facetPropertyDescriptor = |
165 | |
new CompositeComponentPropertyDescriptor(facetName); |
166 | |
|
167 | 0 | if (_required != null) |
168 | |
{ |
169 | 0 | facetPropertyDescriptor.setValue("required", _required.getValueExpression(ctx, Boolean.class)); |
170 | |
} |
171 | |
|
172 | 0 | return facetPropertyDescriptor; |
173 | |
} |
174 | 0 | catch (IntrospectionException e) |
175 | |
{ |
176 | 0 | if (log.isLoggable(Level.SEVERE)) |
177 | |
{ |
178 | 0 | log.log(Level.SEVERE, "Cannot create PropertyDescriptor for attribute ",e); |
179 | |
} |
180 | 0 | throw new TagException(tag,e); |
181 | |
} |
182 | |
} |
183 | |
|
184 | |
} |