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.io.IOException; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import java.util.logging.Level; |
26 | |
import java.util.logging.Logger; |
27 | |
|
28 | |
import javax.faces.component.UIComponent; |
29 | |
import javax.faces.view.facelets.ComponentConfig; |
30 | |
import javax.faces.view.facelets.ComponentHandler; |
31 | |
import javax.faces.view.facelets.FaceletContext; |
32 | |
import javax.faces.view.facelets.TagAttribute; |
33 | |
import javax.faces.view.facelets.TagException; |
34 | |
|
35 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletAttribute; |
36 | |
import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFFaceletTag; |
37 | |
import org.apache.myfaces.view.facelets.AbstractFaceletContext; |
38 | |
import org.apache.myfaces.view.facelets.FaceletCompositionContext; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
@JSFFaceletTag(name="composite:renderFacet") |
47 | |
public class RenderFacetHandler extends ComponentHandler |
48 | |
{ |
49 | 0 | private static final Logger log = Logger.getLogger(RenderFacetHandler.class.getName()); |
50 | |
|
51 | |
public static final String RENDER_FACET_USED = "org.apache.myfaces.RENDER_FACET_USED"; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
@JSFFaceletAttribute(name="name", |
57 | |
className="javax.el.ValueExpression", |
58 | |
deferredValueType="java.lang.String", |
59 | |
required=true) |
60 | |
protected final TagAttribute _name; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
@JSFFaceletAttribute(name="required", |
67 | |
className="javax.el.ValueExpression", |
68 | |
deferredValueType="boolean") |
69 | |
protected final TagAttribute _required; |
70 | |
|
71 | |
public RenderFacetHandler(ComponentConfig config) |
72 | |
{ |
73 | 0 | super(config); |
74 | 0 | _name = getRequiredAttribute("name"); |
75 | 0 | _required = getAttribute("required"); |
76 | 0 | } |
77 | |
|
78 | |
@SuppressWarnings("unchecked") |
79 | |
@Override |
80 | |
public void apply(FaceletContext ctx, UIComponent parent) |
81 | |
throws IOException |
82 | |
{ |
83 | 0 | if (((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata()) |
84 | |
{ |
85 | 0 | String facetName = _name.getValue(ctx); |
86 | |
|
87 | 0 | UIComponent compositeBaseParent |
88 | |
= FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack(); |
89 | |
|
90 | 0 | CompositeComponentBeanInfo beanInfo = |
91 | |
(CompositeComponentBeanInfo) compositeBaseParent.getAttributes() |
92 | |
.get(UIComponent.BEANINFO_KEY); |
93 | |
|
94 | 0 | if (beanInfo == null) |
95 | |
{ |
96 | 0 | if (log.isLoggable(Level.SEVERE)) |
97 | |
{ |
98 | 0 | log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY "); |
99 | |
} |
100 | 0 | return; |
101 | |
} |
102 | |
|
103 | 0 | BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); |
104 | |
|
105 | 0 | List<String> facetList = (List<String>) beanDescriptor.getValue(RENDER_FACET_USED); |
106 | |
|
107 | 0 | if (facetList == null) |
108 | |
{ |
109 | |
|
110 | 0 | facetList = new ArrayList<String>(); |
111 | 0 | beanDescriptor.setValue( |
112 | |
RENDER_FACET_USED, |
113 | |
facetList); |
114 | |
} |
115 | |
|
116 | 0 | facetList.add(facetName); |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | 0 | } |
122 | |
else |
123 | |
{ |
124 | 0 | super.apply(ctx, parent); |
125 | |
} |
126 | 0 | } |
127 | |
|
128 | |
@Override |
129 | |
public void onComponentPopulated(FaceletContext ctx, UIComponent c, |
130 | |
UIComponent parent) |
131 | |
{ |
132 | 0 | if (!((AbstractFaceletContext)ctx).isBuildingCompositeComponentMetadata()) |
133 | |
{ |
134 | 0 | UIComponent parentCompositeComponent |
135 | |
= FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack(); |
136 | |
|
137 | 0 | String facetName = _name.getValue(ctx); |
138 | |
|
139 | 0 | if (_required != null && _required.getBoolean(ctx) && parentCompositeComponent.getFacet(facetName) == null) |
140 | |
{ |
141 | 0 | throw new TagException(this.tag, "Cannot find facet with name '"+facetName+"' in composite component"); |
142 | |
} |
143 | |
|
144 | 0 | c.getAttributes().put(UIComponent.FACETS_KEY, facetName); |
145 | |
} |
146 | 0 | } |
147 | |
} |