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.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      // Constructor
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      // 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  
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              //<h:outputText value="Manually added child 1&lt;br/&gt;" escape="false"/>
88              //<h:outputText value="Manually added child 2&lt;br/&gt;" escape="false"/>
89          }
90  
91          UIComponent component = getChildren().remove(0);
92          getChildren().add(component);
93      }
94  }