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 javax.faces.component.FacesComponent;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIComponentBase;
24 import javax.faces.component.UIViewRoot;
25 import javax.faces.component.html.HtmlOutputText;
26 import javax.faces.context.FacesContext;
27 import javax.faces.event.AbortProcessingException;
28 import javax.faces.event.PreRenderViewEvent;
29 import javax.faces.event.SystemEvent;
30 import javax.faces.event.SystemEventListener;
31
32 @FacesComponent(value = "com.myapp.UIToggleComponent")
33 public class UIToggleComponent extends UIComponentBase implements
34 SystemEventListener
35 {
36
37
38
39
40
41 public UIToggleComponent()
42 {
43
44 setRendererType("testcomponent");
45
46 FacesContext context = FacesContext.getCurrentInstance();
47 UIViewRoot root = context.getViewRoot();
48
49 root.subscribeToViewEvent(PreRenderViewEvent.class, this);
50 }
51
52
53
54
55
56 @Override
57 public String getFamily()
58 {
59
60 return "com.myapp";
61 }
62
63 public boolean isListenerForSource(Object source)
64 {
65
66 return (source instanceof UIViewRoot);
67 }
68
69 public void processEvent(SystemEvent event) throws AbortProcessingException
70 {
71
72 FacesContext facesContext = FacesContext.getCurrentInstance();
73 if (!facesContext.isPostback())
74 {
75 HtmlOutputText text1 = new HtmlOutputText();
76 text1.setId(facesContext.getViewRoot().createUniqueId());
77 text1.setValue("Manually added child 1<br/>");
78 text1.setEscape(false);
79 getChildren().add(text1);
80
81 HtmlOutputText text2 = new HtmlOutputText();
82 text2.setId(facesContext.getViewRoot().createUniqueId());
83 text2.setValue("Manually added child 2<br/>");
84 text2.setEscape(false);
85 getChildren().add(text2);
86
87
88
89 }
90
91 UIComponent component = getChildren().remove(0);
92 getChildren().add(component);
93 }
94 }