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.pss.acid.component;
20
21 import java.util.HashMap;
22 import java.util.Map;
23 import javax.faces.component.FacesComponent;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIComponentBase;
26 import javax.faces.component.UIOutput;
27 import javax.faces.component.UIViewRoot;
28 import javax.faces.context.FacesContext;
29 import javax.faces.event.AbortProcessingException;
30 import javax.faces.event.PreRenderViewEvent;
31 import javax.faces.event.SystemEvent;
32 import javax.faces.event.SystemEventListener;
33 import javax.faces.view.ViewDeclarationLanguage;
34
35 @FacesComponent(value = "com.myapp.UIAddSimpleCCVDL")
36 public class UIAddSimpleCCVDL extends UIComponentBase implements
37 SystemEventListener
38 {
39
40
41
42
43
44 public UIAddSimpleCCVDL()
45 {
46 setRendererType(null);
47
48 FacesContext context = FacesContext.getCurrentInstance();
49 UIViewRoot root = context.getViewRoot();
50
51 root.subscribeToViewEvent(PreRenderViewEvent.class, this);
52 }
53
54
55
56
57
58 @Override
59 public String getFamily()
60 {
61
62 return "com.myapp";
63 }
64
65 public boolean isListenerForSource(Object source)
66 {
67
68 return (source instanceof UIViewRoot);
69 }
70
71 public void processEvent(SystemEvent event) throws AbortProcessingException
72 {
73 FacesContext facesContext = FacesContext.getCurrentInstance();
74 if (!facesContext.isPostback())
75 {
76 ViewDeclarationLanguage vdl = facesContext.getApplication().
77 getViewHandler().getViewDeclarationLanguage(
78 facesContext, facesContext.getViewRoot().getViewId());
79
80 Map<String, Object> attributes = new HashMap<String, Object>();
81 UIComponent component = vdl.createComponent(facesContext,
82 "http://java.sun.com/jsf/composite/testComposite",
83 "dynComp_1", attributes);
84 UIOutput text = (UIOutput) facesContext.getApplication().
85 createComponent(UIOutput.COMPONENT_TYPE);
86 text.setValue("Dynamically added header");
87 component.getFacets().put("header", text);
88
89 getChildren().add(component);
90 }
91 }
92 }