View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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      // Constructor
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      // Public methods
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           // Clear the existing tree. 
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          // Start building the new component tree with formRoot as parent.
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  }