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.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.UIAddSimpleCCVDL3", createTag=true, 
36          namespace="http://testcomponent", tagName="addSimpleCCVDL3")
37  public class UIAddSimpleCCVDL3 extends UIComponentBase implements
38          SystemEventListener
39  {
40  
41      //
42      // Constructor
43      //
44  
45      public UIAddSimpleCCVDL3()
46      {
47          setRendererType(null);
48  
49          FacesContext context = FacesContext.getCurrentInstance();
50          UIViewRoot root = context.getViewRoot();
51          if (root != null)
52          {
53              root.subscribeToViewEvent(PreRenderViewEvent.class, this);
54          }
55      }
56  
57      //
58      // Public methods
59      //
60  
61      @Override
62      public String getFamily()
63      {
64  
65          return "com.myapp";
66      }
67  
68      public boolean isListenerForSource(Object source)
69      {
70  
71          return (source instanceof UIViewRoot);
72      }
73  
74      public void processEvent(SystemEvent event) throws AbortProcessingException
75      {
76          FacesContext facesContext = FacesContext.getCurrentInstance();
77          if (!facesContext.isPostback())
78          {
79              ViewDeclarationLanguage vdl = facesContext.getApplication().
80                  getViewHandler().getViewDeclarationLanguage(
81                      facesContext, facesContext.getViewRoot().getViewId());
82  
83              Map<String, Object> attributes = new HashMap<String, Object>();
84              UIComponent component = vdl.createComponent(facesContext, 
85                  "http://java.sun.com/jsf/composite/testComposite", 
86                  "dynComp_3", attributes);
87              UIOutput text = (UIOutput) facesContext.getApplication().
88                  createComponent(UIOutput.COMPONENT_TYPE);
89              text.setValue("Dynamically added header");
90              component.getChildren().add(text);
91              
92              getChildren().add(component);
93          }
94      }
95  }