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 javax.faces.component.FacesComponent;
22  import javax.faces.component.UIComponentBase;
23  import javax.faces.component.UIViewRoot;
24  import javax.faces.component.html.HtmlOutputText;
25  import javax.faces.context.FacesContext;
26  import javax.faces.event.AbortProcessingException;
27  import javax.faces.event.PreRenderViewEvent;
28  import javax.faces.event.SystemEvent;
29  import javax.faces.event.SystemEventListener;
30  
31  @FacesComponent(value = "com.myapp.UIAddComponent")
32  public class UIAddComponent extends UIComponentBase implements
33          SystemEventListener
34  {
35  
36      //
37      // Constructor
38      //
39  
40      public UIAddComponent()
41      {
42  
43          setRendererType("testcomponent");
44  
45          FacesContext context = FacesContext.getCurrentInstance();
46          UIViewRoot root = context.getViewRoot();
47  
48          root.subscribeToViewEvent(PreRenderViewEvent.class, this);
49      }
50  
51      //
52      // Public methods
53      //
54  
55      @Override
56      public String getFamily()
57      {
58  
59          return "com.myapp";
60      }
61  
62      public boolean isListenerForSource(Object source)
63      {
64  
65          return (source instanceof UIViewRoot);
66      }
67  
68      public void processEvent(SystemEvent event) throws AbortProcessingException
69      {
70  
71          if (!FacesContext.getCurrentInstance().isPostback())
72          {
73  
74              HtmlOutputText component = new HtmlOutputText();
75              component.setValue("Dynamically added child");
76              getChildren().add(component);
77          }
78      }
79  }