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.io.IOException;
22 import java.util.Map;
23 import javax.faces.FacesException;
24 import javax.faces.component.StateHolder;
25 import javax.faces.component.UIComponent;
26 import javax.faces.context.FacesContext;
27 import javax.faces.event.ComponentSystemEvent;
28 import javax.faces.event.ComponentSystemEventListener;
29 import javax.faces.view.facelets.Facelet;
30 import org.apache.myfaces.config.RuntimeConfig;
31 import org.apache.myfaces.view.facelets.AbstractFacelet;
32 import org.apache.myfaces.view.facelets.DynamicComponentRefreshTransientBuildEvent;
33 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
34 import org.apache.myfaces.view.facelets.FaceletDynamicComponentRefreshTransientBuildEvent;
35 import org.apache.myfaces.view.facelets.FaceletFactory;
36 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage;
37 import org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguageBase;
38 import org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener;
39 import org.apache.myfaces.view.facelets.tag.jsf.ComponentSupport;
40
41
42
43
44
45
46
47 public class CreateDynamicCompositeComponentListener
48 implements ComponentSystemEventListener, StateHolder
49 {
50 private String taglibURI;
51 private String tagName;
52 private Map<String,Object> attributes;
53 private String baseKey;
54
55 public CreateDynamicCompositeComponentListener(String taglibURI, String tagName,
56 Map<String, Object> attributes, String baseKey)
57 {
58 this.taglibURI = taglibURI;
59 this.tagName = tagName;
60 this.attributes = attributes;
61 this.baseKey = baseKey;
62 }
63
64 public CreateDynamicCompositeComponentListener()
65 {
66 }
67
68 public void processEvent(ComponentSystemEvent event)
69 {
70 FacesContext facesContext = FacesContext.getCurrentInstance();
71
72 FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage)
73 facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(
74 facesContext, facesContext.getViewRoot().getViewId());
75
76 Facelet componentFacelet;
77 FaceletFactory faceletFactory = vdl.getFaceletFactory();
78
79 FaceletFactory.setInstance(faceletFactory);
80 try
81 {
82 componentFacelet
83 = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
84 }
85 finally
86 {
87 FaceletFactory.setInstance(null);
88 }
89
90 UIComponent component = event.getComponent();
91
92
93
94
95
96 Integer step = (Integer) component.getAttributes().get(
97 CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW);
98 if (step != null && step.intValue() == 0)
99 {
100 component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
101 }
102 else
103 {
104 return;
105 }
106 try
107 {
108 facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD,
109 Boolean.TRUE);
110
111
112
113
114 UIComponent parent = component.getParent();
115 String facetName = null;
116 if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component))
117 {
118 facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
119 }
120
121
122 try
123 {
124 if (facetName != null)
125 {
126 parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY,
127 facetName);
128 }
129
130
131 String markId = (String) component.getAttributes().get("oam.vf.GEN_MARK_ID");
132 if (markId == null)
133 {
134 ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
135 facesContext, component, baseKey);
136 }
137 else
138 {
139 try
140 {
141 component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
142 ((AbstractFacelet)componentFacelet).applyDynamicComponentHandler(
143 facesContext, component.getParent(), baseKey);
144 }
145 finally
146 {
147 component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
148 }
149 }
150
151 if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext))
152 {
153 FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
154 if (fcc == null)
155 {
156 FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
157 FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
158 component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new
159 RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
160 component.getAttributes().put(
161 DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
162
163 }
164 else
165 {
166 component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new
167 RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
168 }
169 }
170 }
171 finally
172 {
173 if (facetName != null)
174 {
175 parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.jsf.core.FacetHandler.KEY);
176 }
177 }
178 }
179 catch (IOException e)
180 {
181 throw new FacesException(e);
182 }
183 finally
184 {
185 facesContext.getAttributes().remove(
186 FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
187 }
188 }
189
190 public Object saveState(FacesContext context)
191 {
192 RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
193 context.getExternalContext());
194 Object[] values = new Object[4];
195 Integer tagId = runtimeConfig.getIdByNamespace().get(taglibURI);
196 if (tagId != null)
197 {
198 values[0] = tagId;
199 }
200 else if (taglibURI.startsWith(CompositeResourceLibrary.NAMESPACE_PREFIX))
201 {
202 values[0] = new Object[]{0, taglibURI.substring(35)};
203 }
204 else if(taglibURI.startsWith(CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX))
205 {
206 values[0] = new Object[]{1, taglibURI.substring(34)};
207 }
208 else
209 {
210 values[0] = taglibURI;
211 }
212 values[1] = tagName;
213 values[2] = attributes;
214 values[3] = baseKey;
215 return values;
216 }
217
218 public void restoreState(FacesContext context, Object state)
219 {
220 Object[] values = (Object[]) state;
221 if (values[0] instanceof String)
222 {
223 taglibURI = (String) values[0];
224 }
225 else if (values[0] instanceof Integer)
226 {
227 RuntimeConfig runtimeConfig = RuntimeConfig.getCurrentInstance(
228 context.getExternalContext());
229 taglibURI = runtimeConfig.getNamespaceById().get((Integer)values[0]);
230 }
231 else if (values[0] instanceof Object[])
232 {
233 Object[] def = (Object[])values[0];
234 String ns = ( ((Integer)def[0]).intValue() == 0) ?
235 CompositeResourceLibrary.NAMESPACE_PREFIX :
236 CompositeResourceLibrary.ALIAS_NAMESPACE_PREFIX;
237 taglibURI = ns + (String)(((Object[])values[0])[1]);
238 }
239 tagName = (String)values[1];
240 attributes = (Map<String,Object>) values[2];
241 baseKey = (String)values[3];
242 }
243
244 public boolean isTransient()
245 {
246 return false;
247 }
248
249 public void setTransient(boolean newTransientValue)
250 {
251 }
252
253 }