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.AttachedObjectTarget; |
30 | |
import javax.faces.view.facelets.FaceletContext; |
31 | |
import javax.faces.view.facelets.TagAttribute; |
32 | |
import javax.faces.view.facelets.TagConfig; |
33 | |
import javax.faces.view.facelets.TagHandler; |
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.FaceletCompositionContext; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
@JSFFaceletTag |
49 | |
public abstract class AttachedObjectTargetHandler<T extends AttachedObjectTarget> |
50 | |
extends TagHandler implements InterfaceDescriptorCreator |
51 | |
{ |
52 | |
|
53 | |
|
54 | 0 | private static final Logger log = Logger.getLogger(AttachedObjectTargetHandler.class.getName()); |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
@JSFFaceletAttribute(name="name", |
62 | |
className="javax.el.ValueExpression", |
63 | |
deferredValueType="java.lang.String", |
64 | |
required=true) |
65 | |
protected final TagAttribute _name; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
@JSFFaceletAttribute(name="targets", |
71 | |
className="javax.el.ValueExpression", |
72 | |
deferredValueType="java.lang.String") |
73 | |
protected final TagAttribute _targets; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
private boolean _cacheable; |
80 | |
|
81 | |
private AttachedObjectTarget _target; |
82 | |
|
83 | |
public AttachedObjectTargetHandler(TagConfig config) |
84 | |
{ |
85 | 0 | super(config); |
86 | 0 | _name = getRequiredAttribute("name"); |
87 | 0 | _targets = getAttribute("targets"); |
88 | 0 | if (_name.isLiteral()) |
89 | |
{ |
90 | 0 | _cacheable = true; |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | 0 | _cacheable = false; |
95 | |
} |
96 | 0 | } |
97 | |
|
98 | |
@SuppressWarnings("unchecked") |
99 | |
public void apply(FaceletContext ctx, UIComponent parent) |
100 | |
throws IOException |
101 | |
{ |
102 | 0 | UIComponent compositeBaseParent |
103 | |
= FaceletCompositionContext.getCurrentInstance(ctx).getCompositeComponentFromStack(); |
104 | |
|
105 | 0 | CompositeComponentBeanInfo beanInfo = |
106 | |
(CompositeComponentBeanInfo) compositeBaseParent.getAttributes() |
107 | |
.get(UIComponent.BEANINFO_KEY); |
108 | |
|
109 | 0 | if (beanInfo == null) |
110 | |
{ |
111 | 0 | if (log.isLoggable(Level.SEVERE)) |
112 | |
{ |
113 | 0 | log.severe("Cannot find composite bean descriptor UIComponent.BEANINFO_KEY "); |
114 | |
} |
115 | 0 | return; |
116 | |
} |
117 | |
|
118 | 0 | BeanDescriptor beanDescriptor = beanInfo.getBeanDescriptor(); |
119 | |
|
120 | |
|
121 | 0 | List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) |
122 | |
beanDescriptor.getValue( |
123 | |
AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY); |
124 | |
|
125 | 0 | if (targetList == null) |
126 | |
{ |
127 | |
|
128 | 0 | targetList = new ArrayList<AttachedObjectTarget>(); |
129 | 0 | beanDescriptor.setValue( |
130 | |
AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY, |
131 | |
targetList); |
132 | |
} |
133 | |
|
134 | |
|
135 | 0 | if (isCacheable()) |
136 | |
{ |
137 | 0 | if (_target == null) |
138 | |
{ |
139 | 0 | _target = createAttachedObjectTarget(ctx); |
140 | |
} |
141 | 0 | targetList.add(_target); |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | 0 | AttachedObjectTarget target = createAttachedObjectTarget(ctx); |
146 | 0 | targetList.add(target); |
147 | |
} |
148 | |
|
149 | 0 | this.nextHandler.apply(ctx, parent); |
150 | 0 | } |
151 | |
|
152 | |
public boolean isCacheable() |
153 | |
{ |
154 | 0 | return _cacheable; |
155 | |
} |
156 | |
|
157 | |
public void setCacheable(boolean cacheable) |
158 | |
{ |
159 | 0 | _cacheable = cacheable; |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
protected abstract T createAttachedObjectTarget(FaceletContext ctx); |
175 | |
} |