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.Random;
22 import javax.faces.component.FacesComponent;
23 import javax.faces.component.UIComponentBase;
24 import javax.faces.component.UIOutput;
25 import javax.faces.component.UIViewRoot;
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.UIDynamicFormComponent")
33 public class UIDynamicFormComponent extends UIComponentBase implements
34 SystemEventListener
35 {
36
37
38
39
40
41 public UIDynamicFormComponent()
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 this.getChildren().clear();
73
74 Integer index = (Integer) this.getAttributes().get("index");
75 if (index == null)
76 {
77 index = 1;
78 }
79 else
80 {
81 index = index + 1;
82 }
83 this.getAttributes().put("index", index);
84
85
86 Random random = new Random() ;
87 int n = random.nextInt(9)+1;
88
89 for(int i = 0; i < n; i++)
90 {
91 UIOutput input = new UIOutput();
92 input.setId("input_"+index+"_"+i);
93 this.getChildren().add(input);
94 }
95 }
96 }