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.managed;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.faces.bean.ManagedBean;
24  import javax.faces.bean.RequestScoped;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIOutput;
27  import javax.faces.component.UIPanel;
28  import javax.faces.component.html.HtmlPanelGroup;
29  import javax.faces.context.FacesContext;
30  import javax.faces.view.ViewDeclarationLanguage;
31  
32  /**
33   *
34   * @author Leonardo Uribe
35   */
36  @ManagedBean(name="componentBindingVDLBean3")
37  @RequestScoped
38  public class ComponentBindingVDLBean3
39  {
40      private UIPanel panel;
41      
42      public UIPanel getPanel()
43      {
44          if (panel == null)
45          {
46              panel = new HtmlPanelGroup();
47              FacesContext facesContext = FacesContext.getCurrentInstance();
48              if (facesContext.isPostback())
49              {
50                  // Just try to mess the binding. In theory this does
51                  // not have effect, because the binding with children
52                  // or facets should be restored fully.
53                  UIOutput out2 = new UIOutput();
54                  out2.setValue("hello2");
55                  panel.getChildren().add(out2);
56              }
57              UIOutput out = new UIOutput();
58              out.setValue("hello1");
59              panel.getChildren().add(out);
60              
61              ViewDeclarationLanguage vdl = facesContext.getApplication().
62                  getViewHandler().getViewDeclarationLanguage(
63                      facesContext, facesContext.getViewRoot().getViewId());
64              
65              Map<String, Object> attributes = new HashMap<String, Object>();
66              UIComponent cc = vdl.createComponent(facesContext, 
67                  "http://java.sun.com/jsf/composite/testComposite", 
68                  "dynComp_3", attributes);
69              UIOutput text = (UIOutput) facesContext.getApplication().
70                  createComponent(UIOutput.COMPONENT_TYPE);
71              text.setValue("Dynamically added header");
72              cc.getChildren().add(text);
73              panel.getChildren().add(cc);
74  
75              if (!facesContext.isPostback())
76              {
77                  // Store something into the state
78                  panel.getAttributes().put("attr1", "value1");
79                  panel.getChildren().get(0).getAttributes().put("attr2", "value2");
80              }
81              else
82              {
83                  //Try to mess the state, in theory it should not have effect
84                  panel.getAttributes().remove("attr1");
85                  panel.getChildren().get(0).getAttributes().remove("attr2");
86              }
87          }
88          return panel;
89      }
90      
91      public void setPanel(UIPanel panel)
92      {
93          this.panel = panel;
94      }
95  }